将Autofac添加到WPF MVVM应用程序 [英] Adding Autofac to WPF MVVM application
问题描述
我似乎找不到解决此问题的方法.我已经看到了几个与此有关的问题,但没有一个真正给我解决方案.我对Autofac完全陌生,实际上并没有做太多WPF + MVVM,但了解一些基础知识.
I can't seem to find an solution to this problem. I've seen several questions about this, but none really give me a solution. I am totally new to Autofac and haven't really done much WPF + MVVM, but know the basics.
我有一个WPF应用程序(使用ModernUI for WPF),正在尝试将Autofac添加到其中,并且由于无法访问我的视图,因此很难弄清楚如何在所有视图中解析我的服务.容器.我有一个主视图,这是我设置容器的入口点:
I have a WPF application (using ModernUI for WPF) which I'm trying to add Autofac to, and I am having a hard time figuring out how to resolve my services within all the views, since they have no access to my container. I have a main view, which is my entry point, where I set up my container:
public partial class MainWindow : ModernWindow
{
IContainer AppContainer;
public MainWindow()
{
SetUpContainer();
this.DataContext = new MainWindowViewModel();
InitializeComponent();
Application.Current.MainWindow = this;
}
private void SetUpContainer()
{
var builder = new ContainerBuilder();
BuildupContainer(builder);
var container = builder.Build();
AppContainer = container;
}
private void BuildupContainer(ContainerBuilder builder)
{
builder.RegisterType<Logger>().As<ILogger>();
...
}
}
我遇到的问题是弄清楚如何在其他视图中解析记录器和其他服务,在这里我通过ViewModel构造函数注入所有依赖项,如下所示:
The problem I'm having is figuring out how I can resolve my logger and other services within my other views, where I inject all my dependencies through the ViewModel constructor, like so:
public partial class ItemsView : UserControl
{
private ItemsViewModel _vm;
public ItemsView()
{
InitializeComponent();
IFileHashHelper fileHashHelper = new MD5FileHashHelper();
ILibraryLoader libraryLoader = new LibraryLoader(fileHashHelper);
ILogger logger = new Logger();
_vm = new ItemsViewModel(libraryLoader, logger);
this.DataContext = _vm;
}
}
某些视图中有大量荒谬的注入参数,这就是我希望Autofac进入其中并帮助我进行清理的地方.
Some views have a ridiculous amount of injected parameters, and this is where I want Autofac to come in and help me clean things up.
我当时正在考虑将容器传递给ViewModel并将其作为属性存储在ViewModelBase类中,但是我已经读到这将是一种反模式,即使那样我也不知道是否会自动在其他ViewModel中解析我的对象.
I was thinking of passing the container to the ViewModel and storing it as a property on my ViewModelBase class, but I've read that this would be an anti-pattern, and even then I don't know if that would automatically resolve my objects within the other ViewModels.
我设法使用Autofac组合了一个简单的控制台应用程序
I managed to put together a simple Console Application using Autofac
class Program
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterType<Cleaner>().As<ICleaner>();
builder.RegisterType<Repository>().AsImplementedInterfaces().InstancePerLifetimeScope();
var container = builder.Build();
using (var scope = container.BeginLifetimeScope())
{
ICleaner cleaner = container.Resolve<ICleaner>();
cleaner.Update(stream);
}
}
}
但这很简单,因为它只有一个入口点.
but that was simple since it has a single entry point.
我想要一些有关如何将Autofac添加到我的WPF应用程序的想法.我确定我做错了什么.感谢您的帮助.
I'd like some ideas on how to add Autofac to my WPF app. I'm sure that I'm doing something wrong. Your help is appreciated.
推荐答案
在我上面的评论中展开:
Expanding on my comment above:
我在所有WPF MVVM应用程序中都使用了Autofac,我相信它是更好的DI框架之一-这是我的观点,但我认为它是有效的.
I use Autofac with all my WPF MVVM applications, I believe it to be one of the better DI frameworks - this is my opinion, but I think it is valid.
对我来说,也应该99%的时间避免PRISM,这是一个'寻找问题的解决方案',并且由于大多数人没有在WPF中构建可动态组合的运行时解决方案,因此不需要,我相信人们会\会不同意.
Also for me PRISM should be avoided 99% of the time, it's a 'solution looking for a problem' and since most people don't build dynamically composable runtime solutions in WPF it is not needed, i'm sure people would\will disagree.
就像任何架构模式一样,应用程序生命周期也存在设置\配置阶段,仅在显示第一个视图(窗口)之前,以您的情况为例,将为依赖项注入,日志记录,异常完成全部设置处理,分派器线程管理,主题等.
Like any architectural patterns there is a setup\configuration phase to the application life-cycle, put simply in your case before the first View (window) is shown there will be a whole of setup done for Dependency Injection, Logging, Exception Handling, Dispatcher thread management, Themes etc.
我有几个将Autofac与WPF \ MVVM一起使用的示例,下面列出了几个示例,我想说一下Simple.Wpf.Exceptions示例:
I have several examples of using Autofac with WPF\MVVM, a couple are listed below, I would say look at the Simple.Wpf.Exceptions example:
https://github.com/oriches/Simple.Wpf.Exceptions
https://github.com/oriches/Simple.Wpf.DataGrid
https://github.com/oriches/Simple.MahApps.Template
这篇关于将Autofac添加到WPF MVVM应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!