Tom Troughton
Tom Troughton

Reputation: 4325

StructureMap and classes that cannot accept constructor arguments

I have a ASP.NET web application (not MVC) which is actually a CMS application. I'm trying to set up StructureMap IoC framework and it's working well, but I've now hit a blocker in my understanding.

In my understanding, StructureMap enforces a pattern where all dependencies are registered in the core application assembly, so underlying assemblies do not themselves have a dependency on StructureMap.

So, say my application is My.App and it references another assembly My.Logic. My dependencies are all registered in a Container in My.App. This means that a class in My.Logic can take injected dependencies using a constructor like this:

public class Foo
{
  private readonly IBar bar;
  public Foo(IBar bar)
  {
    this.bar = bar;
  }
}

But now I have a case where my class in My.Logic is a type which must be registered in the CMS, and this requires that it has an empty constructor.

So the problem is, if I can't inject using constructor parameters, and My.Logic doesn't have a dependency on My.App so I don't have access to the IoC container, is it possible to use StructureMap to handle this scenario?

If not, what alternative do I have other than create the class within the same assembly as the IoC container?

Upvotes: 0

Views: 51

Answers (1)

ozczecho
ozczecho

Reputation: 8849

Use setter injection. See here

For<IBar>().Use<MyBar>();

Policies.FillAllPropertiesOfType<IBar>();

Upvotes: 0

Related Questions