开发者

Multi layered architecture using Entity Framework 4 and Repository Pattern

开发者 https://www.devze.com 2023-04-13 02:20 出处:网络
I have to build the architecture of a web application using Entity Framework 4.1 and ASP.NET. I already have the database structure, so I have to use the database-fist. I have read lots of articles an

I have to build the architecture of a web application using Entity Framework 4.1 and ASP.NET. I already have the database structure, so I have to use the database-fist. I have read lots of articles and threads here, but it seems like I'm missing something. I have decided to organize the proje开发者_StackOverflowcts in the following way:

I have architecuted a web app with Linq2Sql. I used this project. It provides a T4 template that generates a specific static repository class for each entity. I like this approach as it is easy to add additional logic to any repository, such as GetUserByName(). I like this approach but I cannot find a similar approach for EF4 so far. I have found only generic repositories and then I have to manually create the concrete repositories. What I don't like in this case is first that I have the application that I'm working on has a bit complex business logic, so I will have to manually create the concrete repositories for almost each entity. Second, if at first I use the generic repository to get all entities, and later I need to use, for example GetUserByName(), then there will be inconsistency in the code. I would prefer all data retreiving to be done the same way.

I am either missing something in the architecte structure.

  • Generic: Generic Repository if it is neccessary to be seperated

  • DAL: The EDMX (Entity Model) file with the context class and repositories

  • BLL: The business logic of the system

    -- Entities

    -- Services

    -- etc

  • UI: ASP.NET pages

Questions:

  1. Is the separation of logic correct?

  2. Should I use specific repositories?

  3. What implementation of repository pattern would you recommend for the best project organization and easiness to use?

  4. Is it better to use static repository?

Thanks


Don't use static classes as repository. It is awful and far away for correct object oriented design. It also completely blocks any possibility of inversion of control and dependency injection.

If you want to use repository pattern you must use specific repository. Generic repository is just a wrapper around EF related classes (where ObjectSet / DbSet is already EF dependent repository). You should also build your repository on top of aggregate roots not on top of every entity.


My Preferred setup is:

  • Project.Presentation (UI)
  • Project.Application (Application layer which expose DTOs to consumer/UI and execute commands to the domain layer)
  • Project.Domain
    • Entities [with domain logic] (Order, Customer, ...)
    • Domain Services (TransferService, CreditCardService...)
    • Repository Interfaces (IOrderRepository, ICustomerRepository, ...)
  • Project.Repositories.EF (Data access using specific technology implementing repository interfaces (OrderRepository, CustomerRepository)
  • Project.Infrastructure (Cross cutting)
0

精彩评论

暂无评论...
验证码 换一张
取 消