Model View Presenter / Passive View Benefits



Model View Presenter / Passive View Benefits

A hot topic near and dear to my heart is Model View Presenter (MVP) or Passive View design pattern.   This design pattern forms a good model for keeping a strong separation between UI logic and business logic.

In the MVP model, the data access objects constitute the model, the View is the UI components kept as simple as they can be and the Presenter is where the business logic is.   These three components interact with other through interfaces to further reinforce the logical separation.  

The presenter should have no idea where the Model gets its data or how the data is persisted.    The Model defines an interface that must be implemented.   Anything that implements this interface can serve as the model.  This means that we can switch out the data storage strategy without having to change anything in the presenter.

The presenter should also have no idea about the nature of the View.    The View implements an interface that exposes all of the methods, properties, and events that the Presenter will need and the Presenter implements the business logic through this interface.   This means that View may actually been a Win Form, Web Form, a server control, a user control, a SharePoint web part, or even a class library with no UI components.   The only requirement is that the View must implement the interface.

Among other things, this gives us these benefits:

·        Simplified testing.   The Presenter can be tested without having to worry about the database or the UI.   Both the Model and View can be replaced with “Mock” objects designed to simplify testing
·        Business logic implemented in the Presenter can easily be retargeted to any number of UIs.    Once the business logic is implemented and tested, the original View can be converted from a Win Form to Web Form or a Mobile Web Form, etc without impacting the Presenter as long as the new Views continue to implement the same interface
·        The Model can be switched out to for demo purposes to read from local flat files or static XML files.   As long as the new Models implement the original interfaces, neither the Presenter nor the Views needs to change.
·        If you have multiple lines of business with similar business logic but very different data sources, new Models can be implemented targeting the new data sources.   As long as the new models implement the original interfaces, neither the Presenter nor the View needs to change.  
Future posts will explore what some of these benefits mean as well as ferret out some additional benefits.

What benefits do you see with the MVP pattern?  What hurdles have you had to face?

 

author: Nick Harrison | posted @ Monday, September 22, 2008 6:44 PM | Feedback (2)

What Can We Do For Meaningful Variable Names?




What can we do for meaningful variable names?

You hear much about naming conventions and some shops still have some arcane rules in place.   Fortunately most of us have stepped beyond Hungarian notation.   I have seen some shops define rules that the method name should include the number of parameters.   I have seen shops define rules that would ban overloading and insist on a sequence number for  methods that should be overloads.   I have seen shops that wanted the Requirement Number embedded in method names, and I have seen shops put maximum character size limits to simplify reading.   This unfortunately led to awkward abbreviations.

None of these standards have led to more meaning variable names.   The fact that a variable is an integer or a string does nothing for me.   Having a method signature come like:

        Public void int  DoSomething (string strParam, int intParam)

does nothing for us.    Having one like:

        Public void int  ProcessIncomingClaims (string branchCode, int transactionID)

Can be very enlightening.

Like legislating morality, none of these rules will guarantee that the names are good.   At the same time, almost any set of “good” rules could still be applied and end up with “bad” variable names.  We may not always know when a name is “good” but we usually know when the name is “bad”.  

Enter GhostDoc.   GhostDoc is a wonderful tool designed to help automate your comments.   Caution it works best for C#, but they are building support for VB.   The intention here is that GhostDoc takes your method, properties, parameters, etc. and splits them to come up with the documentation for that item.   There are several thinks that seem to be happening here.   First it splits the words at a case change.   Second, it interprets methods as being in the form verb – object and will attempt to structure the comment like that.

For instance, if you have a method called DataBind (), GhostDoc will come up with the comment:

/// <summary>

/// Datas the bind.

/// </summary>

 

This is clearly not what we want for a comment.   Note that the naming convention was not followed.   We would want to have methods following the pattern of Vern Object.   Here it is following the convention of Object Verb.   Ghost Doc ends up with a comment that does not make sense grammatically.   Now consider the comment generated for the method   BindData ():

/// <summary>

/// Binds the data.

/// </summary>

Our method name follows the naming conventions that we have setup and now the generated comment makes sense as well.

Try it and you will find that whenever GhostDoc does not come up with a boiler plate comment, your variable names or method names or property names are probably not named well.

Not a bad litmus test for good names!

Just like Ghost Writers in the publishing world, there is only so much that GhostDoc can do.  It does a good job of structuring your comments.  It does a good job of mapping out the details needed for comprehensive XML documentation.   It also does a good job brining in ancestor details when overriding from a base class.  

It does not explain how logic works or why logic is needed.   This you still need to add yourself, but at least you will know where it goes to produce in comprehensive documentation.     The better you name your methods and parameters, the less you should have to write.    The more highly cohesive your methods, the less you should have to write.    The less you have have to write manually, the better your code can be understand without your comments.

Has anyone else used GhostDoc to help ensure naming conventions or help ensure that code is readable?

?


You hear much about naming conventions and some shops still have some arcane rules in place.   Fortunately most of us have stepped beyond Hungarian notation.   I have seen some shops define rules that the method name should include the number of parameters.   I have seen shops define rules that would ban overloading and insist on a sequence number for  methods that should be overloads.   I have seen shops that wanted the Requirement Number embedded in method names, and I have seen shops put maximum character size limits to simplify reading.   This unfortunately led to awkward abbreviations.

None of these standards have led to more meaning variable names.   The fact that a variable is an integer or a string does nothing for me.   Having a method signature come like:

        Public void int  DoSomething (string strParam, int intParam)

does nothing for us.    Having one like:

        Public void int  ProcessIncomingClaims (string branchCode, int transactionID)

Can be very enlightening.

Like legislating morality, none of these rules will guarantee that the names are good.   At the same time, almost any set of “good” rules could still be applied and end up with “bad” variable names.  We may not always know when a name is “good” but we usually know when the name is “bad”.  

Enter GhostDoc.   GhostDoc is a wonderful tool designed to help automate your comments.   Caution it works best for C#, but they are building support for VB.   The intention here is that GhostDoc takes your method, properties, parameters, etc. and splits them to come up with the documentation for that item.   There are several thinks that seem to be happening here.   First it splits the words at a case change.   Second, it interprets methods as being in the form verb – object and will attempt to structure the comment like that.

For instance, if you have a method called DataBind (), GhostDoc will come up with the comment:

/// <summary>

/// Datas the bind.

/// </summary>

 

This is clearly not what we want for a comment.   Note that the naming convention was not followed.   We would want to have methods following the pattern of Vern Object.   Here it is following the convention of Object Verb.   Ghost Doc ends up with a comment that does not make sense grammatically.   Now consider the comment generated for the method   BindData ():

/// <summary>

/// Binds the data.

/// </summary>

Our method name follows the naming conventions that we have setup and now the generated comment makes sense as well.

Try it and you will find that whenever GhostDoc does not come up with a boiler plate comment, your variable names or method names or property names are probably not named well.

Not a bad litmus test for good names!

Just like Ghost Writers in the publishing world, there is only so much that GhostDoc can do.  It does a good job of structuring your comments.  It does a good job of mapping out the details needed for comprehensive XML documentation.   It also does a good job brining in ancestor details when overriding from a base class.  

It does not explain how logic works or why logic is needed.   This you still need to add yourself, but at least you will know where it goes to produce in comprehensive documentation.     The better you name your methods and parameters, the less you should have to write.    The more highly cohesive your methods, the less you should have to write.    The less you have have to write manually, the better your code can be understand without your comments.

Has anyone else used GhostDoc to help ensure naming conventions or help ensure that code is readable?

author: Nick Harrison | posted @ Monday, September 22, 2008 6:43 PM | Feedback (1)