Dedicated datacontext and foreign keys / navigation properties

I have a problem that I could not find a solution, and I wonder if anyone can give advice.

I have a mocked datacontext / objectset made through interfaces and t4 templates, with some ninject magic, with the intention of having memory in the data sets for unit testing.

However, what should you do with foreign key values ​​/ navigation properties?

Suppose I have hotels and clients, ctx. Hotels have some values, but Customer.Hotels do not. It turns out something like this if this is a one-to-one relationship:

return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Hotel>("HotelModel.FK_Customers_Hotels", "Hotel").Value; 

and one-to-many:

 return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<BookingRow>("HotelModel.FK_BookingRows_Customers", "BookingRow"); 

The level of my skill is not enough to even understand what is happening here.

[edit:] Grand Master Julie Lerman confirms this is a dead end. You cannot correctly simulate object objects, for this you need POCOs.

+1
source share
1 answer

Mocking ObjectContext when you use EntityObject is basically impossible, because, for example, RelationshipManager is a real class that cannot be replaced with your layout. In addition, your entities are highly dependent on incompatible EF code.

Note: β€œBasically” because you can mock it, but you need special intercepting calls for real objects and forwarding them instead of your methods. This is only possible with TypeMock Isolator or MS Moles.

Btw. mocking EF code is what you don't want to do - go through this answer and related answers. Some of them focus on the new EF API, but the problems are the same.

+3
source

Source: https://habr.com/ru/post/951741/


All Articles