UnitOfWork与Unity和实体框架

时间:2012-06-07 06:42:31

标签: c# .net entity-framework dependency-injection unity-container

您好我正在使用Unity管理我的服务层,而服务层又与管理所有存储库的UnitOfWork对话。

我的一些服务调用其他服务,我的问题是如何在服务层之间传递相同的UnitOfWork?

在我的情况下,所有控制器操作都是从定时器上的每个按钮操作或事件的GUI启动的,这就是为什么我有一个工厂按需创建UnitOfWork,但它导致问题,因为我不知道如何通过这个服务之间的单位工作。

特别困难的是知道如何将这个特定的UnitOfWork实例注入到服务构造函数中。请注意,某些服务可能会长时间运行(在后台线程上大约10分钟左右),我不知道这是否会对设计产生任何影响。

目前,从其他服务调用的服务正在创建自己的UnitOfWork,这会导致事务设计和实体框架实体跟踪问题。

建议非常欢迎!

class OtherService : IOtherService
{
    public OtherService(IUnitOfWorkFactory unitOfworkFactory, 
        ISettingsService settingsService)
    {
        UnitOfWorkFactory = unitOfworkFactory;
        SettingsService = settingsService;
    }
    IUnitOfWorkFactory UnitOfWorkFactory;
    ISettingsService SettingsService;

    function SomeSeviceCall()
    {
        // Perhaps one way is to use a factory to instantiate a 
        // SettingService, and pass in the UnitOfWork here?
        // Ideally it would be nice for Unity to handle all of 
        // the details regardless of a service being called from
        // another service or called directly from a controller
        // ISettingsService settingsService = 
        //     UnityContainer.Resolve<ISettingService>();

        using (var uow = UnitOfWorkFactory.CreateUnitOfWork())
        {
            var companies = uow.CompaniesRepository.GetAll();
            foreach(Company company in companies)
            {
                settingsService.SaveSettings(company, "value");
                company.Processed = DateTime.UtcNow();
            }
            uow.Save();
        }
    }
}

class SettingsService : ISettingsService
{
    public SettingsService(IUnitOfWorkFactory unitOfworkFactory)
    {
        UnitOfWorkFactory = unitOfworkFactory;
    }
    IUnitOfWorkFactory UnitOfWorkFactory;

    // ISettingsService.SaveSettings code in another module...
    function void ISettingsService.SaveSettings(Company company, 
        string value)
    {
        // this is causing an issue as it essentially creates a 
        // sub-transaction with the new UnitOfWork creating a new 
        // Entiy Framework context
        using (var uow = UnitOfWorkFactory.CreateUnitOfWork())
        {
            Setting setting = new Setting();
            setting.CompanyID = company.CompanyID;
            setting.SettingValue = value;
            uow.Insert(setting);
            uow.Save();
        }
    }
}

3 个答案:

答案 0 :(得分:2)

嗨,我一直在与这个问题作斗争,这就是我想出来的......

public class UnitOfWorkFactory
{
    private static readonly Hashtable _threads = new Hashtable();
    private const string HTTPCONTEXTKEY = 
        "AboutDbContext.UnitOfWorkFactory";

    public static IUnitOfWork Create()
    {
        IUnitOfWork unitOfWork = GetUnitOfWork();

        if (unitOfWork == null || unitOfWork.IsDisposed)
        {
            unitOfWork = new UnitOfWork();
            SaveUnitOfWork(unitOfWork);
        }
        return unitOfWork;
    }

    public static IUnitOfWork GetUnitOfWork()
    {
        if (HttpContext.Current != null)
        {
            if (HttpContext.Current.Items.Contains(HTTPCONTEXTKEY))
            {
                return (IUnitOfWork)HttpContext
                    .Current.Items[HTTPCONTEXTKEY];
            }
            return null;
        }

        var thread = Thread.CurrentThread;

        if (string.IsNullOrEmpty(thread.Name))
        {
            thread.Name = Guid.NewGuid().ToString();
            return null;
        }

        lock (_threads.SyncRoot)
        {
            return (IUnitOfWork)_threads[Thread.CurrentThread.Name];
        }
    }

    private static void SaveUnitOfWork(IUnitOfWork unitOfWork)
    {
        if (HttpContext.Current != null)
        {
            HttpContext.Current.Items[HTTPCONTEXTKEY] = unitOfWork;
        }
        else
        {
            lock (_threads.SyncRoot)
            {
                _threads[Thread.CurrentThread.Name] = unitOfWork;
            }
        }
    }

    public static void DisposeUnitOfWork(IUnitOfWork unitOfWork)
    {
        if (HttpContext.Current != null)
        {
            HttpContext.Current.Items.Remove(HTTPCONTEXTKEY);
        }
        else
        {
            lock (_threads.SyncRoot)
            {
                _threads.Remove(Thread.CurrentThread.Name);
            }
        }
    }
}

public interface IUnitOfWork : IDisposable
{
    void Commit();
    bool IsDisposed { get; }
}

public class UnitOfWork : MyContext
{

}

public abstract class Repository<T>
    : IRepository<T>, IDisposable where T : class
{
    private UnitOfWork _context;

    private UnitOfWork Context
    {
        get
        {
            if (_context == null || _context.IsDisposed)
                return _context = GetCurrentUnitOfWork<UnitOfWork>();

            return _context;
        }
    }

    public TUnitOfWork GetCurrentUnitOfWork<TUnitOfWork>() 
        where TUnitOfWork : IUnitOfWork
    {
        return (TUnitOfWork)UnitOfWorkFactory.GetUnitOfWork();
    }

    public IEnumerable<T> Get(Expression<Func<T, bool>> predicate)
    {
        return Context.Set<T>().Where(predicate).ToList();
    }

    public bool Exists(Expression<Func<T, bool>> predicate)
    {
        return Context.Set<T>().Any(predicate);
    }

    public T First(Expression<Func<T, bool>> predicate)
    {
        return Context.Set<T>().Where(predicate).FirstOrDefault();
    }

    public IEnumerable<T> GetAll()
    {
        return Context.Set<T>().ToList();
    }

    public IEnumerable<T> GetAllOrderBy(Func<T, object> keySelector)
    {
        return Context.Set<T>().OrderBy(keySelector).ToList();
    }

    public IEnumerable<T> GetAllOrderByDescending(Func<T, object> keySelector)
    {
        return Context.Set<T>().OrderByDescending(keySelector).ToList();
    }

    public void Commit()
    {
        Context.SaveChanges();
    }

    public void Add(T entity)
    {
        Context.Set<T>().Add(entity);
    }

    public void Update(T entity)
    {
        Context.Entry(entity).State = EntityState.Modified;
    }

    public void Delete(T entity)
    {
        Context.Set<T>().Remove(entity);
    }

    public void Dispose()
    {
        if (Context != null)
        {
            Context.Dispose();
        }
        GC.SuppressFinalize(this);
    }
}

public class MyContext : DbContext, IUnitOfWork
{
    public DbSet<Car> Cars { get; set; }

    public void Commit()
    {
        SaveChanges();
    }

    protected override void Dispose(bool disposing)
    {
        IsDisposed = true;
        UnitOfWorkFactory.DisposeUnitOfWork(this);
        base.Dispose(disposing);
    }

    public bool IsDisposed { get; private set; }
}

然后我可以做:

using (var unitOfWork = UnitOfWorkFactory.Create())
{
    _carRepository.Add(new Car
    {
        Make = "Porshe", Name = "Boxter"
    });

    _carRepository.Commit();
}

答案 1 :(得分:1)

您可以使用与当前线程绑定并在服务代码中明确解析的某种“当前”工作单元。您需要类来保存UoW的线程静态实例来实现此目的。但是,这不是一个很好的解决方案。

答案 2 :(得分:-1)

你是法官...... 我认为你是双重的。

第1点: http://www.britannica.com/topic/Occams-razor

第2点: 从EF主对象的F2对象浏览器描述,DBContext ...

公共类DbContext     System.Data.Entity的成员

要点: DbContext实例表示工作单元 存储库模式的组合,以便它可用于从数据库和组中查询更改然后将作为一个单元写回商店。