I have the following class which is creating data access functions for Customers from my DB:
public static Customer Get(int ID)
{
KezberPMDBDataContext db = new KezberPMDBDataContext();
return (from p in db.Customers
where p.CustomerID == ID
select p).FirstOrDefault();
}
public static bool Remove(int ID)
{
Customer c = Get(ID);
if (c != null)
{
KezberPMDBDataContext db = new KezberPMDBDataContext();
db.Customers.DeleteOnSubmit(c);
db.SubmitChanges();
return true;
}
return false;
}
I will be creating more classes, ex, Employees, that will need exactly the same functionality, just with the Employee class.
Is there some way I could avoid code duplication and use templates / generics in some way?
Thanks
EntityFramework 4.3 and 5’s
DbSetclass has aFindmethod that does exactly what yourGetmethod does, and aRemovemethod that does sort of what yourRemovedoes (it accepts an object and not an id).