如何配置依赖注入只允许 class 的一个实例?
How to configure dependency injection to allow only one instance of a class?
在一次采访中有人问我这个问题。
我们在项目中使用统一容器进行依赖注入。使用 Bootstrapper.Initialise();
在 Globals.asax
中初始化
我的DI
public class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
IUnityContainer container = new UnityContainer();
RegisterService(container);
return container;
}
public static void RegisterService(IUnityContainer container)
{
container.RegisterType<ILibraryService, LibraryService>();
}
}
有两个问题。
- DI 在整个项目中被初始化了多少次?
- 如何配置依赖注入只允许class的一个实例?
这是很基本的问题,我把他们搞得一团糟。你能为他们建议一些答案吗?
项目环境为VS2015,C#,MVC 5,Unity,Repository pattern,EF.
假设我们必须传递 person 对象,然后使用 DI 传递 PersonWrapper 对象,然后使用方法 GetPerson() 获取单例 thePerson
public class PersonWrapper
{
private static Person thePerson = null;
public static GetPerson
{
get
{
if(thePerson==null) { thePerson = new Person();}
return thePerson;
}
}
}
1. DI容器会在应用程序启动时初始化一次(Application_Start()
事件在Global.asax).
2. 他们问的是 ContainerControlledLifetimeManager
有一个 RegisterType
方法的重载,它接受类型为 LifetimeManager 的实例的参数,因此该实例被创建一次并且每次都会使用该实例,它不会每次都创建新对象来解决依赖关系现在:
container.RegisterType<ILibraryService, LibraryService>(new ContainerControlledLifetimeManager());
来自 MSDN:
ContainerControlledLifetimeManager which registers an existing object as a singleton instance. For this lifetime manager Unity returns the same instance of the registered type or object each time you call the Resolve or ResolveAll method or when the dependency mechanism injects instances into other classes. This lifetime manager effectively implements a singleton behavior for objects.
您可以在 this MSDN link
阅读更多关于 Life Time Manager 的信息
在一次采访中有人问我这个问题。
我们在项目中使用统一容器进行依赖注入。使用 Bootstrapper.Initialise();
Globals.asax
中初始化
我的DI
public class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
IUnityContainer container = new UnityContainer();
RegisterService(container);
return container;
}
public static void RegisterService(IUnityContainer container)
{
container.RegisterType<ILibraryService, LibraryService>();
}
}
有两个问题。
- DI 在整个项目中被初始化了多少次?
- 如何配置依赖注入只允许class的一个实例?
这是很基本的问题,我把他们搞得一团糟。你能为他们建议一些答案吗? 项目环境为VS2015,C#,MVC 5,Unity,Repository pattern,EF.
假设我们必须传递 person 对象,然后使用 DI 传递 PersonWrapper 对象,然后使用方法 GetPerson() 获取单例 thePerson
public class PersonWrapper
{
private static Person thePerson = null;
public static GetPerson
{
get
{
if(thePerson==null) { thePerson = new Person();}
return thePerson;
}
}
}
1. DI容器会在应用程序启动时初始化一次(Application_Start()
事件在Global.asax).
2. 他们问的是 ContainerControlledLifetimeManager
有一个 RegisterType
方法的重载,它接受类型为 LifetimeManager 的实例的参数,因此该实例被创建一次并且每次都会使用该实例,它不会每次都创建新对象来解决依赖关系现在:
container.RegisterType<ILibraryService, LibraryService>(new ContainerControlledLifetimeManager());
来自 MSDN:
ContainerControlledLifetimeManager which registers an existing object as a singleton instance. For this lifetime manager Unity returns the same instance of the registered type or object each time you call the Resolve or ResolveAll method or when the dependency mechanism injects instances into other classes. This lifetime manager effectively implements a singleton behavior for objects.
您可以在 this MSDN link
阅读更多关于 Life Time Manager 的信息