views:

87

answers:

3

If I register a bunch of data structures with an IoC container, I'd like to say (C# syntax):

var lookup = container.Create<IDictionary<Name,ISequence<EMail>>>() ;

The container should magically find the registered types that implement IDictionary and ISequence, and construct the type I need. Basically, I want to create types based on interfaces and have the container figure out which concrete types will fulfill my requirements.

[edit] I've got an interface layer that defines many types, and several implementation layers. I want to load a particular implementation and have the container automagically figure out that IExpr can be resolved with CExpr (or RubyExpr or PythonExpr). If it's ambiguous then raise an exception.

Has anyone with experience with Java or .NET IoC container frameworks seen one that can do this? Thanks.

A: 

I use StructureMap and it can do that....You can register the concrete type in a config file or in code.

StructureMap

Code config:

private static void LoadMap()
        {
            StructureMap.Pipeline.SmartInstance<ConcreteService> ServicePipeline = StructureMap.StructureMapConfiguration.ForRequestedType<IService>().TheDefault.Is.OfConcreteType<ConcreteService>();
            IPServicePipeline.WithCtorArg("serviceIP").EqualTo("192.168.0.1");
            StructureMap.StructureMapConfiguration.ForRequestedType<IDataRepository>().TheDefaultIsConcreteType<MySqlDataRepository>();
}

File Config:

<StructureMap MementoStyle="Attribute">
     <DefaultInstance PluginType="Blah.Core.Services.IService,Blah.Core" PluggedType="Blah.Services.ConcreteService,Blah.Services" ServiceIP="192.168.5.31"/>
     <DefaultInstance PluginType="Blah.Core.Data.IDataRepository,Blah.Core" PluggedType="Blah.Data.MySql.MySqlDataRepository,Blah.Data"/>

Config for Generic:

<DefaultInstance PluginType="Blah.Core.Services.IService`1,Blah.Core" PluggedType="Blah.Services.EmailNotificationService`1,Blah.Services" smtpServerAddress="192.168.0.1" port="25"/>

Get Generic type:

IService<User> myUserService = ObjectFactory.GetInstance<IService<User>>();
CSharpAtl
It looks like StructureMap can Scan an assembly and [automatically register types](http://structuremap.sourceforge.net/ScanningAssemblies.htm). Looks good.
projectshave
yes you can...which is nice as long as you do not have primitives in your ctors or have more than one class that implements the interfaces you want to register.
CSharpAtl
+1  A: 

Have you tried taking a look at Guice? It's a Java DI framework that binds interfaces to implementations using annotations not XML.

parkr
+1  A: 

Spring is the standard IoC framework for Java. By default you have to set up a configuration file which dynamically loads the correct classes at execution time. With a little manipulation, you could probably get it to use the correct interpreter, but you would need to include either some meta-data with your expressions, or include a language parser for each language, and iterate through your parsers with each expression. In what way are you looking to have the expression resolved, "out of thin air"?

hasalottajava