C# 在抽象工厂模式中使用IoC?
在工厂模式中使用IoC容器是一种不好的做法吗?例如:C# 在抽象工厂模式中使用IoC?,c#,ioc-container,factory-pattern,C#,Ioc Container,Factory Pattern,在工厂模式中使用IoC容器是一种不好的做法吗?例如: public interface IDialogService { void RegisterView<TView, TViewModel>(string viewName) where TViewModel : IDialogViewModel where TView : Window; bool? ShowDialog(string viewName, IDialogView
public interface IDialogService
{
void RegisterView<TView, TViewModel>(string viewName)
where TViewModel : IDialogViewModel
where TView : Window;
bool? ShowDialog(string viewName, IDialogViewModel viewModel);
// factory method:
TViewModel CreateDialogViewModel<TViewModel>(string name) where TViewModel : IDialogViewModel;
}
public class DialogService : IDialogService
{
private readonly IUnityContainer _container;
public DialogService(IUnityContainer container)
{
_container = container;
}
#region IDialogService Members
public void RegisterView<TView, TViewModel>()
where TView : Window
where TViewModel : IDialogViewModel
{
RegisterView<TView, TViewModel>("");
}
public void RegisterView<TView, TViewModel>(string viewName)
where TView : Window
where TViewModel : IDialogViewModel
{
if (!_container.IsRegistered<TViewModel>())
_container.RegisterType<TViewModel>();
if (string.IsNullOrEmpty(viewName))
{
viewName = typeof(TView).Name;
}
_container.RegisterType<Window, TView>(viewName);
}
public bool? ShowDialog(string viewName, IDialogViewModel viewModel)
{
var view = _container.Resolve<Window>(viewName);
view.DataContext = viewModel;
view.Owner = Application.Current.MainWindow;
return view.ShowDialog();
}
// factory method:
public TViewModel CreateDialogViewModel<TViewModel>(string name) where TViewModel : IDialogViewModel
{
return _container.Resolve<TViewModel>(name);
}
#endregion
}
公共接口IDialogService
{
无效注册表视图(字符串视图名称)
TViewModel的位置:IDialogViewModel
其中TView:窗口;
bool?ShowDialog(字符串viewName,IDialogViewModel viewModel);
//工厂方法:
TViewModel CreateDialogViewModel(字符串名称),其中TViewModel:IDialogViewModel;
}
公共类DialogService:IDialogService
{
专用只读IUnityContainer\u容器;
公共对话服务(IUnityContainer容器)
{
_容器=容器;
}
#区域IDialogService成员
公共无效注册表视图()
TView:窗口在哪里
TViewModel的位置:IDialogViewModel
{
注册视图(“”);
}
公共无效注册表视图(字符串视图名称)
TView:窗口在哪里
TViewModel的位置:IDialogViewModel
{
if(!\u container.IsRegistered())
_container.RegisterType();
if(string.IsNullOrEmpty(viewName))
{
viewName=typeof(TView).Name;
}
_container.RegisterType(viewName);
}
public bool?ShowDialog(字符串视图名称,IDialogViewModel视图模型)
{
var view=_container.Resolve(viewName);
view.DataContext=viewModel;
view.Owner=Application.Current.main窗口;
返回view.ShowDialog();
}
//工厂方法:
公共TViewModel CreateDialogViewModel(字符串名称),其中TViewModel:IDialogViewModel
{
返回_container.Resolve(名称);
}
#端区
}
我创建工厂方法的原因是因为我的一些IDialogViewModel实现在其构造函数中有很多参数,而且每个对话框在实例化时都必须有新的UnitOfWork
以下是我如何使用它:
public class PeopleMainViewModel : NotificationObject, ...
{
private readonly IDialogService _dialogService = null;
public PeopleMainViewModel(IDialogService dialogService)
{
_dialogService = dialogService;
}
// this method executes by a command.
public void AddPerson()
{
// here is factory method usage. each time PersonDialogViewModel instantiates, a new Unit of work will be created.
var viewModel = _dialogService.CreateDialogViewModel<PersonDialogViewModel>();
// this shows person dialog window to user...
var result = _dialogService.ShowDialog("PersonWindow", viewModel);
if(result == true)
{
//...
}
}
}
公共类PeopleMainViewModel:NotificationObject。。。
{
私有只读IDialogService _dialogService=null;
public PeopleMainViewModel(IDialogService dialogService)
{
_dialogService=dialogService;
}
//此方法通过命令执行。
公众人士()
{
//这里是工厂方法的用法。每次PersonDialogViewModel实例化时,都会创建一个新的工作单元。
var viewModel=_dialogService.CreateDialogViewModel();
//这将向用户显示人员对话框窗口。。。
var result=_dialogService.ShowDialog(“PersonWindow”,viewModel);
如果(结果==真)
{
//...
}
}
}
您应该尽量避免将IoC容器注入工厂,而是查看您的IoC框架是否可以为您生成工厂。然而,这并不总是可能的,在这种情况下,我认为在工厂使用集装箱是可以接受的,只要它永远不会逃逸
就个人而言,我不会将容器放在服务中(如上所述),因为我觉得类现在有两个职责,注入框架已经转移到应用程序逻辑中