从控制器或视图模型调用存储库的 CRUD 方法?
这就是 josh smith 执行添加客户程序的方式:
**CustomerViewModel**.cs:
public void Save()
{
_customerRepository.AddCustomer(_customer);
}
**CustomerRepository**.cs:
public void AddCustomer(Customer customer)
{
//...
_customers.Add(customer);
if (this.CustomerAdded != null)
this.CustomerAdded(this, new CustomerAddedEventArgs(customer));
}
**AllCustomersViewModel**.cs(acts as Controller):
void OnCustomerAddedToRepository(object sender, CustomerAddedEventArgs e)
{
var viewModel = new CustomerViewModel(e.NewCustomer, _customerRepository);
this.AllCustomers.Add(viewModel);
}
strong text这样做不是更好吗?:
**CustomerViewModel**.cs:
public void Save()
{
if (this.CustomerAdded != null)
this.CustomerAdded(this, new CustomerAddedEventArgs(customer));
}
AllCustomersViewModel.cs(acts as Controller):
void OnCustomer**ADDING**ToRepository(object sender, CustomerAddedEventArgs e)
{
_customerRepository.Add(e.NewCustomer);
var viewModel = new CustomerViewModel(e.NewCustomer);
this.AllCustomers.Add(viewModel);
}
CustomerViewModel.cs 中的此步骤也可以在控制器中,因为控制器持有 1/所有对客户/产品/订单的服务/存储库的引用...
if (this.IsNewCustomer)
_customerRepository.AddCustomer(_customer);
当我现在仍然有一个为同一控制器工作的订单/ProductViewModel 时,我有 3 个存储库实例。如果存储库位于控制器的 Ctor 实例化中,我只有一个实例。
使用 josh smith 架构,您在控制器和 CustomerViewModel 中有一个 customerRepo。
根据我的想法,控制器中只有一个 customerRepo,并且 CustomerViewModel 的 Save/Add 方法可以订阅控制器的 OnAddCustomer 方法。
为什么约什·史密斯选择了 a
public event EventHandler<CustomerAddedEventArgs> CustomerAdded;
而不是 a
simple public Action<Customer> AddDocumentDelegate;
你觉得呢?你觉得我的想法有什么缺点吗?
thats the way josh smith is doing the add-a-customer-procedure:
**CustomerViewModel**.cs:
public void Save()
{
_customerRepository.AddCustomer(_customer);
}
**CustomerRepository**.cs:
public void AddCustomer(Customer customer)
{
//...
_customers.Add(customer);
if (this.CustomerAdded != null)
this.CustomerAdded(this, new CustomerAddedEventArgs(customer));
}
**AllCustomersViewModel**.cs(acts as Controller):
void OnCustomerAddedToRepository(object sender, CustomerAddedEventArgs e)
{
var viewModel = new CustomerViewModel(e.NewCustomer, _customerRepository);
this.AllCustomers.Add(viewModel);
}
strong textWouldn`t it be better to do this?:
**CustomerViewModel**.cs:
public void Save()
{
if (this.CustomerAdded != null)
this.CustomerAdded(this, new CustomerAddedEventArgs(customer));
}
AllCustomersViewModel.cs(acts as Controller):
void OnCustomer**ADDING**ToRepository(object sender, CustomerAddedEventArgs e)
{
_customerRepository.Add(e.NewCustomer);
var viewModel = new CustomerViewModel(e.NewCustomer);
this.AllCustomers.Add(viewModel);
}
This step in the CustomerViewModel.cs could also be in the Controller because the Controller holds 1/all refererence(s) to the Service/Repository of the Customer/Product/Order etc...
if (this.IsNewCustomer)
_customerRepository.AddCustomer(_customer);
When I have now still a Order/ProductViewModel working for the same controller I have 3 instances of the repository. If the repository would be in the controller`s Ctor instantiated I have only ONE instance.
With josh smith architecture you have a customerRepo in the Controller AND CustomerViewModel.
With my idea you have only ONE customerRepo in the controller AND the CustomerViewModel`s Save/Add method could be subscribed to the Controllers OnAddCustomer method.
Why did Josh smith took a
public event EventHandler<CustomerAddedEventArgs> CustomerAdded;
and not a
simple public Action<Customer> AddDocumentDelegate;
What do you think? Do you see any disadvantage in my idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的想法是对的。 viewModel 应该是模型的视图特定表示,它不应该调用您的存储库。您的控制器可以监听视图中的事件(按钮单击、提交等),然后调用存储库。
您应该查看有关此主题的其他帖子 -
http://www .weask.us/entry/mvvm-put-data-access-layer
MVVM 将数据访问层放在哪里?
Your idea is right. The viewModel should be a view specific representation of your model and it should not call your repositories. Your controller could listen on the events from view (button click, submit etc) and then call the repositories.
There are other posts on this topic you should check -
http://www.weask.us/entry/mvvm-put-data-access-layer
MVVM where to put Data Access Layer?