I am new to Entity framework and relatively new to C# as well. I am using Entity Framework with the Repository Pattern. I have a DAL project, a business layer project and a web project which has the aspx pages along with the ObjectDataSource. Right now I have been creating seperate Repositories for all my entities, I would like to use a Generic Repository to handle all the basic CRUD functions. I can create the Generic entity DAL class for all entities like below in the code sample and inherit that in a generic Repository but with an object data Source how do I
1) map the ObjectDataSource's TypeName to a generic Type? assign the TypeName and
ObjectDataTypeName ? The Business Layer generic class inherits the generic IDALEntity class.
<asp:ObjectDataSource ID="ODSCustomers" runat="server"
TypeName="SampleProject.BLL. " how do i access the Customer instance of BL
DataObjectTypeName="SampleProject.DAL. " how do i access the instance of
Customer entity from the generic DAL
class?
SelectMethod="GetCustomers" >
<SelectParameters>
<asp:SessionParameter Name="client_id" SessionField="ClientID" />
</SelectParameters>
2) How are related entities or Navigation properties handled this way? If I want to display columns from multiple entities for e.g Customer entity and Customer.CustomerAddress Entity as well would I bind the grid columns like DataFied="Customer.CustomerAddress.City" ?
public class DALEntityRepository<T> : IDisposable, IDALEntityRepository<T> where T : class
{
private PFOEntities _context;
private ObjectSet<T> _objectSet;
public DALEntityRepository()
{
_context = new Entities(ConnectionStringHelper.GetConnectionString());
_objectSet = (ObjectSet<T>)GetObjectSet();
}
public void Insert(T entity)
{
_context.AddObject(_objectSet.EntitySet.Name, entity);
_context.SaveChanges();
}
public void Update(T newVersion, T origVersion)
{
_objectSet.Attach(origVersion);
_context.ApplyCurrentValues(_objectSet.EntitySet.Name, newVersion);
_context.SaveChanges();
}
public void Delete(T entity)
{
_context.AttachTo(_objectSet.EntitySet.Name, entity);
_objectSet.DeleteObject(entity);
_context.SaveChanges();
}
public IQueryable<T> GetEntities()
{
return _objectSet;
}
public IQueryable<T> GetEntitiesByClientId(int clientId)
{
Expression<Func<T, bool>> predicate = (Expression<Func<T, bool>>)GetPredicate(clientId);
return GetEntities().Where(predicate);
}
private object GetPredicate(int clientId)
{
object retVal = null;
Type type = GetType();
//Use similar if's to check for Different Entities
if (type == typeof(DataEntityRepository<Customers>))
{
Expression<Func<Customers, bool>> predicate = (c) => c.client_id ==
clientId;
retVal = predicate;
}
return retVal;
}
private object GetObjectSet()
{
object retVal = null;
Type type = GetType();
if(type == typeof(DataEntityRepository<Customers>))
{
retVal = _context.Customers;
}
return retVal;
}
Your helps appreciated please let me know if I havnt explained clearly or if you have any questions thanks.