2 Replies - 449 Views - Last Post: 13 February 2013 - 07:34 AM Rate Topic: -----

#1 plywoods  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-February 13

Events in MVC question!

Posted 13 February 2013 - 01:06 AM

Hello,

I need some help. I am newcomer in C#.

The application is the PhoneBook. All the data serializable and stored in the List<>. Ithe main idea is to realize MVC pattern.

The question is how to implement events for deleting and editing contacts? Event for adding a contact is already exist and works.

To delete contact - inside the buttonDeleteContact_Click handler I need to get a selected object, than wrap it in DeletePersonEventArgs
and call the event (identical to AddClicked) DeleteClicked that will contail a code that delete a contact.

1. How to describe a type of Delete event ?
internal class DeletePersonEventArgs : EventArgs


2. What should be in
buttonDeleteContact_Click handler?


3. What should be inside controller's method that handles the DeleteClicked event?

void form_DeleteClicked(object sender, DeletePersonEventArgs e)


The same issues for Update code.

Could you please provide a code example for this events and help me to realize this logic.

The source code attached - VS 2012

Thank you in advance.

Here is the link for the solution
https://skydrive.liv...AIbigiVNGjyCQ9w

Is This A Good Question/Topic? 0
  • +

Replies To: Events in MVC question!

#2 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: Events in MVC question!

Posted 13 February 2013 - 02:11 AM

I suggest you build a universal method (function) that deletes a person, where the arguments contain information that is needed to delete a person.

Then from where ever you want to delete a person, you call that function. Instead of raising an event.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,480
  • Joined: 05-May 12

Re: Events in MVC question!

Posted 13 February 2013 - 07:34 AM

Ah, but the question still arises: where to implement that universal method? In the controller class, or in the model class? If in the model class, then you just broke the MVC pattern because the view is not supposed to know anything about the model. If in the controller, you again break MVC because the view is also not supposed to know anything about the controller either.

So in keeping with the MVC pattern, the OP has the choice of either raising an event, or just give up and not follow the MVC pattern.

To stick with the MVC pattern, the controller will need to register for the Form.DeleteClicked event, and within that event handler, the controller will either get as an event arg the currently selected item, or ask the view for the current selected item, and then pass on the information to a call to the model to delete the item. After the delete call on the model, the controller should go tell the view to refresh itself with the updated model.

If that seems like a lot of jumping through hoops, that's the price of following the MVC pattern. There's similar kind of series of hoops I need to jump through if I were to also strictly follow an N-tier programming paradigm.

This post has been edited by Skydiver: 13 February 2013 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1