views:

58

answers:

1

I am open to other IoC containers, such as NInject and StructureMap if they are much cleaner than this. I hear that StructureMap just introduced "containers" that may simplify this , perhaps?

As the title says, is there a better way? This seems like a lot of code, just to register an object that requires a factory to create it.

// 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);

Versas the "non-factory" way of adding a component:

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

The first seems overly complex.

Thanks in advance!

+3  A: 

I'm not sure what you're trying to register (what's p in the first code block?) but with UsingFactoryMethod, factory registration is a breeze. Sample code:

container.AddFacility<FactorySupportFacility>()
   .Register(
      Component.For<IMyService>()
         .UsingFactoryMethod(() => MyLegacyServiceFactory.CreateMyService())
         .LifeStyle.Pooled
   );
Mauricio Scheffer
Hello again Mauricio! Wow, that does simplify things. As a comment to the question above mentioned, there seems to be a different place for documention, even though the main castleproject site points you to this: http://api.castleproject.org/ Thank you. Marked as answer. Oh, and the "p" was from an Linq query. shouldn't have mattered.
eduncan911
IOU the download page improvement. I haven't forgotten :)
Mauricio Scheffer

related questions