簡體   English   中英

如何從實現接口的類中調用新方法

[英]How to call new methods from a class which implements interface

如何調用在接口的具體類中實現的新方法我正在使用結構映射IOC容器。

public interface ICompanyRepository
{
    IEnumerable<Company> GetAll();
    Company Get(int id);
    Company Add(Company item);
    bool Update(Company item);
    bool Delete(int id);
}

public class CompanyRepository: ICompanyRepository
{
   // Provide implementation for all interface  methods

//Class CompanyRepository will also have the new method called DisplayLog
    public void DisplayLog()
    {
        //To do
    }
}

我正在嘗試在我的Customer控制器類中使用結構映射來實現DI,如何告訴我需要調用company2的方法?

_.Scan(x =>
     {
        x.TheCallingAssembly();

        x.AddAllTypesOf<ICompanyRepository>();

        // or

    });

我的代碼:

private readonly ICustomerRepository customerRepository;

public CustomerController(ICustomerRepository CustomerRepository)
{
    customerRepository = CustomerRepository;
}

// GET: Customer  
public ActionResult Index()
{
    var customers = customerRepository.DisplayLog()
   //Here i need to call CompanyRepository class methods DisplayLog()  how can i call it here ?

   // method DisplayLog() is not be shown here 
    return View(customers);
}

在接口上,您只能調用接口中定義的內容-它是實現該接口的所有類的“公共基礎”的定義。 問問自己:如果您獲得的ICompanyRepository類型不實現DisplayLog怎么辦?

這意味着:除了接口方法之外,不可能立即調用其他任何方法。

要在customerRepository上調用DisplayLog ,有3種方法:

  • DisplayLog()添加到接口
  • 將customerRepository強制轉換為CompanyRepository。 但是,如果customerRepository是CompanyRepository以外的任何其他類型,則將導致異常
  • 使用第二個界面

畢竟,我不確定您正在做的是DI。 在我對DI的理解中,應該是這樣的:

public ActionResult Index(ILogDisplay display)
{
    var customers = display.DisplayLog(customerRepository);
    return View(customers);
}

ILogDisplay是要注入的單獨類的新接口

public interface ILogDisplay 
{
    public YourResultType DisplayLog(ICustomerRepository);
}

在此示例中,您實際上在類中注入了一個依賴項(ILogDisplay的實現)。

這里有兩個問題要提出:

  1. 為什么存儲庫知道如何顯示日志?
  2. DisplayLog()在CustomerRepository上下文中是什么意思?
  3. 控制器為何還要關心存儲庫正在記錄什么?
  4. 當DisplayLog的返回類型明顯無效時,為什么要為其分配一個稱為客戶的變量?

從根本上講,存儲庫的行為對於控制器而言應該是未知的,這是控制反轉原理的本質。 它關心的只是給定接口為存儲庫提供的顯式協定,方法調用將返回客戶。 日志記錄是存儲庫的關注點。

從DI的角度來看,這是一個相當傳統的設置,我們可以:

ICompany資料庫:

public interface ICompanyRepository() {
    IEnumerable<Company> GetAll();
    Company Get(int id);
    Company Add(Company item);
    bool Update(Company item);
    bool Delete(int id);
}

客戶資料庫:

public class CompanyRepository: ICompanyRepository
{
    private readonly ILogger logger;

    public CompanyRepository(ILogger logger) {
        this.logger = logger;
    }
    // Provide implementation for all interface  methods

    public Company Get(int id) {

        var customers = this.randomCustomerSource.Get(id);
        this.logger.Info("Whatever you want to log here");
        return customers;
    }
}

CustomerController:

public class CustomerController {
    private readonly ICustomerRepository customerRepository;

    public CustomerController(ICustomerRepository CustomerRepository)
    {
        customerRepository = CustomerRepository;
    }
    // GET: Customers
    public ActionResult Index()
    {
        var customers = customerRepository.GetAll()


        return View(customers);
    }
}

因此,存儲庫請求一個Ilogger ,控制器請求一個ICompanyRepository ,並將只調用GetAll()並返回結果。 通常涉及的內容更多,但這是返回數據的控制器的工作流程的最基本要點。

暫無
暫無

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

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