【问题标题】:How can I inject repository in a simple way?如何以简单的方式注入存储库?
【发布时间】:2014-04-23 15:36:13
【问题描述】:

我正在尝试使用 UnitOfWork、Ninject 和 Repository 模式,现在我有这个代码:

 public class UnitOfWork : IUnitOfWork
{
    private readonly MyContext context;

    public IGenericRepository<User> UserRepository { get; set; }
    public IGenericRepository<EmailChangesByUser> EmailChangesByUserRepository { get; set; }
    public IGenericRepository<PasswordChangesByUser> PasswordChangesByUserRepository { get; set; }
    public IGenericRepository<MyUserDataChanges> MyUserDataChangesByUserRepository { get; set; }
    public IGeoRepository GeoRepository { get; set; }
    public IFileManager FileManager { get; set; }

    public UnitOfWork(MyContext cont, IGenericRepository<User> userRepo,
        IGenericRepository<EmailChangesByUser> emailChangesByUserRepo,
        IGenericRepository<PasswordChangesByUser> passwordChangesByUserRepo,
        IGenericRepository<MyUserDataChanges> myUserDataChangesByUserRepo,
        IFileManager fileUploader, IGeoRepository geoRepo)
    { ... }
}

一切正常。但是我可以添加附加依赖项,所以构造函数会很胖。所以我试图以另一种方式做同样的事情(总结代码):

public class UnitOfWork : IUnitOfWork
{
    private readonly MyContext _context;
    private Hashtable _repositories;

    public UnitOfWork(MyContext context)
    {
        _context = context;
    }

    public IRepository<TEntity> Repository<TEntity>() where TEntity : IEntity
    {
        if (_repositories == null)
        {
            _repositories = new Hashtable();
        }

        var type = typeof(TEntity).Name;

        if (_repositories.ContainsKey(type))
        {
            return (IRepository<TEntity>)_repositories[type];
        }

         _repositories.Add(...) // HERE IS MY PROBLEM, ANY WAY TO INJECT HERE REPOS

        return (IRepository<TEntity>)_repositories[type];
    }
}

我不知道我是否能正确解释自己!

【问题讨论】:

    标签: entity-framework dependency-injection ninject asp.net-mvc-5 unit-of-work


    【解决方案1】:

    您可以以不同的方式处理此问题。由于存储库和工作单元都以抽象方式(使用接口)指定,您的工作单元的具体实现应该创建存储库的具体实例:

    public class EntityFrameworkUnitOfWork : IUnitOfWork
    {
      private readonly MyContext context;
    
      private IGenericRepository<User> _userRepository 
      public IGenericRepository<User> UserRepository 
      { 
          get
          {
              if ( _userRepository == null )
                 _userRepository = new EntityFrameworkUserRepository( this.context );
              return _userRepository;
          }
      }
    
      public UnitOfWork(MyContext cont)
      { 
          this.cotnext = cont;
      }
    }
    
    public class AnotherConcreteUnitOfWork : IUnitOfWork
    {
      private readonly Something some;
      private readonly Another dependency;
    
      private IGenericRepository<User> _userRepository 
      public IGenericRepository<User> UserRepository 
      { 
          get
          {
              if ( _userRepository == null )
                 _userRepository = new AnotherConcreteUserRepository( some, dependency );
              return _userRepository;
          }
      }
    
      public UnitOfWork(Something some, Another dependency)
      { 
          this.some = some;
          this.dependency = dependency;
      }
    }
    

    这样当您在实现之间切换时,您只需指定工作单元的具体类型:

    kernel.Bind<IUnitOfWork>().To<EntityFrameworkUnitOfWork>();
    

    kernel.Bind<IUnitOfWork>().To<AnotherConcreteUnitOfWork>();
    

    请注意,为具体的存储库设置一个具体的工作单元可以更轻松地处理您必须首先注入到工作单元并从那里注入到存储库的外部依赖项(在上面的示例中,有实体框架单元工作单元取决于 dbcontext,而其他工作单元取决于其他对象)。

    【讨论】:

    • 谢谢!!你打开了我的眼睛!!
    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多