首页 > 解决方案 > 对象引用未设置为存储库层的对象实例

问题描述

我在我的框架上收到空异常错误。我尝试在我的应用程序中应用存储库和工作单元设计模式。我想做的只是用GetAll()方法从我的数据库中检索用户标题。

这是我的存储库类:

public class Repository<T> : IRepository<T> where T : class
{
    protected readonly DbContext Context;

    public Repository(DbContext context)
    {
        this.Context = context;
    }

    public T Get(int id)
    {
        return Context.Set<T>().Find(id);
    }

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

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

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

    public void AddRange(IEnumerable<T> entityList)
    {
        Context.Set<T>().AddRange(entityList);
    }

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

    public void RemoveRange(IEnumerable<T> entityList)
    {
        Context.Set<T>().RemoveRange(entityList);
    }
}

这是IUserTitlesRepository

public interface IUserTitlesRepository : IRepository<UserTitles>
{

}

并且,实现上述接口的类:

    public UserTitlesRepository(XaPaDataContext context) : base(context)
    {

    }

    public XaPaDataContext XaPaDataContext
    {
        get { return Context as XaPaDataContext; }
    }

在进入 Controller 层之前,我还有两层,分别是 Operation 和 Manager 层。而且,我认为我在这部分搞砸了(在 Base Manager 类上,如下所示)。

这是操作层:

public class UserTitlesOperations
{
    private readonly IUnitOfWork _uow;

    public UserTitlesOperations(IUnitOfWork uow)
    {
        _uow = uow;
    }

    public List<UserTitles> GetAllUserTitles()
    {
        try
        {
            List<UserTitles> userTitleList = _uow.UserTitles.GetAll().ToList();
            _uow.Complete();
            return userTitleList;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
    }
}

下面是BaseManager为所有管理器类提供继承的类。

public abstract class BaseManager
{
    private IUnitOfWork _iUow;

    private readonly XaPaDataContext _context;

    public IUnitOfWork IUOW
    {
        get
        {
            if (_iUow == null)
            {
                _iUow = new XaPaUnitOfWork(_context);
            }

            return _iUow;
        }
    }
}

这是经理类:

public class UserTitlesManager : BaseManager
{
    private readonly UserTitlesOperations _userTitlesOperations;

    public UserTitlesManager()
    {
        _userTitlesOperations = new UserTitlesOperations(base.IUOW);
    }

    public List<UserTitlesWM> GetAllUserTitles()
    {
        try
        {
            return UserTitlesMapping.MaptoWM(_userTitlesOperations.GetAllUserTitles());
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
    }
}

最后,这是我的 API 控制器:

[Route("api/LoginRequest")]
public class TitlesController : BaseController
{
    UserTitlesManager _userTitlesManager;

    public LoginController()
    {
        _userTitlesManager = new UserTitlesManager();
    }

    [Route("RetreiveTitles")]
    public HttpResponseMessage GetTitles()
    {
        try
        {
            return Request.CreateResponse(HttpStatusCode.OK, _userTitlesManager.GetAllUserTitles());
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.ToString());
        }
    }
}

顺便说一句BaseController,它只是另一个 API 控制器,它为所有其他 API 控制器提供继承,并包含一个由所有其他控制器使用的方法。

所以,我仍在努力提高自己对这种设计模式的认识,如果有人能在BaseManager课堂上展示我的错误,我会很高兴。正如我所说,我想问题是由那条private readonly XaPaDataContext _context;线引起的。另一方面,我不知道如何纠正它,因为我的操作类的构造函数要求IUnitOfWork.

先感谢您!

编辑:

刚刚意识到我忘了分享我的工作单元课程:

public class XaPaUnitOfWork : IUnitOfWork
{
    private readonly XaPaDataContext _context;

    public XaPaUnitOfWork(XaPaDataContext context)
    {
        _context = context;
        Categories = new CategoriesRepository(_context);
        OrderDetails = new OrderDetailsRepository(_context);
        Orders = new OrdersRepository(_context);
        ProductImages = new ProductImagesRepository(_context);
        Products = new ProductsRepository(_context);
        Users = new UsersRepository(_context);
        UserTitles = new UserTitlesRepository(_context);
        UserTokens = new UserTokensRepository(_context);
    }

    public ICategoriesRepository Categories { get; private set; }
    public IOrderDetailsRepository OrderDetails { get; private set; }
    public IOrdersRepository Orders { get; private set; }
    public IProductImagesRepository ProductImages { get; private set; }
    public IProductsRepository Products { get; private set; }
    public IUsersRepository Users { get; private set; }
    public IUserTitlesRepository UserTitles { get; private set; }
    public IUserTokensRepository UserTokens { get; private set; }

    public int Complete()
    {
        return _context.SaveChanges();
    }

    public void Dispose()
    {
        _context.Dispose();
    }
}

标签: c#entity-frameworkrepository-patternnullreferenceexceptionunit-of-work

解决方案


在我更改了我的 BaseManager 类后,如下所示:

public abstract class BaseManager
{
    private IUnitOfWork _iUow;

    public IUnitOfWork IUOW
    {
        get
        {
            if (_iUow == null)
            {
                _iUow = new XaPaUnitOfWork(new XaPaDataContext());
            }

            return _iUow;
        }
    }
}

我已经收到HttpStatusCode.OK

但是,老实说,我仍然不确定真正的原因。我主要是用心做这个更正。


推荐阅读