簡體   English   中英

使用基類(C#)的DI

[英]DI with using base class (C#)

我有基本的附件控制器,這是它的代碼

    public class ApiAttachmentControllerBase<T> : PM101MobileApiController where T : Entity<int>
    {
        private readonly IObjectStorageManager _storageManager;
        private readonly IRepository<T> _repository;

        public ApiAttachmentControllerBase(IObjectStorageManager storageManager, IRepository<T> repository)
        {
            _storageManager = storageManager;
            _repository = repository;
        }



         private void CheckFileSize(IFormFile file)
        {
            if (file.Length > PM101Consts.MaxAttachmentSize)
            {
                throw new UserFriendlyException(L("Attachment_Warn_SizeLimit", PM101Consts.MaxAttachmentSizeMb.ToString()));
            }

        }

        private void CheckFileType(IFormFile file, params string[] supportedTypes)
        {
            if (supportedTypes.Any())
            {
                var extention = Path.GetExtension(file.FileName);
                if (!supportedTypes.ToList().Contains(extention))
                {
                    throw new UserFriendlyException(L("Attachment_Warn_Type", extention));
                }
            }

        }
   }
}

我在另一個這樣的控制器中繼承了它

 public class InspectionsController : ApiAttachmentControllerBase<Inspection>
{
    private readonly IRepository<Inspection> _inspectionRepository;

    public InspectionsController(IObjectStorageManager storageManager, IRepository<Inspection> repository,
        IRepository<Inspection> inspectionRepository) : base(storageManager, repository)
    {
        _inspectionRepository = inspectionRepository;
    }


    /// <summary>
    /// Method for posting pre-inspection
    /// </summary>
    /// <remarks>When you start pre inspection you send just jobId, tenantId, and status
    /// When you finishing inspection you send full DTO with all fields</remarks>
    /// <response code="200">Returns if pre inspection created
    /// </response>
    [HttpPost]
    public async Task<IActionResult> AddPreInspection(CreatePreInspectionDto input)
    {
        var preInspection = new Inspection();
        ObjectMapper.Map(input, preInspection);
        await _inspectionRepository.InsertAsync(preInspection);
        return Ok();
    }

AddPreInspection我嘗試使用像repository.InsertAsync這樣的repository.InsertAsync

但是它不起作用,所以我為存儲庫創建DI,例如private read-only IRepository<Inspection> _inspectionRepository;

這是一個好習慣還是我可以使用基類中的存儲庫?

如果可以,我該怎么辦?

如果您使基本ApiAttachmentControllerBase擁有protected (而不是private )存儲庫,那么InspectionsController將可以訪問它。

protected訪問修飾符允許該成員被當前類或派生類訪問。

public class ApiAttachmentControllerBase<T> : PM101MobileApiController where T : Entity<int>
{
  private readonly IObjectStorageManager _storageManager;
  protected readonly IRepository<T> Repository;

  public ApiAttachmentControllerBase(IObjectStorageManager storageManager, IRepository<T> repository)
  {
    _storageManager = storageManager;
    Repository = repository;
  }
....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM