Dependency Injection difficulties

36 views
Skip to first unread message

Marcelo Zabani

unread,
Oct 27, 2013, 2:18:51 PM10/27/13
to simple...@googlegroups.com
I've been trying to do some dependency injection, but I've faced some difficulties:

  • There are no Register<T> methods in ISimpleContainer. Although an implementation may have them, it would be nice if I could register instance factories that received some type of context information. Something like having Register<T>(Func<IContext, T> factory) in ISimpleContainer would be helpful.
  • Is it possible to access owin entries in IContext's Variables property? I haven't found a way to get a reference to an instance that implements IContext yet so I don't know. It would be very useful, however, since this would make it possible for middleware not to maintain their owin per request dictionaries when they want to serve something with a per-request lifetime.
  • Why are the Get<T>() and Get(Type) in ISimpleContainerScope? What is the purpose of having a scoped container? (I'm just a beginner when it comes to dependency injection, so please bear with me..)

Thank you!

James D'Angelo

unread,
Jan 7, 2014, 4:47:12 PM1/7/14
to simple...@googlegroups.com
Hi Marcelo,

You are probably trying to use the interface as if the IoC container was pre-baked into Simple.Web. You actually need to install a NuGet package targeting your favorite IoC container or write your own wrapper to plug into Simple.Web. Using the Simple.Web.StructureMap package you would setup your IoC like so:

using Simple.Web.StructureMap

public class StructureMapStartupTask : StructureMapStartupBase
{
    protected override Configure(ConfigurationExpression cfg)
    {
        cfg.For<IFoo>().Use<Foo>();
    }
}

This class will automatically get picked up by Simple.Web (the base class implements IStartupTask) and your dependencies will be registered nicely. You can even have multiple "StructureMapStartupBase"s so you can segregate your configuration. Hope this helps.
Reply all
Reply to author
Forward
0 new messages