Historically, Microsoft has always focused on the “Database first” model, where you build a database and then from this database create a data layer… With Entity Framework 4, we now can do what is referred to as “Model first” design. The scenario I am really interested in though is “Code first”! Since the release of EF4, Microsoft have been pushing out some feature CTPs. These are previews of what they have planned for the future. The interesting thing here is the new “Code first” support, where we can create POCOs (Plain old CLR objects) and then use EF’s new DbContext
/DbSet
to persist them! This is a very transparent convention over configuration based method of persisting data:
“Convention over Configuration (also known as Coding by convention) is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility.”
First things first, I need to create a POCO object for each of my model classes (Like products, customers, etc.), Here is a sample:
public class Product
{
public Guid Id { get; set; }
public string Reference { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public double BuyPrice { get; set; }
public double SellPrice { get; set; }
public Guid CategoryId { get; set; }
public Guid TaxId { get; set; }
}
Nice and clean, huh?
Next, I need to create a code first session/repository:
public class EFSession : ISession
{
DbContext _context;
public EFSession(DbContext context)
{
_context = context;
}
public void CommitChanges()
{
_context.SaveChanges();
}
public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
where T : class
{
foreach (var item in _context.Set<T>().Where(expression))
Delete<T>(item);
}
public void Delete<T>(T item) where T : class
{
_context.Set<T>().Remove(item);
}
public void DeleteAll<T>() where T : class
{
foreach (var item in _context.Set<T>().AsQueryable())
Delete<T>(item);
}
public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
where T : class
{
return _context.Set<T>().SingleOrDefault(expression);
}
public IQueryable<T> All<T>() where T : class
{
return _context.Set<T>().AsQueryable();
}
public void Add<T>(T item) where T : class
{
_context.Set<T>().Add(item);
}
public void Add<T>(IEnumerable<T> items) where T : class
{
foreach (var item in items)
Add<T>(item);
}
public void Update<T>(T item) where T : class
{
}
public void Dispose()
{
_context.Dispose();
}
}
And here is my new EF Features CTP friendly DbContext
:
public class OpenPOSContext : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<ClosedCash> ClosedCash { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Payment> Payments { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Stock> Stock { get; set; }
public DbSet<StockAction> StockActions { get; set; }
public DbSet<Store> Stores { get; set; }
public DbSet<Tax> Taxes { get; set; }
public DbSet<Terminal> Terminals { get; set; }
public DbSet<Ticket> Tickets { get; set; }
public DbSet<TicketLine> TicketLines { get; set; }
public DbSet<User> Users { get; set; }
}
This is the only EF-focused part of the code!
The models are nice and clean and they do not need to derive from any special class… I can now also add data annotations directly into my class (I do not need the UGLY buddy classes).
The source is available on Codeplex.
Also read ScottGu’s AWESOME article (Code-First Development with Entity Framework 4).
CodeProject