ADO.NET EF 作为 DAL
我正在开发一个使用分层架构的项目。我有一个 DAL,其中使用实体框架、一个使用 DAL 返回的对象的业务逻辑层和一个应用程序层。
我不确定我的想法是否正确,所以我只想问你的想法。
我的 DAL 基于映射器。我有 BLL 用于操作我的数据的类型(映射器)。这些映射器返回 DTO,因为我不想向 BLL 公开任何 EF 对象,因此它们的实现不依赖于 EF 来工作。
所有这些映射器所做的都是对单个“表”进行 CRUD 操作,例如:
using (var contex = new EFEntities()){
var obj = (from x in context.Table where x.ID == param select x).SingleOrDefault;
return Map(x.ToList());
}
Map 方法将 EF 对象映射到 DTO,该 DTO 仅具有一些属性来映射我想要公开的值。
有更优雅的方法吗?我只是使用 EF 来方便访问我的数据库 - 我不必编写任何 ADO.NET 代码。
欢迎对此提出任何意见。
谢谢。
I'm developing a project using a layered architecture. I have a DAL in which i'm using Entity Framework, a business logic layer which consumes the objects returned by the DAL and an app layer.
I'm not entirely sure i'm thinking this right, so i'll just ask you what you think.
My DAL is based on mappers. I have types - mappers - that the BLL uses to operate on my data. These mappers return DTO's, because i did not want to expose to my BLL any EF objects so their implementations are not dependent on EF to work.
All these mappers do are CRUD actions on a single 'table', like:
using (var contex = new EFEntities()){
var obj = (from x in context.Table where x.ID == param select x).SingleOrDefault;
return Map(x.ToList());
}
The Map method maps the EF object to a DTO, which has only some properties to map the values i want to expose.
Is there a more elegant approach to this? I am just using EF to facilitate the access to my database - i don't have to write any ADO.NET code.
Any input on this would be welcome.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种歪曲,因为您将完整的对象层放入 DAL 中。 EF 不仅仅是 DAL。
您可能想要查找的术语是“贫血对象模型”,这不是一个好术语。
别跟EF斗了如果你不喜欢它,就不要使用它——周围有很多更好的框架。
That is a perversion, given that you are putting a complete object layer into your DAL. EF is a lot more than a DAL.
The term you may want to look up for that is "anemic object model", and it is not a nice term.
Don't fight EF. if you dont like it, don't use it - there are a LOT better frameworks around.