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 service) メソッドを作成する必要があるのでしょうか? それは二重の仕事です。
4.) サービスロケータを取得する必要がありますか?
アップデート
現時点では、個人的な目的のためにDIツールをレイプする必要があると感じています=>
App.xaml.cs => ここで、MainWindow を作成し、そのデータ コンテキストを 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 => ここで、LessonPlannerDailyViewModel や LessonPlannerWeeklyViewModel などの特定の Controller/ViewModel に必要なデータをプリロード/セットアップします...
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
}
もう 1 つの 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 には期間があり、期間には DocumentListViewModel にテンプレート化された特定のセル データがあります - 期間 1 には N ドキュメントがあります。
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
静的サービスロケーターを使用していない場合、サービスをカスケードするときにサービスのインスタンスを1つしか確保できないため、それらをカスケードする必要があります...
ここのDIツールは、MVVMパターンに従ってwpfアプリを具体的に実行するのにどのように役立ちますか?