依赖注入与Unity一起使用但不与Ninject一起使用

时间:2013-06-03 01:39:57

标签: entity-framework dependency-injection ninject unity-container unit-of-work

我一直在使用工作单元和DI工作EF Repository模式。我跟随这个例子:link在示例中使用Unity作为DI容器,但我想使用Ninject。 (注意:使用Unity一切正常)。 所以我设置了我的Ninject Controller Factory:link。 当我去我的控制器并尝试列出消息时,它工作正常但是当我尝试创建消息没有任何反应时,值被传递到控制器和存储库,但数据由于某种原因没有保存到数据库。 这是控制器构造函数:

 private readonly IUserRepository _userRepository;
    private readonly IMessageThreadRepository _messageThreadRepository;

    public TetsController(IMessageThreadRepository messageThreadRepository, IUserRepository userRepository)
    {
        _messageThreadRepository = messageThreadRepository;
        _userRepository = userRepository;
    }

这是messageThreadRepository的代码,由控制器

调用
 public class MessageThreadRepository : RepositoryBase<MessageThread>, IMessageThreadRepository
{
    private readonly RepositoryBase<MessageThread> _messageThreadRepository;
    private readonly RepositoryBase<Message> _messageRepository; 
    private readonly IUnitOfWork _unitOfWork;
    private readonly UserRepository _userRepository;

    public MessageThreadRepository(IUnitOfWork unitOfWork, RepositoryBase<MessageThread> messageThreadRepository,
                                   IDatabaseContextFactory databaseContextFactory, UserRepository userRepository, RepositoryBase<Message> messageRepository)
        : base(databaseContextFactory)
    {
        _unitOfWork = unitOfWork;
        _messageThreadRepository = messageThreadRepository;
        _userRepository = userRepository;
        _messageRepository = messageRepository;
    }
 //some code here
 // part with adding to database
  Message message = messageFactory.CreateMessage(messageBody, userSender, messageThread);

        _messageRepository.Add(message);

        if (!doesThreadExist)
        {
            _messageThreadRepository.Add(messageThread);
        }

        _unitOfWork.Commit();
}

修改 我试图将用户添加到数据库,但没有运气。我再次设法获取所有用户,但我无法添加它们,这里是用户存储库:

public class UserRepository : RepositoryBase<User>, IUserRepository
{
    private readonly IUnitOfWork _unitOfWork;
    private readonly RepositoryBase<User> _repository; 

    public UserRepository(IUnitOfWork unitOfWork, IDatabaseContextFactory databaseContextFactory, RepositoryBase<User> repository) : base (databaseContextFactory)
    {
        _unitOfWork = unitOfWork;
        _repository = repository;
    }

    public User GetUserById(int id)
    {
        return _repository.GetSingleByCriteria(u => u.UserId.Equals(id));
    }

    public User GetUserByEmail(string email)
    {
        return _repository.GetSingleByCriteria(u => u.Email.Equals(email));
    }

    public User GetUserByUserName(string name)
    {
        return _repository.GetSingleByCriteria(u => u.UserName.Equals(name));
    }

    public void AddUser(User user)
    {
        _repository.Add(user);
        _unitOfWork.Commit();
    }

    public IEnumerable<User> GetAllUsers()
    {
        return _repository.GetAll();
    }
}

1 个答案:

答案 0 :(得分:0)

我假设您正在使用MVC,因为您引用了一篇关于将其与MVC一起使用的文章。

你做了这里提到的所有步骤吗?

http://bubblogging.wordpress.com/2012/06/04/mvc-controller-factory-ninject/

为什么使用自定义控制器工厂而不是使用Ninject.MVC3包使用MVC的内置依赖注入支持?