我只是在编写一个实现 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;
}
}
}
现在我有几个问题:
1.)我应该从哪里调用这个类?app.xaml.cs 做 wpf 的东西?
2.) 我应该注册服务吗,如果是,我应该在哪里注册?
3.) 当我对服务“ICustomerService”进行延迟初始化时,为什么要为它创建一个 Register(T 服务) 方法?那是双重工作。
4.) 我应该去找服务定位器吗?
更新
目前我觉得我必须为我的个人目的强奸一个 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();
}
}
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
}
在另一个 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 );
}
}
// ...
}
PeriodViewModel.cs => 我的 DataGrid 中的每个 DataRow 都有一个 Period 并且 Period 有一个特定的单元格数据模板化到 DocumentListViewModel - Period 1 有 N Documents 是关系仅供参考......所以 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);
}
//...
}
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>();
}
// ...
}
总结问题:您是否看到从顶部级联服务的对象链:
MainViewodel -> LessonPlannerDailyViewModel -> PeriodViewModel -> DocumentListViewModel
我需要级联它们,因为如果我不使用静态服务定位器,我只能确保在级联服务时拥有一个服务实例......
这里的 DI 工具如何帮助我按照 MVVM 模式具体执行 wpf 应用程序?