How to create a three-tier solution for the Entity platform

I created three projects in the solution, one web application, two class libraries for DAL and BLL. Created an Entity Framework model in the DAL layer and specified the DAL library in the BLL project.

When I call BLL objects from a web application project running in problems, it says that I need to reference the entity structure. I do not need a dependency on DAL library objects in a web application project.

Are there any specific recommendations for creating a clean three-tier application using the Entity Framework.

+7
source share
1 answer

It looks like your BLL exposes the entity classes you added in the DAL. You will need to create wrapper classes (this is POCO) in the BLL and return them instead of objects from the DAL.

It is likely that you :

 // DAL // .edmx file generated entities public IQueryable<TableEntity> GetTableEntities() { // read from entity framework and return } // BLL public IEnumerable<TableEntity> ReadTableEntitiesForUser(int userID); { var d = new DAL(); var entities = d.GetTableEntities(); // restrict to entites this user "owns" entities = entities.Where(e => e.OwnerID.Equals(userID)); return entities; } // WebApp var b = new BLL(); var myEntities = b.ReadTableEntitiesForUser(1234); 

This is probably what you should do:

 // DAL // .edmx file generated entities public IQueryable<TableEntity> GetTableEntities() { // read from entity framework and return } // BLL public class TableEntityDTO { public int ID { get; set; } public string Name { get; set; } // continue on for each column in the table // and make a DTO class for each table in your database } public IEnumerable<TableEntityDTO> ReadTableEntitiesForUser(int userID); { var d = new DAL(); var entities = d.GetTableEntities(); // restrict to entites this user "owns" entities = entities.Where(e => e.OwnerID.Equals(userID)); // convert from "Entity Framework Object" to "BLL Object" foreach(var e in entities) { yeild return new TableEntityDTO() { ID = e.ID, Name = e.Name }; } } // WebApp var b = new BLL(); var myEntities = b.ReadTableEntitiesForUser(1234); 

This is true for the Entity Framework that ships with .NET 3.5SP1 and for Linq-To-SQL, which I used a bit, it may be true for the latest versions of EF, but with Code-First and other things can be a way to avoid this extra step object-to-data transfers, although with a service-oriented architecture, the DTO is probably the best way to go.

+17
source

All Articles