When getting types from an assembly, is there a way to determine if type is an Anonymous Type? - vb.net

I added an Anonymous type to my project:
'Put the courses from the XML file in an Anonymous Type
Dim courses = From myCourse In xDoc.Descendants("Course")
Select New With
{
.StateCode = myCourse.Element("StateCode").Value, _
.Description = myCourse.Element("Description").Value, _
.ShortName = myCourse.Element("ShortName").Value, _
.LongName = myCourse.Element("LongName").Value, _
.Type = myCourse.Element("Type").Value, _
.CipCode = CType(myCourse.Element("CIPCode"), String) _
}
For Each course In courses
If Not UpdateSDECourseCode(acadYear, course.StateCode, course.Description, course.Type, course.ShortName, course.LongName, course.CipCode) Then errors.Add(String.Format("Cannot import State Course Number {0} with Year {1} ", course.StateCode, acadYear))
Next
After doing so, a Unit Test failed:
Public Function GetAreaTypeList() As List(Of Type)
Dim types As New List(Of Type)
Dim asmPath As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "My.Stuff.dll")
For Each t As Type In Reflection.Assembly.LoadFrom(asmPath).GetTypes()
If t.Namespace.StartsWith("My.Stuff.BatchUpdater") Then
If t.BaseType Is GetType(My.Stuff.BatchUpdater.Area) Then
types.Add(t)
End If
End If
Next
Return types
End Function
It fails because a new type has been added to the project (VB$AnonymousType_0`6) and it does not have a property called Namespace.
I fixed by making the following change to the IF Statement:
If Not t.Namespace Is Nothing AndAlso t.Namespace.StartsWith("My.Stuff.BatchUpdater") Then
Since I don't fully understand what's happening, I feel leery about my code change.
Why is the Namespace Nothing for the Anonymous type?
Would you fix your Unit Test in the same fashion? Or should it be something more specific (e.g. If Not t.Names = "VB$AnonymousType_0`6")
UPDATE
decyclone gave me the info I needed to create a better test:
For Each t As Type In Reflection.Assembly.LoadFrom(asmPath).GetTypes()
'Ignore CompilerGeneratedAttributes (e.g. Anonymous Types)
Dim isCompilerGeneratedAttribute = t.GetCustomAttributes(False).Contains(New System.Runtime.CompilerServices.CompilerGeneratedAttribute())
If Not isCompilerGeneratedAttribute AndAlso t.Namespace.StartsWith("My.Stuff.BatchUpdater") Then
'...Do some things here
End If
Next
Honestly, it could be improved more with a LINQ query, but this suits me.

Anonymous methods and types are decorated with CompilerGeneratedAttribute. You can check for their existance to identify an anonymous type.
var anonymous = new { value = 1 };
Type anonymousType = anonymous.GetType();
var attributes = anonymousType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false);
if (attributes.Any())
{
// Anonymous
}
You can filter these out in your test.
It is also possible to mark a user defined type with CompilerGeneratedAttribute. So maybe you can combine it with checking if Namespace is null or not
var anonymous = new { value = 1 };
Type anonymousType = anonymous.GetType();
var attributes = anonymousType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false);
if (attributes.Any() && anonymousType.Namespace == null)
{
// Anonymous
}

Related

Getting original object from Entity Framework SaveChanges function?

For auditing reasons I am overriding the SaveChanges function. However, I want to capture to original and current values as the original object (i.e person) so that I can serialize both the before and after.
Public Overrides Function SaveChanges() As Integer
ChangeTracker.DetectChanges()
Dim ctx As ObjectContext = DirectCast(Me, IObjectContextAdapter).ObjectContext
Dim objectStateEntryList As List(Of ObjectStateEntry) = ctx.ObjectStateManager.
GetObjectStateEntries(EntityState.Added Or EntityState.Modified Or EntityState.Deleted).ToList()
For Each ent As ObjectStateEntry In objectStateEntryList
If Not ent.IsRelationship Then
Dim objectType As Type = ObjectContext.GetObjectType(ent.Entity.GetType)
Dim audit As New Audit With {
.ObjectId = ent.EntityKey.EntityKeyValues.First.Value,
.ObjectType = ObjectContext.GetObjectType(ent.Entity.GetType).Name,
.User = (From u In Users Where u.Username = My.User.Name).First
}
With audit
Select Case ent.State
Case EntityState.Added
.Action = "Created"
.Detail = "Record created"
Case EntityState.Deleted
.Action = "Deleted"
.Detail = "Record deleted"
Case EntityState.Modified
Dim newObj As String = SerializeToString(
Convert.ChangeType(ent.Entity, objectType)
)
.Action = "Modified"
.Detail = newObj.ToString
End Select
End With
End If
Next
Return MyBase.SaveChanges()
End Function
That's how far I got, but when I try and ChangeType it throws "Object must implement IConvertible".
The last time I worked on a project that absolutely needed to track every change, we used a history table and an ON UPDATE trigger. Changing data would fire the trigger, which would then copy the original row into the history table.
This is 100% EF compatible, but you need to set it up separately for each table.
This doesn't exactly answer your main question but may help you to improve your auditing.
Disclaimer: I'm the owner of the project Entity Framework Plus
I recommend you to look at our EF+ Audit Feature, all auditing information can be easily retrieved using this library.
// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntityContext();
// ... ctx changes ...
var audit = new Audit();
audit.CreatedBy = "ZZZ Projects"; // Optional
ctx.SaveChanges(audit);
// Access to all auditing information
var entries = audit.Entries;
foreach(var entry in entries)
{
foreach(var property in entry.Properties)
{
}
}
// CALL your serializer here
SerializeToString(entries, ...);
The code is Open Source.

Translation of c# linq to vb - overload resolution failure in intellisense with 'selectmany' statement

I've tried every translation service under the sun to get this syntax right but I still get "Overload resolution failed because no accessible 'SelectMany' can be called with these arguments"
on the first part of the select statement (up to the full stop just before the groupby keyword)
the original c# statement from an online example I'm trying to get working locally:
public IEnumerable<TagGroup> GetTagGroups()
{
var tagGroups =
// extract the delimited tags string and session id from all sessions
DbSet.Select(s => new { s.Tags, s.Id })
.ToArray() // we'll process them in memory.
// split the "Tags" string into individual tags
// and flatten into {tag, id} pairs
.SelectMany(
s =>
s.Tags.Split(_tagDelimiter, StringSplitOptions.RemoveEmptyEntries)
.Select(t => new { Tag = t, s.Id })
)
// group {tag, id} by tag into unique {tag, [session-id-array]}
.GroupBy(g => g.Tag, data => data.Id)
// project the group into TagGroup instances
// ensuring that ids array in each array are unique
.Select(tg => new TagGroup
{
Tag = tg.Key,
Ids = tg.Distinct().ToArray(),
})
.OrderBy(tg => tg.Tag);
return tagGroups;
}
The closest I've come to it in VB:
Public Function GetTagGroups() As IEnumerable(Of TagGroup)
' extract the delimited tags string and session id from all sessions
' we'll process them in memory.
' split the "Tags" string into individual tags
' and flatten into {tag, id} pairs
' group {tag, id} by tag into unique {tag, [session-id-array]}
' project the group into TagGroup instances
' ensuring that ids array in each array are unique
Dim tagGroups = DbSet.[Select](Function(s) New With { _
s.Tags, _
s.Id _
}).ToArray().SelectMany(Function(s) s.Tags.Split(_tagDelimiter, StringSplitOptions.RemoveEmptyEntries).[Select](Function(t) New With { _
Key .Tag = t, _
s.Id _
})).GroupBy(Function(g) g.Tag, Function(data) data.Id).[Select](Function(tg) New With { _
Key .Tag = tg.Key, _
Key .Ids = tg.Distinct().ToArray() _
}).OrderBy(Function(tg) tg.Tag)
Return tagGroups
End Function
This results in the visual studio 2012 intellisense underlining in blue the first part of the statement from "DbSet" on the first line through to the last parenthesis before the ".GroupBy" near the bottom. The error is "Overload resolution failed because no accessible 'SelectMany' can be called with these arguments".
As it's a code example I'm trying to convert to vb to run locally and understand and I'm not experienced enough with linq I'm completely at a loss of how to try and deal with this. It's way beyond my current understanding so could be a simple syntax error or a complete hash from start to finish for all I know!
Would be very grateful for any pointers.
I have now put this in VS2k8, and as well as the two simple issues with the VB sample I previously noted:
The first two New With constructs must specify .<field> = AFAIK, and
the last New With should not be anonymous.
I now also note these are the declarations I needed to get the code to not have errors. Other than adding intermediate query variables (which BTW mean you could thread the C# comments back in), I don't recall actually changing the code further. Note the _tagDelimiter as a Char() -- what did the C# code declare it as? (Looking at the String.Split overloads that mention StringSplitOptions it has to be Char() or String() or C# is implicitly changing types somewhere that VB.NET doesn't.)
Class TagList
Public Tags As String
Public Id As String
End Class
Private DbSet As IQueryable(Of TagList)
Class TagGroup
Public Tag As String
Public Ids() As String
End Class
Private _tagDelimiter As Char()
Public Function GetTagGroups() As IEnumerable(Of TagGroup)
Dim theTags = DbSet.[Select](Function(s) New With { _
.Tags = s.Tags, _
.Id = s.Id _
}).ToArray()
Dim many = theTags.SelectMany(Function(s) s.Tags.Split(_tagDelimiter, StringSplitOptions.RemoveEmptyEntries).[Select](Function(t) New With { _
Key .Tag = t, _
.Id = s.Id _
}))
Dim grouped = many.GroupBy(Function(g) g.Tag, Function(data) data.Id)
Dim unordered = grouped.[Select](Function(tg) New TagGroup With { _
.Tag = tg.Key, _
.Ids = tg.Distinct().ToArray() _
})
Dim tagGroups = unordered.OrderBy(Function(tg) tg.Tag)
Return tagGroups
End Function

Strange "Expected invocation on the mock at least once, but was never performed" error when I am setting up the Mock

I'm getting this error from Moq via NUnit, and it doesn't make much in the way of sense to me.
"Expected invocation on the mock at least once, but was never performed: x => x.DeleteItem(.$VB$Local_item)"
"at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable1 setups, IEnumerable1 actualCalls, Expression expression, Times times, Int32 callCount)
at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
at Moq.Mock.Verify[T](Mock mock, Expression1 expression, Times times, String failMessage)
at Moq.Mock1.Verify(Expression`1 expression)
at PeekABookEditor.UnitTests.ItemBrowsing.Can_Delete_Item() in C:\Projects\MyProject\MyProject.UnitTests\Tests\ItemBrowsing.vb:line 167"
Very similar code works well in C#, so the error might be minor and syntactical on my part.
Here's my code:
<Test()> _
Public Sub Can_Delete_Item()
'Arrange: Given a repository containing some item...
Dim mockRepository = New Mock(Of IItemsRepository)()
Dim item As New Item With {.ItemID = "24", .Title = "i24"}
mockRepository.Setup(Function(x) x.Items).Returns(New Item() {item}.AsQueryable())
'Act ... when the user tries to delete that product
Dim controller = New ItemsController(mockRepository.Object)
Dim result = controller.Delete(24)
'Assert ... then it's deleted, and the user sees a confirmation
mockRepository.Verify(Sub(x) x.DeleteItem(item))
result.ShouldBeRedirectionTo(New With {Key .action = "List"})
Assert.AreEqual(DirectCast(controller.TempData("message"), String), "i24 was deleted")
End Sub
The guilty line appears to be "mockRepository.Verify(Sub(x) x.DeleteItem(item))"
Any thoughts on how to fix this?
Working C# code isn't the exact same, but here it is:
[Test]
public void Can_Delete_Product()
{
// Arrange: Given a repository containing some product...
var mockRepository = new Mock<IProductsRepository>();
var product = new Product { ProductID = 24, Name = "P24"};
mockRepository.Setup(x => x.Products).Returns(
new[] { product }.AsQueryable()
);
// Act: ... when the user tries to delete that product
var controller = new AdminController(mockRepository.Object);
var result = controller.Delete(24);
// Assert: ... then it's deleted, and the user sees a confirmation
mockRepository.Verify(x => x.DeleteProduct(product));
result.ShouldBeRedirectionTo(new { action = "Index" });
controller.TempData["message"].ShouldEqual("P24 was deleted");
}
In your VB test method, you create Item with a string ItemID = "24", but you call the controller.Delete method with an integer value of 24.
Check your controller code and see if the type discrepancy results in the item not being identified correctly, so either DeleteItem is not called at all, or is called with a different Item.

How do I return a value to a sqldatareader if value is null?

I am currently using a sql data reader (in vb.net) to extract an article object via a stored proc from a SQL Server 2008 database. Part of this object includes the two properties shown below:
theArticle.Truthfulness = ((myReader.GetInt32(myReader.GetOrdinal("Truthfulness"))))
theArticle.Relevance = ((myReader.GetInt32(myReader.GetOrdinal("Relevance"))))
My problem is that the Truthfulness and Relevance may return a null value and this is causing the function to fall over.
I think I understand why. I am asking for an integer value (getin32) and because null is returned it fails.
How do I accommodate the null value from the database so it does not fall over?
You can check whether or not a given ordinal position is null using .IsDBNull() and then do something - e.g. set your value to -1 or something:
int myOrdinal = myReader.GetOrdinal("Truthfullness");
if(myReader.IsDBNull(myOrdinal))
{
theArticle.Truthfulness = -1;
}
else
{
theArticle.Truthfulness = myReader.GetInt32(myOrdinal);
}
As Mike Hofer points out in his answer, you could also wrap all this logic into an extension method:
public static class SqlDataReaderExtensions
{
public static int SafeGetInt32(this SqlDataReader reader,
string columnName, int defaultValue)
{
int ordinal = reader.GetOrdinal(columnName);
if(!reader.IsDbNull(ordinal))
{
return reader.GetInt32(ordinal);
}
else
{
return defaultValue;
}
}
}
and then just use that "SafeGetInt32" method instead:
theArticle.Truthfulness = myReader.SafeGetInt32("Truthfullness", -1);
Marc
Did you check, SqlDataReader.IsDBNull Method? Probably something like:
if(myReader.IsDBNull(myReader.GetOrdinal("Truthfulness"))
theArticle.Truthfulness = string.Empty;
else
theArticle.Truthfulness = ((myReader.GetInt32(myReader.GetOrdinal("Truthfulness"))))
You know, I deal with this all the time in Oracle. To clean the code up, I wrote a set of extension methods to simplify the operation:
using System.Data.OracleClient;
public static class OracleDataReaderExtensions
{
public static int GetInt32(this OracleDataReader reader, string columnName, int defaultValue)
{
return reader.GetInt32(reader.GetOrdinal(columnName)) != DbNull.Value ?
reader.GetInt32(reader.GetOrdinal(columnName)) :
defaultValue;
}
}
Create a separate overload for each type you want to return. I primarily work with string, int, date, and decimal. Remember YAGNI (you don't need to work with every type supported by the reader, only those you actually use.)
An extension class like this for SQL Server is really easy to write, and will VASTLY simplify your work. Trust me on that. Would I lie to you? :)
This generic version may be of use:
private T ValueOrDefault<T>(System.Data.IDataReader rdr, string columnName)
{
T vod = default(T);
try
{
int idx = rdr.GetOrdinal(columnName);
if (!rdr.IsDBNull(idx))
return (T)rdr[idx];
}
catch (IndexOutOfRangeException) { }
return vod;
}
Could be extended to catch InvalidCastException, or use Convert.ChangeType instead of casting?
IsDbNull(int) is usually much slower that using methods like GetSqlInt32 and then comparing to DBNull.Value or using it's own .IsNull Like:
public static int Int32(this SqlDataReader r, int ord)
{
var t = r.GetSqlInt32(ord);
return t.IsNull ? default(int) : t.Value;
}
Tried a few template solutions but to no avail so far. The problem is that all Sql-types (SqlInt32 here) types are actually structs and while they all have .Value property C# doesn't have real templates to handle that. Also they have their own INullable interface which has only .IsNull and is not conpatible with Nyllable<>.
I suspect that one would need full set of Sql-types as C# templates or to add ICOnvertible to them in order to be able to have just one or two templated methods.
If someone has maybe an idea with a functional trick or two speak up :-)
Here is what we use on SQLServer and it works like a charm:
...
Dim X as Object = pbDr("TotAmt") 'dr is dim'ed as a DataReader
...
Public Function pbDr(ByVal drName As String) As Object
Dim SQLError As SqlClient.SqlException
Dim IsNull As Boolean
Dim Ordinal, DispNbr As Integer
Try
Ordinal = dr.GetOrdinal(drName)
IsNull = dr.IsDBNull(Ordinal)
If IsNull Then
Dim Dbtype As String = dr.GetFieldType(Ordinal).ToString
If Dbtype = "System.String" Then
Return ""
ElseIf Dbtype = "System.Int32" _
OrElse Dbtype = "System.Double" _
OrElse Dbtype = "System.Decimal" _
OrElse Dbtype = "System.Int16" Then
Return 0
Else
MsgBox("Print This Screen And Send To Support" _
& "pbdr-Object = " & Dbtype, MsgBoxStyle.Critical)
Return ""
End If
Else
Return dr(Ordinal)
End If
Catch sqlerror
Call DispSQLError(SQLError, "pbDr")
pbDr = ""
End Try
End Function
Nowadays, you probably want the null if the database returns it and as such you would use a Nullable<int>:
public static class Extensions
{
public static int? GetNullableInt32(this SqlDataReader reader, int ordinal)
{
if (reader.IsDBNull(ordinal))
return null;
return reader.GetInt32(ordinal);
}
public static long? GetNullableInt64(this SqlDataReader reader, int ordinal)
{
if (reader.IsDBNull(ordinal))
return null;
return reader.GetInt64(ordinal);
}
}

How to refactor function that takes lots of arguments?

I wrote a wrapper for a COM object that only excepted strings as input, so in good OOP practice I wrapped the string up in a function so that it was easier to build and call.
I was just wondering if anyone could think of a better way to do the following code.
Public Function OpenTable(ByVal TablePath As String, Optional ByVal OpenAs As String = Nothing, _
Optional ByVal Hide As Boolean = False, Optional ByVal AsReadOnly As Boolean = False, _
Optional ByVal Interactive As Boolean = True, Optional ByVal Password As String = Nothing, _
Optional ByVal NoIndex As Boolean = False, Optional ByVal ViewAutomatic As Boolean = True) As TableInfo
If String.IsNullOrEmpty(TablePath) Then
Throw New ArgumentNullException("TablePath", "TablePath cannot be null or empty")
End If
Dim Builder = New StringBuilder("Open Table ")
Builder.AppendFormat("{0}{1}{2}", ControlChars.Quote, TablePath, ControlChars.Quote)
If (Not String.IsNullOrEmpty(OpenAs)) Then Builder.AppendFormat(" as {0} ", OpenAs)
If (Hide) Then Builder.Append(" Hide ")
If (AsReadOnly) Then Builder.Append(" ReadOnly ")
If (Interactive) Then Builder.Append(" Interactive ")
If (Not String.IsNullOrEmpty(Password)) Then Builder.AppendFormat(" Password {0} ", Password)
If (NoIndex) Then Builder.Append(" NoIndex ")
If (ViewAutomatic) Then Builder.Append(" View Automatic ")
MyComApp.Do(Builder.ToString)
Dim FileInfo = New IO.FileInfo(TablePath)
Return New TableInfo(FileInfo.Name.Substring(0, InStrRev(FileInfo.Name, ".") - 1))
End Function
The amount of arguments that the function has to take is my biggest worry. This one is not too bad but there are some other functions that I may have to make in the future that will take a lot more arguments, so I'm mainly looking for better ways to build large argument functions.
In this case it seems many of the parameters are just 'configuration values' (which end up being strings), you could modify it to accept a single class for all the configuration that you prepare before the call and that will return you the string accordingly.
Something like
class COMConfiguration {
private bool Hide = false;
private bool AsReadOnly = false;
//and so on...
public void setHide(bool v) { Hide = v; }
//only setters
public string getConfigString() {
StringBuilder sb = new StringBuilder();
if (Hide) { sb.Append(" Hide "); }
if (AsReadOnly) { sb.Append(" ReadOnly "); }
//and so on
return sb.ToString()
}
}
One way to handle functions that can take lots of arguments is to create a new object type whose sole purpose is to hold arguments for that function. Then you create a new object of that type, set the properties as needed, then pass that one object reference to your OpenTable function.
Since i dont know your programming language, im gonna keep this to pseudo code, but my general answer is to use ann array as single parameter:
function OpenTable( options As array) {
if (options is not array or options is empty) {
Throw exception
}
return_string = "";
if ( key is set ('readOnly', options) and is not empty) {
return_string = return_string + ' readonly';
}
// repeat last 3 lines for all your params
}
Ok the last part of your function doesnt make sense to me, but the idea of array of params should come across i think. Good luck.
You can switch all your boolean parameters to a single parameter of an enum type, marked as Flags. Here's an example declaration:
' Define an Enum with FlagsAttribute.
<FlagsAttribute( )> _
Enum TableOptions as Short
Hide = 1
AsReadOnly = 2
Interactive = 4
NoIndex = 8
ViewAutomatic = 16
End Enum