关于避免此抽象上的代码重复有什么建议吗?
因此,我正在 SQL Compact Edition 之上使用实体框架构建一个应用程序。 我不喜欢使用 Entites 作为我的业务对象的想法,所以我一直在构建一个层(我称之为 ObjectModel 层,可能不是最好的术语),它将采用我的普通对象,使用它们填充并保存实体。 反之亦然 - 获取实体,填充 POCO 以用作业务对象
假设我有一个名为 Customer 的 POCO 对象,
public class Customer:ICustomer
{
#region ICustomer Members
public System.Guid id {get;set;}
public string Forename {get;set;}
public string Surname { get; set; }
#endregion
}
我想使用我的 ObjectModel 层填充它。 我目前正在这样做...
OM_Customer<ICustomer,Entity.Customer> customerLayer = new OM_Agent<ICustomer,Entity.Customer>();
ICustomer businessObject = customerLayer.GetById(new Guid("4a75d5a5-6f5a-464f-b4b3-e7807806f1a9"));
OM_Customer 类继承自 ObjectModelBase,如下所示。
public abstract class ObjectModelBase<BllClass, EntityClass>
{
public DataStoreEntities1 db;
public EntityClass _dataObject;
public string setName;
#region POCO-ORM Mapping Functions
public EntityClass MapBLLToEntity(BllClass bllObject)
{
Mapper.CreateMap<BllClass, EntityClass>();
return Mapper.Map<BllClass, EntityClass>(bllObject);
}
public BllClass MapEntityToBLL(EntityClass entityObject)
{
Mapper.CreateMap<EntityClass, BllClass>();
return Mapper.Map<EntityClass, BllClass>(entityObject);
}
#endregion
public void Save(BllClass toAdd)
{
_dataObject = MapBLLToEntity(toAdd);
using (db = new DataStoreEntities1())
{
db.AddObject(setName, _dataObject);
db.SaveChanges();
}
}
public BllClass GetById(Guid id)
{
using (db = new DataStoreEntities1())
{
EntityClass result = (EntityClass)((object)(from c in db.CustomerSet where c.id == id select c).First());
return MapEntityToBLL(result);
}
}
}
这里的问题在于 GetById 方法。 Save 方法可以被子类很好地使用,因为所有实体框架对象上下文都有 AddObject() 方法。
但是,我希望有一个通用的 getById 方法,因为我的所有实体都有一个名为 id 的属性(也在数据库中,作为主键)。问题是这里的 linq...
ModelClass result = (ModelClass)((object)(from c in db.CustomerSet where c.id == id select c).First());
特定于 db.CustomerSet。 我希望这个抽象类为我的所有实体提供通用的 CRUD/分页/“集合检索”,但不需要重复的击键,但是,这个简单的 GetById 方法促使我寻求帮助。
当前想法:
- 让我的 ID 使用 Entity SQL,这样我可以更好地控制构建查询字符串。
- 废弃实体框架和 linq,因为完全抽象可能太耗时。
对于 SO 社区如何解决上述问题的任何建议,我们将不胜感激。
So I'm building an App using the Entity Framework on top of SQL Compact Edition. I didn't like the idea of using the Entites as my business objects so I've been building a layer (I've called it ObjectModel layer, prob not the best terminology) in-between that will take my plain objects, use them to populate and save the entities. And also vice versa - take entites, populate the POCOs for use as business object
Let's say I have a POCO object called Customer,
public class Customer:ICustomer
{
#region ICustomer Members
public System.Guid id {get;set;}
public string Forename {get;set;}
public string Surname { get; set; }
#endregion
}
And I want to populate this using my ObjectModel layer. I currently do this...
OM_Customer<ICustomer,Entity.Customer> customerLayer = new OM_Agent<ICustomer,Entity.Customer>();
ICustomer businessObject = customerLayer.GetById(new Guid("4a75d5a5-6f5a-464f-b4b3-e7807806f1a9"));
The OM_Customer class inherits from ObjectModelBase, which is below..
public abstract class ObjectModelBase<BllClass, EntityClass>
{
public DataStoreEntities1 db;
public EntityClass _dataObject;
public string setName;
#region POCO-ORM Mapping Functions
public EntityClass MapBLLToEntity(BllClass bllObject)
{
Mapper.CreateMap<BllClass, EntityClass>();
return Mapper.Map<BllClass, EntityClass>(bllObject);
}
public BllClass MapEntityToBLL(EntityClass entityObject)
{
Mapper.CreateMap<EntityClass, BllClass>();
return Mapper.Map<EntityClass, BllClass>(entityObject);
}
#endregion
public void Save(BllClass toAdd)
{
_dataObject = MapBLLToEntity(toAdd);
using (db = new DataStoreEntities1())
{
db.AddObject(setName, _dataObject);
db.SaveChanges();
}
}
public BllClass GetById(Guid id)
{
using (db = new DataStoreEntities1())
{
EntityClass result = (EntityClass)((object)(from c in db.CustomerSet where c.id == id select c).First());
return MapEntityToBLL(result);
}
}
}
The problem here lies with the GetById method. The Save method can be used by subclasses fine as all Entity Framework Object Contexts have the AddObject() method.
However, I wish to have a generic getById method, as all my entities will have a property called id (in the db as well, being the primary key).The problem is that the linq here...
ModelClass result = (ModelClass)((object)(from c in db.CustomerSet where c.id == id select c).First());
is specific to db.CustomerSet. I wish to have this abstract class provide the generic CRUD/Paging/'Collection Retrieval' for all my entities, but without the repetitive keystrokes, however, this simple GetById method has driven me to ask for help.
Current ideas :
- have my get by Id use Entity SQL where i'd have more control over building the query string.
- scrap the entity framework and linq as it may be too time consuming to abstract fully.
Would appreciate any adivce as to how the SO community would go about solving the above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我首先要问为什么您认为使用 EF 实体作为您的业务实体必然是一件坏事? 我已经在许多项目中这样做了,但还没有遇到任何我无法克服的严重问题。
话虽如此,如果您确实觉得有充分的理由将两者分开,那么这是一个选择。
您可以使用委托来注入要查询的特定实体集对象,并使用另一个委托来注入键的选择器 - 它可以允许派生程序仅指定其存储语义。 所以你的代码看起来像:
First, I would start by questioning why you feel that using EF entities as you business entities would necessarily be a bad thing? I've done so on a number of projects and I have yet to run into any serious issues that I can't overcome.
Having said that, if you really feel you have a strong reason to separate the two, here's one option.
You can use a delegate to inject the specific entity set object to query and another for the selector for the key - it could allows derivatives to only specify their storage semantics. So your code would look like: