a) Domain entities shouldn’t contain code related to persistence, thus they should be Persistence Ignorant PI. But assuming Domain Model DM is designed using Entity Framework and assuming that service layer performs CRUD operation for POCO domain entities via Linq-to-Entities, would we argue that service layer accesses DAL directly or via Domain Model:
class CustomerService
{
public string doSomething( ... )
{
...
var customer = context.Customers.Where( ... );
...
}
...
}
b) Does using Linq-to-Entities within DM violate PI rule? For example, does the following Customer entity violate PI:
class Customer
{
public string InterestedWhatOtherCustomerOrdered( ... )
{
...
var orders = context.Orders.Where( ... ); // does this violate PI rule?
...
}
...
}
REPLY TO Luke McGregor:
a)
Yeah it does as it refers to the context directly. A better way would
be to use the internal navigation properties on Customer to perform
the same action,
So instead navigation properties should contact the context?! But since navigation properties also reside within domain model, couldn’t we then argue that by contacting the context directly, they too would violate PI?
b) According to Fowler’s PEAA chapter on Data Mapper, it is ok to extract from Data Mapper any methods needed by the domain code into an interface class, which domain code can then use. How exactly could that be done when using EF instead of hand written Data Mapper, in such a manner that we wouldn’t violate PI?
thank you
Yeah it does as it refers to the context directly. A better way would be to use the internal navigation properties on Customer to perform the same action,
or to create a separate query class for this function.