Any tips on preventing code duplication in this abstraction?

So, I am building an application using the Entity Framework on top of SQL Compact Edition. I did not like the idea of ​​using Entites as my business objects, so I created a layer (I called it the ObjectModel layer, and not the best terminology) between them, which will use my simple objects, use them to fill and save objects. And vice versa - to participate, fill out POCOs for use as a business object


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 am currently doing 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 lower ..

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 , Entity Framework AddObject().

, getById, id ( db, ). , linq ...

ModelClass result = (ModelClass)((object)(from c in  db.CustomerSet  where c.id == id select c).First());

db.CustomerSet. , CRUD/Paging/'Collection Retrieval , GetById .

:

  • get by Id, Entity SQL, .
  • linq, .

, SO .

+3
1

-, , , 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)); }
    }
}
+4

Source: https://habr.com/ru/post/1711951/


All Articles