Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework in layered architecture

I am using a layered architecture with the Entity Framework. Here's What I came up with till now (All the projects Except UI are class library):

  • Entities: The POCO Entities. Completely persistence ignorant. No Reference to other projects. Generated by Microsoft's ADO.Net POCO Entity Generator.

  • DAL: The EDMX (Entity Model) file with the context class. (t4 generated). References: Entities

  • BLL: Business Logic Layer. Will implement repository pattern on this layer. References: Entities, DAL. This is where the objectcontext gets populated: var ctx=new DAL.MyDBEntities();

  • UI: The presentation layer: ASP.NET website. References: Entities, BLL + a connection string entry to entities in the config file (question #2).

Now my three questions:

  1. Is my layer discintion approach correct?
  2. In my UI, I access BLL as follows:
    var customerRep = new BLL.CustomerRepository();
    var Customer = customerRep.GetByID(myCustomerID);

    The problem is that I have to define the entities connection string in my UI's web.config/app.config otherwise I get a runtime exception. IS defining the entities connectionstring in UI spoils the layers' distinction? Or is it accesptible in a muli layered architecture.

  3. Should I take any additional steps to perform chage tracking, lazy loading, etc (by etc I mean the features that Entity Framework covers in a conventional, 1 project, non POCO code generation)?

Thanks and apologies for the lengthy question.

like image 212
Kamyar Avatar asked Jan 06 '11 04:01

Kamyar


People also ask

What is Entity Framework architecture?

The Entity Framework uses information in the model and mapping files to translate object queries against entity types represented in the conceptual model into data source-specific queries. Query results are materialized into objects that the Entity Framework manages.

What are layers in Entity Framework?

Entity Client shows the entity framework layers, which are the core functionality. These layers are called as Entity Data Model. The Storage Layer contains the entire database schema in XML format. The Entity Layer which is also an XML file defines the entities and relationships.

What are the three types of Entity Framework?

There are three approaches to model your entities in Entity Framework: Code First, Model First, and Database First. This article discusses all these three approaches and their pros and cons.

What are the 4 layers of architecture?

Although the layered architecture pattern does not specify the number and types of layers that must exist in the pattern, most layered architectures consist of four standard layers: presentation, business, persistence, and database (Figure 1-1).


2 Answers

BLL: Business Logic Layer. Will implement repository pattern on this layer

I don't really agree with this. Repository is meant to abstract the underlying data store (SQL Server, XML, etc). It's a data layer concern, not a business one - therefore why should it be in the BLL?

Is my layer discintion approach correct?

Kind of. :) It's a bit subjective, but generally you have:

  • Data
    • Repository goes here.
  • Business
    • Business rules, domain logic, and entities.
  • Presentation
    • UI/web application.

Now, usually those three are broken up further. So in your case I would have:

  1. MyCompany.MyProject.Data (Repository)
  2. MyCompany.MyProject.Business.Services (calls repository, applies business rules, etc)
  3. MyCompany.MyProject.Business.DomainModel (Entities)
  4. MyCompany.MyProject.UI (Web app)

Should I take any additional steps to perform chage tracking, lazy loading, etc (by etc I mean the features that Entity Framework covers in a conventional, 1 project, non POCO code generation)?

If you're not using POCOs (e.g your using default code generation). Then you don't need to worry about change tracking.

As for lazy loading - that is a decision you need to make. I personally disable lazy loading, because I don't want lazy developers returning a bunch of records when they didn't ask for it.

Instead, force the calling code (e.g the business/service) to eager load what it needs.

If your using a ASP.NET MVC application, if you have lazy loading on, your View could end up calling the database at render time, breaking the MVC pattern.

like image 147
RPM1984 Avatar answered Sep 19 '22 20:09

RPM1984


1) Layers seem fine to me

2) Dont see a problem with the connection string being in your UI app.config. Has to be defined someplace. You could copy your DAL.config to your application BIN folder that likely had the connection string created in it when you created the project but that to me would not seem right. I would personally manage it in your UI layer app.config

like image 45
Jim Scott Avatar answered Sep 17 '22 20:09

Jim Scott