Click here to Skip to main content
Click here to Skip to main content

Tagged as

Building OpenPOS: Part 10 – Code First

, 23 Aug 2010
Rate this:
Please Sign up or sign in to vote.
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

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 thou is “Code first”! Since the release of EF4, Microsoft have been pushing out some feature CTPs. These are previews of what thy 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
    {
        //nothing needed?
    }

    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 thy 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)

Building OpenPOS- Part 1 - Introduction
Building OpenPOS- Part 2 - Rapid prototype using SketchFlow
Building OpenPOS- Part 3 – Scaffolding and Navigation
Building OpenPOS- Part 4 – SalesModule (Part 1)
Building OpenPOS- Part 5 – SalesModule (Part 2)
Building OpenPOS- Part 6 – Make it look pretty…
Building OpenPOS- Part 7 – NoSQL, MSSQL, SQL CE-
Building OpenPOS- Part 8 - Windows 7 love
Building OpenPOS- Part 9 - Settings and Notifications

Source is available on Codeplex

Also read ScottGu’s AWESOME article (Code-First Development with Entity Framework 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

rudigrobler

South Africa South Africa
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 23 Aug 2010
Article Copyright 2010 by rudigrobler
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid