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);
最初のものは複雑すぎるようです。
前もって感謝します!