Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF ObjectContext, Service and Repository - Managing context lifetime.

I am fairly new to the MVP and the Entity Framework world so bear with me.

I currently have a View + Presenter combination, the view has two events Edit and Delete and the presenter just listens for these events. I also have a service object and repositories set up. The service layer takes some repository implementations which take an ObjectContext so the order of construction is (passing in the top object to the one below it:

ObjectContext
   |
   V
Repositries
   |
   V
Service Object
   |
   V
Presenter

Now the problem is that when I create object context at the top, It's alive for the whole time the presenter is alive meaning that Edit and Delete use the same context instance from the service.

So calling ServiceObject.Delete and ServiceObject.Edit use the same context which makes it hard to manage change tracking. From what I understand the context should really only be short lived and only for a unit of work, to me Edit and Delete are both different lots of work.

How do you do DI with the entity framework and still manage context life time?

I have seen people just new up the object context in side the repository, is this a good pattern.

Or should I do that in the service object, something like:

ServiceObject{
  public void Edit(// some args) {
     Using(var context = new MyObjectContext) {
         var repo = new MyRepo(context);
         var entity = repo.GetForID(12);
         // Do some stuff for edit
         context.SaveChanges();
     }
  }
}

But If I do it like that I am no longer passing my repository into the constructor of ServiceObject and not doing DI :(.

What can I do in this situation?

Does anyone know any open source projects that I can look at that may help me with this problem.

Thanks.

like image 386
Nathan W Avatar asked Jul 23 '09 01:07

Nathan W


1 Answers

I will go from top (presenter) and describe the relations between participants.

Presenter gets service object through dependency. Service functionality is outlined using its contract:

class Presenter 
{
  public Presenter(IService service)
  {
     ... 
  }
}

Service implementation is abstracted from particular data access layer implementation. Basically whenever service performs some action that requires data source interaction it creates an instance of unit of work and disposes it when it finished.

interface IService
{
  void Do();
}

class Service : IService
{
  private readonly IUnitOfWorkFactory unitOfWorkFactory;
  public Service(IUnitOfWorkFactory unitOfWorkFactory)
  {
    this.unitOfWorkFactory = unitOfWorkFactory;
  }

  public void Do()
  {
    // Whenever we need to perform some data manipulation we create and later dispose
    // dispose unit of work abstraction. It is created through a factory to avoid 
    // dependency on particular implementation.
    using(IUnitOfWork unitOfWork = this.unitOfWorkFactory.Create())
    {
       // Unit of work holds Entity Framework ObjectContext and thus it used 
       // create repositories and propagate them this ObjectContext to work with
       IRepository repository = unitOfWork.Create<IRepository>();
       repository.DoSomethingInDataSource();

       // When we are done changes must be commited which basically means committing
       // changes of the underlying object context.
       unitOfWork.Commit();
    }
  }
}


/// <summary>
/// Represents factory of <see cref="IUnitOfWork"/> implementations.
/// </summary>
public interface IUnitOfWorkFactory
{
    /// <summary>
    /// Creates <see cref="IUnitOfWork"/> implementation instance.
    /// </summary>
    /// <returns>Created <see cref="IUnitOfWork"/> instance.</returns>
    IUnitOfWork Create();
}

/// <summary>
/// Maintains a list of objects affected by a business transaction and coordinates the writing out of 
/// changes and the resolution of concurrency problems.
/// </summary>
public interface IUnitOfWork : IDisposable
{
    /// <summary>
    /// Creates and initializes repository of the specified type.
    /// </summary>
    /// <typeparam name="TRepository">Type of repository to create.</typeparam>
    /// <returns>Created instance of the repository.</returns>
    /// <remarks>
    /// Created repositories must not be cached for future use because once this 
    /// <see cref="IUnitOfWork"/> is disposed they won't be able to work properly.
    /// </remarks>
    TRepository Create<TRepository>();

    /// <summary>
    /// Commits changes made to this <see cref="IUnitOfWork"/>.
    /// </summary>
    void Commit();
}

/// <summary>
/// Represents factory of <see cref="UnitOfWork"/>s. 
/// </summary>
public class UnitOfWorkFactory : IUnitOfWorkFactory
{
    private readonly IUnityContainer container;

    /// <summary>
    /// Initializes a new instance of the <see cref="UnitOfWorkFactory"/> class.
    /// </summary>
    /// <param name="container">
    /// Dependency injection container instance used to manage creation of repositories 
    /// and entity translators.
    /// </param>
    public UnitOfWorkFactory(IUnityContainer container)
    {
                 this.conainer = container;
    }


    /// <summary>
    /// Creates <see cref="IUnitOfWork"/> implementation instance.
    /// </summary>
    /// <returns>Created <see cref="IUnitOfWork"/> instance.</returns>
    public IUnitOfWork Create()
    {
        var unitOfWork = this.container.Resolve<UnitOfWork>();
        unitOfWork.SetupObjectContext();
        return unitOfWork;
    }

     ... other members elidged for clarity
}

The implementation of IUnitOfWork receives instance of IUnityContainer and then creates child container and registers ObjectContext instance there. This child container will be used to create repositories and propagate ObjectContext.

Here is a simplified implementation of IUnitOfWork:

class UnitOfWork : IUnitOfWork
{
  private readonly IUnityContainer container;
  private ObjectContext objectContext;

  public UnitOfWork (IUnityContainer container)
  {
    this.container = container.CreateChildContainer();
  }

  public void SetupObjectContext()
  {
    this.objectContext = ... // Create object context here
    this.container.RegisterInstance(context.GetType(), context);
  }

  public void Create<TRepository>()
  {
    // As long as we registered created object context instance in child container
    // it will be available now to repositories during resolve
    return this.container.Resolve<TRepository>();
  }

  public void Commit()
  {
     this.objectContext.SaveChanges();
  }
}

class Repository : IRepository
{
  private readonly SomeObjectContext objectContext;

  public Repository(SomeObjectContext objectContext)
  {
    this.objectContext = objectContext;
  }

  public void DoSomethingInDataSource()
  {
    // You can use object context instance here to do the work
  }
}
like image 126
Dzmitry Huba Avatar answered Nov 10 '22 07:11

Dzmitry Huba