我正在研究 Entity Framework 以开始使用它。下面是我安排我的项目与 EF 交互的方式。我只想知道我是否正朝着正确的方向前进。请建议更好的方法。请忽略架构之类的东西,比如 UI 不应该引用 DAL。这只是“了解”项目。 :)
这就是我正在做的:
DAL 的一个项目。这里所有的数据库操作都将使用 EF 进行。
模型 - 业务实体类。我不会返回 EF 的 Order 对象,而是返回模型的实体类。 (有没有办法将我的业务实体映射到 EF 实体?)
UI - 这将调用 DAL 并完成任务。
项目 1 - DAL
这个项目还包含我的 edmx 文件。
public class DALRepository
{
OrderEntities orderEntitiesContext = new OrderEntities();
public IQueryable<Model.OrderModel> GetOrders ()
{
return orderEntitiesContext.Orders.Select(o => new Model.OrderModel { OrderName = o.OrderName, Id = o.OrderID });
}
}
项目 2 - 模型
public class OrderModel
{
public long Id { get; set; }
public string OrderName { get; set; }
}
项目 3 - 用户界面
public partial class OrderList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DAL.DALRepository dal = new DAL.DALRepository();
GridView1.DataSource = dal.GetOrders();
GridView1.DataBind();
}
}
最佳答案
考虑添加一个具有 DAL 外部所需 API 的接口(interface)项目,以在没有 DAL 的情况下启用单元测试,并添加一个使用 DAL 项目实现这些接口(interface)的实现项目。
关于c# - 这是与 Entity Framework 交互的正确方式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6390181/