Using entity framework as a DAL, howto?

Topics: Questions
Jul 29, 2014 at 9:58 AM
Edited Jul 29, 2014 at 9:59 AM
Hello,

Please bear with me, it's a long question with lots of code examples, the questions are at the bottom.

I like to use the Entity Framework (EF) as a Data Access Layer (DAL) but I'm not sure how to do it.

This is what I gather from documentation and Google.
I add a folder to the project named DAL, in which I aadd an empty project named DAL.
Using NuGet I add the latest EF (6.0.2) to this project.

Then I add an empty .edmx file and create the same entities I made in my model.
From there I can create the database.

I then have to make a proxy DbContext to map the model to the EF.

Using the example from the documentation it would look like:

using System.Data.Entity;
using Model; // Basic Model folder/project
namespace DAL
{
public class ExampleDbContext : DbContext
{
public DbSet<Person`> Persons {get; set}
public DbSet<Family> Families {get; set}
}
}

Then I've to create an UnitOfWork, that would look like:

using System;
using System.Data.Entity;
using System.Transactions;

namespace DAL
{
public class UnitOfWork : IUnitOfWork
{
public UnitOfWork (DbContext orm)
{
  this.Orm = orm;
}

# region IUnitOfWork Members
public object Orm { get; private set; }

public voi Add<T>(T entity) where T : class
{
  try
  {
    ((DbContext Orm).Set<T>.Add(entity);
  }
  catch (Exception ex)
  {
    throw new Ecpetion ("some error message");
  }
}

// Same for Update/Delete/Transaction
#endregion
}
}

Then I've to create a Repository:

using System;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace DAL
{
public class Repository : IRepository
{
public Repository(IUnitOfWork unitOfWork)
{
    UoW = unitOfWork;
}

#region IRepository Members
public T AddEntity<T>(T entity) where T: class
{
    this.UoW.Add(entity);
    return entity;
}
// Same for Update/Delete
public IUnitOfWork UoW {get; private set; }
#endregion

private DbContext GetSession()
{
   return (DbContext)UoW.Orm;
}
}
}

For the SaveChanges I can do the following

I create a RelayCommandClass like

public class RelayCommand : ICommand
{
private Action<object> _action;

public RelayCommand(Action<object> action)
{
    _action = action;
}

#region ICommand Members

public bool CanExecute(object parameter)
{
    return true;
}

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)
{
    _action(parameter);
}

#endregion
}

I add to my viewmodel:

public ICommand SaveChangesCommand { get; set; }

and in the viewmodel contructor:

SaveChangesCommand = new RelayCommand(SavePersonChanges);

And the procedure somewhere:

private void SavePersonChanges()
{
dbContext.SaveChanges();
}

Now my questions are:
Does this all make sense?

In the documentation there is an example where you create a class EntityBase that inherits ModelBase.
Do I still need to apply this to my model?

Does the SavePersonChanges procedure also go in the ViewModel?

Any pointers?

Kind regards

Jeroen