2011-11-16 20 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

请更改。我没有得到你 –