将unity转换为autofac

时间:2015-05-31 02:17:49

标签: c# unity-container autofac

我正在尝试将我在缓存中找到的示例代码转换为存储库。唯一的问题是它使用统一的DI,我不知道它是如何工作的。

public interface IUnitOfWork : IDisposable
{
    IRepository<Blog> BlogRepository { get; }
    Task<int> SaveChangesAsync();
}

public class UnitOfWork : IUnitOfWork
{
    private IDataStoreContext dataStoreContext;

    private readonly IUnityContainer container;

    public IRepository<Blog> BlogRepository
    {
        get
        {
            // TODO : Use unity containers to generate the UnitOfwork so that to make surethat
            // datacontext is a single instance in that instance of uow
            return new GenericRepository<Blog>(
                this.container.Resolve<ICacheStrategy<Blog>>(),
                new SqlDataStoreStrategy<Blog>(this.dataStoreContext));
        }
    }


    public UnitOfWork(IDataStoreContext dataStoreContext, IUnityContainer container)
    {
        this.dataStoreContext = dataStoreContext;
        this.container = container;
    }

    public async Task<int> SaveChangesAsync()
    {
        return await this.dataStoreContext.SaveChangesAsync();
    }

    public void Dispose()
    {
        this.dataStoreContext.Dispose();
    }
}

有人能指出我应该如何将其作为autofac声明写出正确的方向吗?

我只需要使用基本的autofac,例如

    var builder = new ContainerBuilder();

        // Create the container and use the default application services as a fallback
        AutofacRegistration.Populate(builder, services);

        builder.Register(c => new Logger())
             .As<ILogger>()
             .InstancePerLifetimeScope();

0 个答案:

没有答案