创建具有ApplicationService

时间:2016-04-06 09:23:43

标签: c# mvvm domain-driven-design

在我的Application Serrvices中,我从视图使用的域实体创建ViewModels。

我的问题是ViewModel应该如何调用方法Application Services。 在我的代码中,ViewModel尝试在BooApplicationService中调用一个方法,但我不知道应该如何注入该对象。

public class FooApplicationService
{
    private readonly IFooRepository _repository;

    public MyFooApplicationService(IFooRepository repository)
    {
        _repository = repository;
    }

    public IEnumerable<FooViewModel> GetAllFoo()
    {
        IEnumerable<FooEntity> foo = _repository.GetAllFoo();
        return foo.Select(x=>new FooViewModel(x));
    }
}

public class FooViewModel :ViewModelBase
{
    //****************
    //How do I initiate this?
    private readonly BooApplicationService _booApplicationService;
    //****************

    public FooViewModel(FooEntity entity)
    {
        Id = entity.Id;
        Name = entity.Name;

        ButtonClick = new RelayCommand(ExecuteButtonClick);
    }

    public int Id {get;}
    public string Name {get;set;}
    public ICommand ButtonClick { get; }

    private void ExecuteButtonClick()
    {
        _booApplicationService.DoSomething();
    }
}

我的想法是将构造函数中的BooApplicationService注入MyFooApplicationService,然后使用GetAllFoo中的public class FooApplicationService { private readonly IFooRepository _repository; private readonly IBooApplicationService _booApplicationService public MyFooApplicationService(IFooRepository repository, IBooApplicationService booApplicationService) { _repository = repository; _booApplicationService = booApplicationService; } public IEnumerable<FooViewModel> GetAllFoo() { IEnumerable<FooEntity> foo = _repository.GetAllFoo(); return foo.Select(x=>new FooViewModel(_booApplicationService, x)); } } 。见例:

:-

但我认为它违反了SRP。如果我进一步在ViewModel中进行更改以使其获得更多依赖关系,我必须在应用程序服务中添加这些依赖项,这会使其更加复杂。

0 个答案:

没有答案