你调用的对象是空的。 C#接口

时间:2018-08-26 03:55:05

标签: c# interface

我知道这个问题已经问了很多遍了,我知道出现此错误的原因是因为LicenseInfoRepository为空,但是我不确定如何解决它。

public abstract class VendorCodeGeneratorBase
{

    protected ILicenseInfoRepository LicenseInfoRepository;
    protected ICountryRegionRepository CountryRegionRepository;
    protected IEnumerable<IBrandModel> BrandModels;

    protected string GenerateVendorCodeUnEncoded(GenerateVendorCodeRequestModel requestModel)
    {
        int formatId = requestModel.VendorCodeFormat;
        var strToConvert = "";
        var brandBytes = new byte[5];
        var brands = LicenseInfoRepository.GetSubscriptionBrandsForVendorCode(requestModel.KeyNumber);

该错误发生在行上

var brands = LicenseInfoRepository.GetSubscriptionBrandsForVendorCode(requestModel.KeyNumber);

我不知道如何实例化实现该接口的类。这是课程

public class LicenseInfoRepository : ILicenseInfoRepository
{
    private readonly IEstDbContext _estDbContext;

    public LicenseInfoRepository(IEstDbContext estDbContext)
    {
        _estDbContext = estDbContext;
    }

    public List<VendorCodeSubscriptionBrandModel> 
    GetSubscriptionBrandsForVendorCode(int keyNumber)
    {
        return _estDbContext.SubscriptionBrands
            .Include(sb => sb.Brand)
            .Where(sb => sb.KeyNumber == keyNumber)
            .ProjectTo<VendorCodeSubscriptionBrandModel>()
            .ToList();
    }
}

我有一个名为repositories的文件夹,里面有LicenseInfoRepository.cs类,里面还有一个有Interfaces接口的名为ILicenseInfoRepository.cs的文件夹。该界面如下所示:

namespace Data.Repositiories
{
    public interface ILicenseInfoRepository
    {
        List<VendorCodeSubscriptionBrandModel> 
GetSubscriptionBrandsForVendorCode(int keyNumber);
    }
}

1 个答案:

答案 0 :(得分:0)

在拥有LicenseInfoRepository.cs的文件夹中,需要一个接口类ILicenseInfoRepository.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
// Add/remove as appropriate

namespace YourProject.DAL // Change to your project / repository folder name
{
    public interface ILicenseInfoRepository : IDisposable
    {
        List<VendorCodeSubscriptionBrandModel> GetSubscriptionBrandsForVendorCode(int keyNumber);
        // Add any other methods that exist in LicenseInfoRepository.cs file
    }
}

您的VendorCodeGeneratorBase类中可能缺少行,尤其是您通常实例化private ApplicationDbContext db = new ApplicationDbContext();的方式。添加界面后,请分享错误/详细信息,以便我们继续修复您的代码。