Autofac in web applications, where should I store the container for easy access?

前端 未结 5 1069
难免孤独
难免孤独 2020-12-08 23:16

I\'m still pretty new to using Autofac and one thing I miss in the documentation and examples is how to make it easy to get to the configured container from different places

5条回答
  •  被撕碎了的回忆
    2020-12-08 23:39

    Having IOC container globally available is not a best practice. Even passing container is not encouraged.

    If dependency injection can not be used (you need to create\request objects after component has been created) then you can:

    1. Use hand-coded factories (factory is injected to the component and component uses factory to create other objects)
    2. Use Autofac delegate factories or new auto-generated factories in Autofac 2.

提交回复
热议问题