You should pass a container instance in the constructor where you need.
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/structuremap-users/eff18526-a6e7-4e9a-9bbe-25f9aa3f7f62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You should pass a container instance in the constructor where you need.
On Jul 21, 2014 11:55 PM, "David Ames" <david...@gmail.com> wrote:
Hey all,--We were using ObjectFactory to access the default container. EG:If we needed to do Service Location (which we avoided where possible, we would do)public static BreadcrumbTracker ObtainTracker(){return StructureMap.ObjectFactory.Container.GetInstance<BreadcrumbTracker>();}In our StructureMapControllerFactory, we would do something like:protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType){if ((requestContext == null) || (controllerType == null))return null;BController controller = (BController) ObjectFactory.Container.GetInstance(controllerType);Now that ObjectFactory is obsolete, what is the recommended replacement/default? I can't seem to find a static class/method to get at the default container.Cheers,dave
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.
To post to this group, send email to structuremap-users@googlegroups.com.
Thanks Jeremy,
The "static" part is why I'm wanting to remove ObjectFactory because in practice it creates terrible coupling from your application code to ObjectFactory. You can happily take in IContainer as a constructor argument in any class that needs to do service location or use the Container. That technique even has the advantage of getting the right nested or profiled container that created the object if that's in use.For frameworks like MVC, Web API, or our own FubuMVC, you just create the container on the fly in the bootstrapping and ensure that it's disposed on application startup (FubuMVC does this for you).
- Jeremy
On Tuesday, July 22, 2014 9:12:29 AM UTC-5, Luka wrote:
You should pass a container instance in the constructor where you need.
On Jul 21, 2014 11:55 PM, "David Ames" <david...@gmail.com> wrote:
Hey all,--We were using ObjectFactory to access the default container. EG:If we needed to do Service Location (which we avoided where possible, we would do)public static BreadcrumbTracker ObtainTracker(){return StructureMap.ObjectFactory.Container.GetInstance<BreadcrumbTracker>();}In our StructureMapControllerFactory, we would do something like:protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType){if ((requestContext == null) || (controllerType == null))return null;BController controller = (BController) ObjectFactory.Container.GetInstance(controllerType);Now that ObjectFactory is obsolete, what is the recommended replacement/default? I can't seem to find a static class/method to get at the default container.Cheers,dave
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
public class MvcApplication : System.Web.HttpApplication { public IContainer Container { get { return (IContainer)HttpContext.Current.Items["_container"]; } set { HttpContext.Current.Items["_container"] = value; } }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles);
DependencyResolver.SetResolver(new SMDependencyResolver(() => Container ?? ObjectFactory.Container));
ObjectFactory.Configure(cfg => { cfg.AddRegistry(new StandardRegistry()); cfg.AddRegistry(new ControllerRegistry()); cfg.AddRegistry(new ActionFilterRegistry(() => Container ?? ObjectFactory.Container)); cfg.AddRegistry(new MvcRegistry()); cfg.AddRegistry(new TaskRegistry()); cfg.AddRegistry(new ModelMetadataRegistry()); });
using (var container = ObjectFactory.Container.GetNestedContainer()) { foreach (var task in container.GetAllInstances<IRunAtStartup>()) task.Execute(); } }
public void Application_BeginRequest() { //This approach implements ContainerPerRequest, so nested actions use the same controller as the parent Request action // -- the benefit is that a single container allows there to be one DbContext instantiated by the one Container. Container = ObjectFactory.Container.GetNestedContainer(); foreach (var task in Container.GetAllInstances<IRunOnEachRequest>()) task.Execute(); }
public void Application_EndRequest() { try { foreach (var task in Container.GetAllInstances<IRunAfterEachRequest>()) task.Execute(); } finally { Container.Dispose(); Container = null; }
}
public void Application_Error() { foreach (var task in Container.GetAllInstances<IRunOnError>()) task.Execute();
} }
The "static" part is why I'm wanting to remove ObjectFactory because in practice it creates terrible coupling from your application code to ObjectFactory. You can happily take in IContainer as a constructor argument in any class that needs to do service location or use the Container. That technique even has the advantage of getting the right nested or profiled container that created the object if that's in use.For frameworks like MVC, Web API, or our own FubuMVC, you just create the container on the fly in the bootstrapping and ensure that it's disposed on application startup (FubuMVC does this for you).
- Jeremy
On Tuesday, July 22, 2014 9:12:29 AM UTC-5, Luka wrote:
You should pass a container instance in the constructor where you need.
On Jul 21, 2014 11:55 PM, "David Ames" <david...@gmail.com> wrote:
Hey all,--We were using ObjectFactory to access the default container. EG:If we needed to do Service Location (which we avoided where possible, we would do)public static BreadcrumbTracker ObtainTracker(){return StructureMap.ObjectFactory.Container.GetInstance<BreadcrumbTracker>();}In our StructureMapControllerFactory, we would do something like:protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType){if ((requestContext == null) || (controllerType == null))return null;BController controller = (BController) ObjectFactory.Container.GetInstance(controllerType);Now that ObjectFactory is obsolete, what is the recommended replacement/default? I can't seem to find a static class/method to get at the default container.Cheers,dave
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/structuremap-users/7a2e0b22-89b8-41ac-8baa-1bc3dbeaf8eb%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsubscribe@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/structuremap-users/eff18526-a6e7-4e9a-9bbe-25f9aa3f7f62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsubscribe@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/structuremap-users/eff18526-a6e7-4e9a-9bbe-25f9aa3f7f62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
public static class StructureMapObjectFactory
{ private static readonly Lazy<Container
> _containerBuilder = new Lazy<Container>(() => new Container(), LazyThreadSafetyMode.ExecutionAndPublication); public static IContainer Container { get { return _containerBuilder.Value; } } public static void Initialize<T>() where T : Registry, new() { Container.Configure(x => { x.AddRegistry<T>(); }); } }
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/structuremap-users/d00c2859-9dad-4cc5-8a1a-ab36e05db7fe%40googlegroups.com.