Click here to Skip to main content
12,452,344 members (51,496 online)
Click here to Skip to main content
Add your own
alternative version

Stats

20.5K views
13 bookmarked
Posted

Separate Domain from Presentation – part III

, 5 Sep 2009 CDDL
Rate this:
Please Sign up or sign in to vote.
This is a third post in the series of posts about “Separate Domain from Presentation” Refactoring.Previous Posts:Separate Domain from Presentation – part ISeparate Domain from Presentation – part IILast time we explained how to refactor towards MVP – Supervising Controller...
Cross post from IRefactor

This is a third post in the series of posts about “Separate Domain from Presentation” Refactoring.

Previous Posts:
Separate Domain from Presentation – part I
Separate Domain from Presentation – part II

Last time we explained how to refactor towards MVP – Supervising Controller pattern.
We left our project in the following state:
In this post I will complete the required refactoring steps and will suggest more steps to even deepen the separation of UI and BL concerns.

Refactoring Steps:
  • "Extract Interface" – in order to have a better encapsulation and separation of concerns, the CoursesPresenter shouldn’t access CoursesView directly. After all, the only functionality of interest to the CoursesPresenter is the CoursesView Courses property. Therefore, we will extract an interface from CoursesView class as follows: right click on the CoursesView class » Refactor » Extract Interface and select Courses property as shown in the figure below.
  • Compile the Solution and execute the Unit Tests.
  • In the CoursesPresenter class change all the occurrences of CoursesView to ICoursesView.
  • Compile the Solution and execute the Unit Tests.
  • Last time we indicated that the presenter should handle complicated user events by subscribing to the view. After introducing the ICoursesView interface it’s simple. Add to the interface the following code:
event Action LoadCourses;
event Action SaveCourses;
  • Implement the newly added events in the CoursesView class:
public event Action LoadCourses;
public event Action SaveCourses;
  • In the CoursesPresenter class rename the Load and Save methods to LoadCoursesEventHandler and SaveCoursesEventHandler respectively. Use right click » Refactor » Rename tool to rename it easily.
  • Wire-up the events in the CoursesPresenter constructor as follows:
public CoursesPresenter(ICoursesView view)
{
   this.view = view;
   view.LoadCourses += LoadCoursesEventHandler;
   view.SaveCourses += SaveCoursesEventHandler;
}
  • Compile the Solution and execute the Unit Tests.
  • In the CoursesView class add the notification code:
private void NotifyObservers(Delegate del)
{
    Delegate[] observers = del.GetInvocationList();
    foreach (Delegate observer in observers)
    {
        try
        {
            Action action = observer as Action;
            if (action != null)
            {
                action.DynamicInvoke();
            }
        }
        catch
        {
            // graceful degradation.
        }
    }       
}
  • Change the CoursesView.Load and CoursesView.Save methods to call NotifyObservers respectively:
private void FrmMain_Load(object sender, EventArgs e)
{
    //...
    NotifyObservers(LoadCourses);
    //...
}
private void Save()
{
    //...
    NotifyObservers(SaveCourses);
    //...
}
  • Compile the Solution and execute the Unit Tests.
  • Now it is the time to remove all the temporary instantiations of the CoursesPresenter class in the Load and Save methods. Remove all the occurrences.
  • In the Program.cs class instead of Application.Run(new CoursesView()) write the following:
static void Main()
{
   //...
   CoursesView coursesView = new CoursesView();
   CoursesPresenter coursesPresenter = new CoursesPresenter(coursesView);
   Application.Run(coursesView);
}

This concludes the “Separate Domain from Presentation” refactoring.
We ended with the following:
For next possible steps, consider the following:
  • Go over the CoursesView.Designer.cs and remove all the TableAdapter instances.
  • Create DAL and move Save and Load methods further more, from the presenter to the DAL.
  • Create the CoursesView and CoursesPresenter using Abstract Factory or using Dependency Injection.

<!-- codeproject -->

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

Share

About the Author

Uri Lavi
Other
Israel Israel
Uri Lavi is a development lead with extensive experience in Data Intensive, Business Compound, Distributed and Scalable Software Systems. Uri specializes in mentoring, coaching and consulting for complex software engineering topics, among which: Software Architecture, Design Patterns & Refactoring.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Generalwtostore Pin
wtosell9-Dec-09 16:05
memberwtosell9-Dec-09 16:05 
Generalwtostore Pin
wtosell9-Dec-09 16:04
memberwtosell9-Dec-09 16:04 
GeneralFrom chaos to order Pin
einy7-Sep-09 20:58
membereiny7-Sep-09 20:58 
GeneralRe: From chaos to order Pin
lisszhou7-Sep-09 22:24
memberlisszhou7-Sep-09 22:24 
GeneralRe: From chaos to order Pin
lisszhou7-Sep-09 22:25
memberlisszhou7-Sep-09 22:25 
GeneralRe: From chaos to order Pin
Uri Lavi7-Sep-09 22:28
memberUri Lavi7-Sep-09 22:28 
GeneralMy vote of 1 Pin
Ahmed Salah Mozaly5-Sep-09 21:02
memberAhmed Salah Mozaly5-Sep-09 21:02 
GeneralRe: My vote of 1 Pin
Uri Lavi6-Sep-09 0:30
memberUri Lavi6-Sep-09 0:30 
GeneralRe: My vote of 1 Pin
lisszhou7-Sep-09 22:26
memberlisszhou7-Sep-09 22:26 
GeneralRe: My vote of 1 Pin
edhardyhive8-Mar-10 15:10
groupedhardyhive8-Mar-10 15:10 
GeneralRe: My vote of 1 Pin
edhardyhive8-Mar-10 15:10
groupedhardyhive8-Mar-10 15:10 
GeneralRe: My vote of 1 Pin
edhardyhive8-Mar-10 15:11
groupedhardyhive8-Mar-10 15:11 
GeneralRe: My vote of 1 Pin
edhardyhive8-Mar-10 15:12
groupedhardyhive8-Mar-10 15:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 6 Sep 2009
Article Copyright 2009 by Uri Lavi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid