views:

37

answers:

1

Hello all,

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 text*Wouldn`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?

A: 

Hi,

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

http://stackoverflow.com/questions/1717621/mvvm-where-to-put-data-access-layer

Unmesh Kondolikar
I still thought about it again and see a flaw in my idea: Having 5000 CustomerViewModel objects each doing in the Add/Save method: OnCustomerAdd(customer) , that way I have to declare 5000 events erhm... gotta read your links LOL now I am confused...
Lisa
Yes .. make your views call appropriate methods on the controller. So when the user action prompts you to save just call save on controller from the view and then the controller will do the data access.
Unmesh Kondolikar
just what I have observed: josh smith has a AddCustomer method(save) in the CustomerViewModel, WAF (WPF Application Framework) has the AddCustomer/AddBook method in the BookController. A View is just a model of the view so it must be passiv concerning inducing data persistance etc... thats the job of the controller. Hm seems I gotta have a deeper look at WAF pretty big app but full with architecture :P
Lisa
@Unmesh Could it be that I have to put the CRUD-ICommands + the Actions/Methods like Add/Del etc of my ViewModels into my Controller? That I could do this in the controller: myNewCustomerViewModel.AddCustomerDelegate += new Action<Customer>(OnAddCustomer); That looks good for me. In the end I have ViewModels whose state concerning the graphical representation/gui logic is driven by the controller. Would make much more sense to me. Hell I really dislike josh smith msdn article... not really production ready at all...
Lisa
Hell... I made a mistake. The CustomerViewModel (josh smith) is a detail view with SAVE button. The "DocumentListViewModel" (WAF) is a List view therefore it can have/must a del/add button/command. A detail view can not be deleted... only you work with that workspace crap nobody wants being used to winforms user interfaces....
Lisa