2011-11-16 44 views
0

我正在創建一個API,我想公開一個名爲IReportFields的接口,我希望客戶端實現這個類,並且基本上從任何數據源(通常是數據庫)獲取字段。如何使用Ninject連接/綁定多個實施?

在我的IReport接口中,我想接受一個I​​ReportFields實例(可以是多個,在我的應用程序中我至少有4個實例),然後在該接口中做任何我需要做的事情,通常它會是像構建報告或其他內容。

因此,例如:

public interface IReportField 
{ 
ICollection<ReportField> GetFields(); 
} 

有很多種類型的報表字段如他們可以推導出3個或4個不同的數據庫表等等

然後我的主界面上我會:

public interface IReport 
{ 
string GetReport(IReportField[] field); 
} 

問:

IReportFields可以有多種實現方式,即許多不同我怎樣稱呼方法GetReport請記住我正在使用Ninject,我該如何將接口連接在一起?

//該位是我在哪裏卡住了,我怎麼傳中,PARAMS,因爲我不想上,需要我一類的硬扶養得到報告

IFieldReport field1 = new FieldImp1(); 
IFieldReport field2 = new FieldImp2(); 

var report = GetReport(feild1, field2); 

回答

1

您可以使用已經全部連接好Constructor注射IFieldReports如您有接線如下:

IKernel kernel = new StandardKernel(); 

kernel.Bind<IReportField>().To<FieldImp1>(); 
kernel.Bind<IReportField>().To<FieldImp2>(); 
kernel.Bind<IReport>().To<ReportImpl>(); 

而且你有ReportImpl這樣的:

public class ReportImpl : IReport 
{ 
    public ReportImpl(List<IReportField> fieldReports) 
    { 
     // you now have all the wires to IReportField in fieldReports parameter 
     foreach(IReportField fieldReport in fieldReports) 
     { 
      var fields = fieldReport.GetFields(); 
      // do whatever with the fields 
     } 
    } 
} 

請讓我知道,如果我聽錯了

+0

將ninject FO,拿起我已經加入到我的籽粒沒有我不得不把它提供給iReport的的IReportField的所有實現?我不知道這是可能的? – Haroon

+0

請更改。我沒有得到你 –