Home  /  Questions  /  Question

88   96.5
Feb 24, 2012

EntitityFramework with Repository Layers


   There are lots of articles about EntityFramework with Repository Layer. We are using it like  below
   for the Account  -->AccountRepository
   fo the User--> UserRepository  ...  like so.

  But we always use one datacontext ,one entity.
  Should we seperate entities   like AccountEntities for AccountRepository , UserEntities for UserRepository ?


 Best Regards..

380   99.9
Mar 01, 2012
It is recommended to use a single context but separate entities for each domain object.

76   96.3
Apr 25, 2012

Maintaining sepearate entities is the preferred choice over a entity will all data/ entity objects. There helps out by loading the specific set of data objects related to the respective repository. In your case, you can have Account Entities which deals with Account module only. Just incase a situation arrives where in you need to club both user and account entity objects, go for unit of work pattern (transaction over both repositories).

Simple words, Yes you can seperate the entities like Account Entities for Account repository and use the same data context. Load respective repositories based on the modules (Account/ User), use unit of work for persisting data on both the repositories.

Hope this helps.