0

我一直在使用 Unit of Work 和 DI 研究 EF 存储库模式。我正在关注这个示例:链接在示例中使用 Unity 作为 DI 容器,但我想使用 Ninject。(注意:使用 Unity 一切正常)。所以我已经像这样设置了我的 Ninject 控制器工厂: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();
    }
}
4

1 回答 1

0

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

您是否执行了此处提到的所有步骤?

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

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

于 2013-06-03T06:20:09.150 回答