This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I am porting over code that was originally developed on top of Entity Framework, and the entities were all POCO entities under EF. When we do unit testing (well intergration testing actually since we are testing the DB layers against a test database), we might have a unit test like the following: // Find all countries. There should be 244 The problem I am having is my Are.Equal function. This is a function I wrote for doing unit testing, that will enumerate through the properites of the entity using reflection and throw an exception when they don't match. It handles primitive types, complex types, enumerations etc. But the problem of course is that now it all blows up comparing the properties in the entity because there are a lot more properties in the entity coming from the base class, as well as navigation properties that get lazy loaded. Somehow I need to find a way to flag the propetites in the entity that I want to compare, and ignore that that I don't. Is it possible to use a custom code generation template so I could somehow augment the entity properties with attributes so that only the 'normal' properties (ie: the entity values) and the primary and foreign key ID's would be compared? All the navigation properties and reference properties would be left alone and not touched, so it won't be causing lazy loading of related data? And the base class properties would be ignored also? The only other option I have would be to change my code to test each property I want to test separately, but that means I have to re-write a ton of unit tests ;( |
|
|
Absolutely. Here's an article which should get you started: http://www.mindscapehq.com/blog/index.php/2009/09/16/customising-lightspeed-entity-templates/ You could also do it by modifying Are.Equal to skip the unwanted base class and lazy-load properties by: * Checking PropertyInfo.DeclaringType. You could exclude properties declared on Entity, but keep properties declared lower down the hierarchy. * Checking typeof(Entity).IsAssignableFrom(PropertyInfo.PropertyType). This would detect to-one navigation properties so you could skip them. * Checking PropertyInfo.PropertyType.IsGenericType && PropertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(EntityCollection<>). This would detect to-many navigation properties so you could skip them too. You could detect through association properties in a similar way. |
|
|
I could not skip the base class since it contains the Id property, which we do want to check against. But it turns out this was easy. I already had code that skipped anything but value and string properties for complex types, so it already skipped over reference and collection properites (forgot I already wrote that for OpenAccess when I was tesing it :) ). It was barfing on properties in the base class like IsValid and EntityState which are value properties. But it turns out that in the base class all the properties I need to skip conveniently had BrowsableAttribute(false) on them, so I just ignored any properties that were not marked as browseable and now it works. Phew. This is what I ended up with to get the list of properties to compare: var properties = t.GetProperties() |
|