I recently upgraded to StructureMap 3.0 and noticed that ObjectFactory.Inject is missing. What is the equivalent for simple injection config that this method provided?
Asked
Active
Viewed 1,833 times
5
-
33.0 moved a lot of methods to `ObjectFactory.Container`. Inject is there, but I'm now to structuremap. So I don't know if it's changed. – Reactgular Jun 04 '14 at 16:15
-
@MathewFoscarini I noticed that as well, but wanted to make sure that's the official response. – leojh Jun 04 '14 at 19:54
-
Official.. that will be difficult. The developer still hasn't released documentation for 3.0, and it might take sometime before docs happen as their making changes to support a new doc format. I've been using the master branch from git, and that's been a lot better since the source has comments that don't exist in any docs. – Reactgular Jun 05 '14 at 01:45
-
I have same question, and yay, I found it, it's in `ObjectFactory.Container` – Vu Nguyen Jul 01 '14 at 04:10
1 Answers
6
As mentioned, 3.0 moved a lot of methods to ObjectFactory.Container
. Inject
is there, but ObjectFactory
will be dropped out at 4.0. So avoid this approach.
Inject
and a lot of methods are in the Container
class. This is not a static class as ObjectFactory
is. To deal with this you can configure like this:
var container = new Container(x =>
{
x.For<IFooBar>().Use<FooBar>();
}
container.Inject(myObject);
OK, this works only if I'm in the same class, but sometimes you need IContaner
class inside a controller and you create your Container
at project Startup, in this case you can do this:
public MyController(ISession session, IContainer container)
{
_session = session;
_container = container;
}
public void DoSomeStuff()
{
_container.Inject(new FooBar());
}
IContainer
can be injected using your Dependency Resolver. In my case I'm using System.Web.Mvc.DependencyResolver
with a custom StructureMapDependencyResolver
so this DependencyResolver.Current.GetService<IContainer>().Inject(myService);
is possible too.