【问题标题】:'UnitOfWork._mandateRepository' is never assigned to, and will always have its default value null'UnitOfWork._mandateRepository' 从未分配给,并且始终具有其默认值 null
【发布时间】:2022-01-14 23:19:54
【问题描述】:

我正在 ASP.NET Core Web API 中实现 Repository 和 UnitOfWork。

我有这个代码:

数据库上下文:

public class DDMDbContext : IdentityDbContext<ApplicationUser>
{
    public DDMDbContext(DbContextOptions<DDMDbContext> options)
    : base(options) { }

    public virtual DbSet<Mandate> Mandates { get; set; }
}

型号如下图:

public abstract class EntityBase
{
    [Key]
    public int Id { get; set; }
}

public class Mandate : EntityBase
{
    public DateTime DueDate { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

IBaseRepository:

public interface IBaseRepository<T> where T : BaseEntity
{
    Task<IEnumerable<T>> GetAll();
}

基础存储库:

public class BaseRepository<T> : IBaseRepository<T> where T : BaseEntity
{
    private readonly DDMDbContext _context;
    private DbSet<T> _entities;

    public BaseRepository(DDMDbContext context)
    {
        _context = context;
        _entities = context.Set<T>();
    }

    public async Task<IEnumerable<T>> GetAll()
    {
        var list = await _entities.ToListAsync();
        return list;
    }
}

最后是 UnitOfWork:

public interface IUnitOfWork : IDisposable
{
    IBaseRepository<Mandate> MandateRepository { get; }

    void SaveChanges();
    Task SaveChangesAsync();
}

工作单元:

public class UnitOfWork : IUnitOfWork
{
    private readonly DDMDbContext _context;

    public UnitOfWork(DDMDbContext context)
    {
        _context = context;
    }
    #region Mandate
    private readonly IBaseRepository<Mandate> _mandateRepository;

    public IBaseRepository<Mandate> MandateRepository => _mandateRepository ?? new BaseRepository<Mandate>(_context);
    # endregion

    public void Dispose()
    {
        if (_context != null)
        {
            _context.Dispose();
        }
    }
}

现在的问题是,当我浏览 IUnitOfWork 中的代码时。

我收到了这个警告:

然后我收到这条消息:'UnitOfWork._mandateRepository' 从未分配给。

_mandateRepository

中突出显示

私有只读 IBaseRepository _mandateRepository;

我该如何解决这个问题?

【问题讨论】:

  • 消息很清楚,请问您有什么问题?

标签: c# asp.net-core


【解决方案1】:

这一行总是返回一个新的 BaseRepository,因为 _mandateRepository 永远不会被设置。

public IBaseRepository<Mandate> MandateRepository => _mandateRepository ?? new BaseRepository<Mandate>(_context);

你真正想做的是

public IBaseRepository<Mandate> MandateRepository
{
  get 
  {
    if (_mandateRepository == null)
    {
      _mandateRepository = new BaseRepository<Mandate>(_context);
    }
    return _mandateRepository;
  }
}

这样,_mandateRepository 被初始化一次。不过,您必须删除 readonly 修饰符。

【讨论】:

  • 你的意思是我应该从以下位置删除只读:private readonly IBaseRepository _mandateRepository;
  • 可以,否则不能像上面的例子那样在运行时设置。
  • 我是否需要从这个过于私有的只读DDMDbContext _context中删除只读;
  • 不,因为它是在构造函数中设置的。构造函数和初始化程序是您可以设置只读字段或仅获取属性的唯一位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多