关于避免此抽象上的代码重复有什么建议吗?

发布于 2024-07-26 01:44:45 字数 2585 浏览 4 评论 0原文

因此,我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画▽骨i 2024-08-02 01:44:45

首先,我首先要问为什么您认为使用 EF 实体作为您的业务实体必然是一件坏事? 我已经在许多项目中这样做了,但还没有遇到任何我无法克服的严重问题。

话虽如此,如果您确实觉得有充分的理由将两者分开,那么这是一个选择。

您可以使用委托来注入要查询的特定实体集对象,并使用另一个委托来注入键的选择器 - 它可以允许派生程序仅指定其存储语义。 所以你的代码看起来像:

public abstract class ObjectModelBase<BllClass, EntityClass>
{
    // ... same basic code here...

    // supplied by your derivatives...
    protected abstract Func<IQueryable<EntityClass>> GetEntitySet { get; };
    protected abstract Func<Guid,Func<EntityClass,bool>> KeySelector { get; }

    public BllClass GetById(Guid id)
    {
        using (db = new DataStoreEntities1())
        {
            EntityClass result = (EntityClass)((object)
                 GetEntitySet()
                     .Where( KeySelector( id ) )
                     .First();
            return MapEntityToBLL(result);
        }
    }
}

public class OM_Customer : ObjectModelBase<ICustomer,Entity.Customer>
{
    protected abstract Func<IQueryable<Entity.Customer>> GetEntitySet
    { 
        get { return db.CustomerSet; }
    }

    protected abstract Func<Guid,Func<Entity.Customer,bool>> KeySelector
    {
        get { return (g => (e => e.Id == g)); }
    }
}

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:

public abstract class ObjectModelBase<BllClass, EntityClass>
{
    // ... same basic code here...

    // supplied by your derivatives...
    protected abstract Func<IQueryable<EntityClass>> GetEntitySet { get; };
    protected abstract Func<Guid,Func<EntityClass,bool>> KeySelector { get; }

    public BllClass GetById(Guid id)
    {
        using (db = new DataStoreEntities1())
        {
            EntityClass result = (EntityClass)((object)
                 GetEntitySet()
                     .Where( KeySelector( id ) )
                     .First();
            return MapEntityToBLL(result);
        }
    }
}

public class OM_Customer : ObjectModelBase<ICustomer,Entity.Customer>
{
    protected abstract Func<IQueryable<Entity.Customer>> GetEntitySet
    { 
        get { return db.CustomerSet; }
    }

    protected abstract Func<Guid,Func<Entity.Customer,bool>> KeySelector
    {
        get { return (g => (e => e.Id == g)); }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文