1

NInject や StructureMap などの他の IoC コンテナーがこれよりもはるかにクリーンであれば、それらを受け入れます。StructureMap がこれを簡素化する「コンテナ」を導入したと聞きましたが、おそらく?

タイトルの通りですが、何か良い方法はありませんか?これは、ファクトリを作成する必要があるオブジェクトを登録するためだけに、大量のコードのように見えます。

// The process to register an object, with a factory method
var cfg = new MutableConfiguration(p.Name);
cfg.Attributes["factoryId"] = p.TypeFactory.Name;
cfg.Attributes["factoryCreate"] = "Create";
var model = _container.Kernel.ComponentModelBuilder.BuildModel(
    p.Name, p.TypeService, p.Type, null);
model.LifestyleType = LifestyleType.Pooled;
model.Configuration = cfg;
_container.Kernel.AddCustomComponent(model);

コンポーネントを追加する「非工場」の方法とは対照的に:

// registering a component with no factory method
_container.AddComponentLifeStyle(
    p.Name, p.TypeService, p.Type, LifestyleType.Singleton);

最初のものは複雑すぎるようです。

前もって感謝します!

4

1 に答える 1

3

何を登録しようとしているのかはわかりませんが (p最初のコード ブロックには何がありますか?) UsingFactoryMethod、工場登録は簡単です。サンプルコード:

container.AddFacility<FactorySupportFacility>()
   .Register(
      Component.For<IMyService>()
         .UsingFactoryMethod(() => MyLegacyServiceFactory.CreateMyService())
         .LifeStyle.Pooled
   );
于 2010-03-03T01:33:41.857 に答える