如何创建实体框架三层解决方案 [英] How to create three tier solution for Entity framework

查看:123
本文介绍了如何创建实体框架三层解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个解决方案三个项目,一是网络applciation,为DAL和BLL二级库。创造了DAL层的实体框架模型,并在BLL项目中引用的DAL库。

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.

当我打电话从Web应用程序项目BLL对象我遇到了问题,它说我需要引用实体框架。我不想在Web应用程序项目在DAL库对象的任何相关性。

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.

有建设使用Entity Framework的清洁三层应用程序了任何具体的指导。

Is there any specific guidance on building clean three-tier applicaiton using Entity Framework.

推荐答案

听起来像你BLL被揭实体您在DAL添加类。你需要创建在BLL包装类(这是POCO),并从DAL返回这些,而不是实体。

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);

这可能是你的做:

// 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);

这是对于使用.NET 3.5SP1和LINQ到SQL这两者我都用过了一下,可能为EF的最新versons真正附带的实体框架正确的,但有code -first和其他东西有可能避免这种额外数据传输-对象工序的方式,尽管使用面向服务的体系结构,DTO的可能最好的方式。

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.

这篇关于如何创建实体框架三层解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆