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.