I created three projects in a solution, one web applciation, two class libraries for DAL and BLL. Created the Entity Framework model in the DAL layer and referenced the DAL library in the BLL project.
When I call the BLL objects from the web application project am running into problems, it says I need to reference entity framework. I dont want any dependency on DAL library objects in the web application project.
Is there any specific guidance on building clean three-tier applicaiton using Entity Framework.
The three-tier architecture is the most popular implementation of a multi-tier architecture and consists of a single presentation tier, logic tier, and data tier.
This can be a relational database management system such as PostgreSQL, MySQL, MariaDB, Oracle, DB2, Informix or Microsoft SQL Server, or in a NoSQL Database server such as Cassandra, CouchDB or MongoDB. In a three-tier application, all communication goes through the application tier.
Sounds like your BLL is exposing the entity
classes you added in the DAL. You'll need to create wrapper classes (That are POCO) in the BLL and return those instead of the entities from the DAL.
This is probably what you are doing:
// 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 be doing:
// 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 shipped with .NET 3.5SP1 and for Linq-To-SQL both of which I have used a bit, it may hold true for the latest versons of EF, but with Code-First and other things there may be a way to avoid this extra Data-Transfer-Object step, though with a Service Orientated Architecture, DTOs are likely the best way to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With