0

Learning to implement the entity framework. I have a generic repository. I would like to implement a business layer. I know how to do the coding for this, but am a little unsure where to do this.

  1. Do I make a business layer class for each entity?

For example, the database has providers, agencies and contacts. I have one generic repository. Should I create a ProviderManager, an AgencyManager and a ContactManager?

  1. Do I need an explicit repository for each entity in addition to the business layer?

For example, do I need an explicit ProviderRepository?

Ethan Schofer
  • 1,544
  • 3
  • 19
  • 51
  • What about concentrating on the functionality you must implement instead of concentrating on patterns you don't understand? We cannot answer your question because we don't know what you must achieve and how complex are your business requirements. – Ladislav Mrnka Jul 03 '12 at 08:28
  • Thats sort of my problem. Its a pretty simple project, and so I thoight it would be a good project to learn to use the entity framework. So I worked through Microsoft's EF tutorials, which only has one repository. So my original thought was that I would just have a separate repository for each entity, but that seems kind of silly. The functionality I need is basic CRUD operations, plus a few extras. – Ethan Schofer Jul 03 '12 at 12:13
  • So why not to use pure EF without any repositories and simply learn EF first before you go for additional complexities? [EF itself already implements repository](http://stackoverflow.com/questions/5625746/generic-repository-with-ef-4-1-what-is-the-point/5626884#5626884). I think the learning of EF should be enough for a single project. – Ladislav Mrnka Jul 03 '12 at 12:20

2 Answers2

0

you can easily generate repositories using nuget package T4Scaffolding for EF model classes. you can start new class project and implement this repositories as per the below given link.then reference it in your business class .this repositories include basic CRUD operations for each model classes. for more details http://thedatafarm.com/data-access/using-t4scaffolding-to-create-dbcontext-and-repository-from-domain-classes/

0

Do I make a business layer class for each entity?

No, that's not really necessary. Business may use multiple repository entities in order to perform a business operation. e.g. CheckOutOrder class may include payment, shipping, inventory etc. type repository operation. It's business layer and it should be unaware of how your data is being represented by persistent storage.

Addressing your second question, Repository pattern is an abstraction layer you put on your data access layer (e.g. ORM like ADO.NET/EF/NHibernate) IMO, If you really want to use repository pattern, keep it in mind that you're introducing an extra layer.

Your repository layer could be like:

IRespository<T>
RespositoryBase<T> : IRepository<T>

Further if you want to keep it generic then that's ok too. But make sure your generic function remains in the base class. which you would want to do to make reuse of generic code in all repository. Now you can create a dedicated class for each entity.

ProviderRepository : RepositoryBase<ProviderEntity>

you can also introduce abstraction for each type of repository to represent entity specific methods.

ProviderRepository : RepositoryBase<ProviderEntity>

ProviderRepository : RepositoryBase<ProviderEntity>, IProviderRepository

The actual implementation may differ but this is a demonstration just to give the idea to give you a head start. You will find enormous implementation of Repository pattern on internet. But I would suggest understand it first and keep it simple.

vendettamit
  • 12,898
  • 2
  • 25
  • 51