msf*_*boy 3 c# dependency-injection service-locator
我只是在编写一个实现ServiceLocator模式的类.
public class ServiceFactory : IServiceFactory
{
private IDictionary<Type, object> instantiatedServices;
public ServiceFactory()
{
instantiatedServices = new Dictionary<Type, object>();
}
public T GetService<T>() where T : class, new()
{
if (this.instantiatedServices.ContainsKey(typeof(T)))
{
return (T)this.instantiatedServices[typeof(T)];
}
else
{
T service = new T();
instantiatedServices.Add(typeof(T), service);
return service;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有几个问题:
1.)我应该从哪里打电话给这个班级?app.xaml.cs做wpf的东西?
2.)我应该注册服务,如果是,我应该在哪里注册?
3.)当我对服务"ICustomerService"进行延迟初始化时,为什么我应该为它创建一个Register(T服务)方法呢?这是双重工作.
4.)我应该去找服务定位器吗?
UPDATE
目前我觉得我必须为我个人目的强奸DI工具=>
App.xaml.cs =>这里我创建MainWindow并将其datacontext设置为MainViewModel.cs
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
var mainVM = new MainViewModel();
var mainWindow = new MainWindow();
mainWindow.DataContext = mainVM;
mainWindow.ShowDialog();
}
}
Run Code Online (Sandbox Code Playgroud)
MainViewModel.cs =>这里我预装/设置我需要的某些Controller/ViewModel的数据,如LessonPlannerDailyViewModel或LessonPlannerWeeklyViewModel等...
public class MainViewModel : SuperViewModel
{
private LightCommand _newSchoolYearWizardCommand;
private LightCommand _showSchoolclassAdministrationCommand;
private LightCommand _showLessonPlannerDailyCommand;
private LightCommand _showLessonPlannerWeeklyCommand;
private LightCommand _openSchoolYearWizardCommand;
private SuperViewModel _vm;
private FadeTransition _fe = new FadeTransition();
private readonly IMainRepository _mainService;
private readonly ILessonPlannerService _lessonPlannerService;
private readonly IAdminService _adminService;
private readonly IDocumentService _documentService;
private readonly IMediator _mediator;
private readonly IDailyPlanner _dailyVM;
private readonly IWeeklyPlanner _weeklyVM;
private SchoolclassAdministrationViewModel _saVM;
public MainViewModel()
{
// These are a couple of services I create here because I need them in MainViewModel
_mediator = new Mediator();
_mainService = new MainRepository();
_lessonPlannerService = new LessonPlannerService();
_adminService = new AdminService();
_documentService = new DocumentService();
this._mediator.Register(this);
InitSchoolclassAdministration();
}
//... Create other ViewModel/Controller via button commands and their execute method
}
Run Code Online (Sandbox Code Playgroud)
在另一个ViewModel上是LessonPlannerDailyViewModel.cs =>这里我创建了一个可绑定的PeriodViewModel对象集合,它们为构造函数提供了一些服务.在下面的代码之后的下一段看到由ONE PeriodViewModel创建的DocumentListViewModel.cs,它再次获取服务 - 与我在MainViewModel中创建的相同... -
public class LessonPlannerDailyViewModel : LessonPlannerBaseViewModel, IDailyPlanner
{
private ILessonPlannerService _lpRepo;
private IMainRepository _mainRepo;
private IMediator _mediator;
private IDocumentService _docRepo;
private ObservableCollection<PeriodViewModel> _periodListViewModel;
private LightCommand _firstDateCommand;
private LightCommand _lastDateCommand;
private LightCommand _nextDateCommand;
private LightCommand _previousDateCommand;
public LessonPlannerDailyViewModel(IMediator mediator, ILessonPlannerService lpRepo, IMainRepository mainRepo, IDocumentService docRepo)
{
_mediator = mediator;
_lpRepo = lpRepo;
_mainRepo = mainRepo;
_docRepo = docRepo;
_mediator.Register(this);
SchoolYear schoolyear = _mainRepo.GetSchoolYear();
MinDate = schoolyear.Start;
MaxDate = schoolyear.End;
SelectedDate = DateTime.Now;
}
private void LoadLessonPlannerByDay(DateTime data)
{
_periodListViewModel = new ObservableCollection<PeriodViewModel>();
_lpRepo.GetLessonPlannerByDay(data).ForEach(p =>
{
_periodListViewModel.Add(new PeriodViewModel(p, _lpRepo, _docRepo));
});
PeriodListViewModel = _periodListViewModel;
}
private DateTime _selectedDate;
public DateTime SelectedDate
{
get { return _selectedDate; }
set
{
if (_selectedDate.Date == value.Date)
return;
_selectedDate = value;
this.RaisePropertyChanged("SelectedDate");
LoadLessonPlannerByDay( value );
}
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
PeriodViewModel.cs =>我的DataGrid中的每个DataRow都有一个Period和一个Period有一个dattablemplated到DocumentListViewModel的特定单元格 - Period 1有N Documents是关系FYI ...所以PeriodViewModel创建一个DocumentListViewModel.
public class PeriodViewModel : SuperViewModel
{
private Period _period;
private ILessonPlannerService _lpRepo;
public PeriodViewModel(Period period, ILessonPlannerService lpRepo, IDocumentService docRepo)
{
_period = period;
_lpRepo = lpRepo;
// Update properties to database
this.PropertyChanged += (o, e) =>
{
switch (e.PropertyName)
{
case "Homework": _lpRepo.UpdateHomeWork(PeriodNumber, LessonDayDate, Homework); break;
case "Content": _lpRepo.UpdateContent(PeriodNumber, LessonDayDate, Content); break;
}
};
Documents = new DocumentListViewModel(_period.Id, period.Documents, docRepo);
}
//...
}
Run Code Online (Sandbox Code Playgroud)
DocumentListViewModel.cs =>这里我设置了添加/删除/打开文档的命令,这可以通过documentService/documentRepository来完成
public class DocumentListViewModel : SuperViewModel
{
private LightCommand _deleteDocumentCommand;
private LightCommand _addDocumentCommand;
private LightCommand _openDocumentCommand;
private int _parentId;
private readonly IDocumentService _documentService;
public DocumentListViewModel(int parentId,ObservableCollection<Document> documents, IDocumentService documentService)
{
_parentId = parentId;
_documentService = documentService;
DocumentList = documents;
SelectedDocuments = new ObservableCollection<Document>();
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
总结问题:你是否看到从顶部层叠服务的对象链:
MainViewodel - > LessonPlannerDailyViewModel - > PeriodViewModel - > DocumentListViewModel
我需要级联它们,因为如果我不使用静态服务定位器,我只能确保在级联服务时有一个服务实例...
这里DI工具如何帮助我具体做一个WPF应用程序MVVM模式下?
第四个问题是最容易回答的问题:不,你根本不应该选择Service Locator,因为它是一种反模式.
那么替代方案是什么?使用Register Resolve Release模式.这应该是回答其他问题的良好起点.