views:

134

answers:

1

Hi, 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.

+2  A: 

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)); }
    }
}
LBushkin
Thanks very much. I feel the need to seperate the two for two reasons. a) to facilitate testability b) so change data access solutions in the future should I please, without having to touch any of my business logic classes.Cheers again for taking the time to help.