如何从新实例的构造函数解决控制台应用程序中的统一依赖关系
How to resolve unity dependency in console app from constructor of new instance
背景:
到目前为止,我只在 WPF 应用程序中使用 Unity IoC。
我想在控制台应用程序中使用它。
在 WPF 应用程序中运行一些 "magic" 以自动选择新 object/instance.
的正确构造函数
问题:
如何在我的控制台应用程序中使用相同的 "magic"?
static void Main(string[] args)
{
IUnityContainer unitycontainer = new UnityContainer();
// RegisterType of Dependency in container
unitycontainer.RegisterType<IMyDependency,Dependency>(new ContainerControlledLifetimeManager());
// How shall I choose the correct constructor?
// In WPF Application the correct constructor was used automatically
// How the wpf apps use the correct constructor with my dependencies?
var newclass = new newclass();
}
public class NewClass
{
private readonly IMyDependency _dependency;
public NewClass(IMyDependency dependency)
{
_dependency = dependency;
}
}
调用 IUnityContainer.Resolve
实例化 class,这将调用具有已注册依赖项的构造函数。
var newclass = unitycontainer.Resolve<NewClass>();
背景:
到目前为止,我只在 WPF 应用程序中使用 Unity IoC。
我想在控制台应用程序中使用它。
在 WPF 应用程序中运行一些 "magic" 以自动选择新 object/instance.
问题:
如何在我的控制台应用程序中使用相同的 "magic"?
static void Main(string[] args)
{
IUnityContainer unitycontainer = new UnityContainer();
// RegisterType of Dependency in container
unitycontainer.RegisterType<IMyDependency,Dependency>(new ContainerControlledLifetimeManager());
// How shall I choose the correct constructor?
// In WPF Application the correct constructor was used automatically
// How the wpf apps use the correct constructor with my dependencies?
var newclass = new newclass();
}
public class NewClass
{
private readonly IMyDependency _dependency;
public NewClass(IMyDependency dependency)
{
_dependency = dependency;
}
}
调用 IUnityContainer.Resolve
实例化 class,这将调用具有已注册依赖项的构造函数。
var newclass = unitycontainer.Resolve<NewClass>();