私はMEFを使用して、異なるタイプのコンストラクタ引数を持つ同じタイプのアイテムを作成できますか?
IOne
ITwo
IThree
ICalcalculator
そしてそれらのいくつかの実装
-
を持って定義された次のインタフェースを持っています
ClassOne: IOne
ClassTwo: IOne
ClassThree : ITwo
ClassFour : ITwo
ClassFive: IThree
MyCalc : ICalculator
MyCalcはクラス1-4、しかし2つのClassOne
のインスタンスと初期化即ちとして区別ClassThree
を必要
public MyCalc(){
ClassOne first= new ClassOne("filePath1");
ClassOne second = new ClassOne("filePath2");
ClassThree third = new ClassThree ("filePath3");
ClassThree fourth = new ClassThree ("filePath4");
}
私はMEFを使用してMyCalcを作成しようとしています。上記の例では、filePathX
は設定ファイルに含まれています。私はこれまでに以下のことを行ってきましたが、それはうまくいくように見えますが、現在の方法とアプローチが正しくないと感じています。このアプローチを見ると、私は名前(ValDatePrices & EDDatePrices)に縛られていますが、私の現在のアプローチ(上記を参照)よりもクリーンではありません。
異なるctor引数を持つ同じタイプの複数のオブジェクトを読み込むには、よりクリーンな方法がありますか?
public class MyCalc: ICalculator
{
private CompositionContainer container;
public MyCalc()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MyCalc).Assembly));
container = new CompositionContainer(catalog);
try
{
this.container.ComposeExportedValue<ClassFive>(
new ClassFive((ConfigurationManager.AppSettings["SomePath"])));
this.container.ComposeExportedValue<ClassOne>(
"ValDatePrices"
, new ClassOne((ConfigurationManager.AppSettings["filePath1"])));
this.container.ComposeExportedValue<ClassOne>(
"EDDatePrices"
, new ClassOne((ConfigurationManager.AppSettings["filePath2"])));
this.container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
[Import("ValDatePrices")]
public ClassOne ValDatePrices;
[Import("EDDatePrices")]
public ClassOne EDDatePrices;
[Import]
public ClassFive SPointReader;
public void Calculate()
{
Console.WriteLine(SPointReader.Result);
Console.WriteLine(ValDatePrices.Result.Count);
Console.WriteLine(EDDatePrices.Result.Count);
Console.ReadKey();
}
}
使用
class Program
{
static void Main(string[] args)
{
var p = new MyCalc();
p.Calculate();
}
}
}
サイド質問:MyCalc
コンストラクタ内のコードを配置すべきですか?
がよさそうだ参照してください、前にこのようにそれについて考えたことはありません - しかしMEFベースのソリューションを探して、それと作品を試みたが、最終的な目標は、これらのアイデアを包含することであるので、 WPF/Prismベースのアプリケーション – Ahmad
MEFを使用する特別な理由はありますか? NInjectは、MEFが行うことができるすべてのことを行うことができます。財産の輸入/注射、マルチインジェクションなどのようなものです。私は、WPFとほとんどの場合、Prismよりはるかに優れたオプションと互換性があると言います。シンプルでありながら強力です:) – edvaldig