使用 Unity 进行桌面应用程序依赖注入
Desktop Application Dependency Injection with Unity
我使用了 MVC Web App 的 unity。
在 MVC 中我们有 app_start 方法。
我们使用
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
这会将参数发送到控制器构造函数。
现在我正试图找到如何将此模式实现到 DesktopApplication。
你们知道我们使用 new Form1().Show(); //bla bla foo
当我创建一个新表单时,当我创建一个 System.Windows.Forms 类型的新实例时,它会自动将参数发送给构造函数。
对不起我的语言。
现在我正在使用类似的东西并询问它是否是更好的解决方案:
public partial class frm_FirmaSecimi : XtraForm
{
private IFirmaService _firmaService;
public frm_FirmaSecimi()
{
InitializeComponent();
_firmaService = UnityConfig.Container.Resolve<IFirmaService>();
}
}
有没有办法把它变成:
public partial class frm_FirmaSecimi : XtraForm
{
private IFirmaService _firmaService;
public frm_FirmaSecimi(IFirmaService firmaService)
{
InitializeComponent();
_firmaService = firmaService;
}
}
顺便说一下,这是一个 DevExpress 表单。
感谢解答。
尝试为您的表单引入接口,然后在每个表单上使用它:
public class FormMain : IFormMain
{
ISubForm _subForm;
public FormMain(ISubForm subForm)
{
InitializeComponent();
_subForm = subForm;
}
...
}
现在,在程序启动时,创建 Unity 容器,并解决您的 IFormMain
依赖关系:
private static void Main()
{
var container = BuildContainer();
Application.Run(container.Resolve<IFormMain>());
}
public static IUnityContainer BuildContainer()
{
var currentContainer = new UnityContainer() ;
currentContainer.RegisterType<IFormMain, FormMain>();
// note: registering types could be moved off to app config if you want as well
return currentContainer;
}
显然您的实施看起来不会完全像这样,但它应该为您指明正确的方向。
免责声明:我根本不使用 WinForms,因此以上可能不是应用程序的最佳解决方案,具体取决于它的复杂性等。
也试着阅读这篇文章,它可能会有所帮助:http://foyzulkarim.blogspot.co.nz/2012/09/using-dependency-injection-unity-to.html
我使用了 MVC Web App 的 unity。 在 MVC 中我们有 app_start 方法。 我们使用
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
这会将参数发送到控制器构造函数。 现在我正试图找到如何将此模式实现到 DesktopApplication。
你们知道我们使用 new Form1().Show(); //bla bla foo 当我创建一个新表单时,当我创建一个 System.Windows.Forms 类型的新实例时,它会自动将参数发送给构造函数。
对不起我的语言。 现在我正在使用类似的东西并询问它是否是更好的解决方案:
public partial class frm_FirmaSecimi : XtraForm
{
private IFirmaService _firmaService;
public frm_FirmaSecimi()
{
InitializeComponent();
_firmaService = UnityConfig.Container.Resolve<IFirmaService>();
}
}
有没有办法把它变成:
public partial class frm_FirmaSecimi : XtraForm
{
private IFirmaService _firmaService;
public frm_FirmaSecimi(IFirmaService firmaService)
{
InitializeComponent();
_firmaService = firmaService;
}
}
顺便说一下,这是一个 DevExpress 表单。 感谢解答。
尝试为您的表单引入接口,然后在每个表单上使用它:
public class FormMain : IFormMain
{
ISubForm _subForm;
public FormMain(ISubForm subForm)
{
InitializeComponent();
_subForm = subForm;
}
...
}
现在,在程序启动时,创建 Unity 容器,并解决您的 IFormMain
依赖关系:
private static void Main()
{
var container = BuildContainer();
Application.Run(container.Resolve<IFormMain>());
}
public static IUnityContainer BuildContainer()
{
var currentContainer = new UnityContainer() ;
currentContainer.RegisterType<IFormMain, FormMain>();
// note: registering types could be moved off to app config if you want as well
return currentContainer;
}
显然您的实施看起来不会完全像这样,但它应该为您指明正确的方向。
免责声明:我根本不使用 WinForms,因此以上可能不是应用程序的最佳解决方案,具体取决于它的复杂性等。
也试着阅读这篇文章,它可能会有所帮助:http://foyzulkarim.blogspot.co.nz/2012/09/using-dependency-injection-unity-to.html