MVC is a fundamental design pattern for the separation of user interface logic from business logic. The pattern separates the modeling of the application domain, the presentation of the application, and the actions based on user input into three distinct objects:
View - Forms, SmartParts, and user controls play the role of a view
Controllers - classes implement controllers
Model - state and other data takes the form of the model.
To build a controller class
- Create a new class in your application and modify the class declaration to inherit from Microsoft.Practices.CompositeUI.Controller.
- Implement your own methods to control the view.
- If you require data to be stored in the WorkItem shared state, use the State property of the base class, or the State attribute, as shown in the following code.
[State("Customer")]
public Customer customer
{
get { return (Customer)State["Customer"]; }
set { State["Customer"] = value; }
}
To build a view class
- Create a form, SmartPart, or user control to display information to the user.
- Create a class to manage the data displayed in the view. For example, you can create a Customer object that has fields such as firstName and lastName. It is also common to use the DataSet for simpler applications. You can use the State attribute to keep data in a common place within the class.
- Add the [CreateNew] attribute to inject an instance of the dependent class through a property, as shown in the following code.
private MyControllerClass myController;
[CreateNew]
public MyControllerClass Controller
{
set { myController = value; }
}
To implement the MVC pattern
- Create a WorkItemas shown in the following code.
public class MyWorkItem : WorkItem
{
} - Override the OnRunStarted Method of the WorkItem to create an instance of the view, as shown in the following code. This automatically creates or retrieves the corresponding controller instance.
protected override void OnRunStarted()
{
base.OnRunStarted();
SampleView view = this.Items.AddNew();
workspace.Show(view);
}
No comments:
Post a Comment