How loose is loose enough?

In MVP pattern, the challenge part is how to update data back to model/db from view. If view can call the model’s update function directly, it’s so called ‘supervision controller’, as Martin Fowler’s newly suggested. Obviously this might cause tight coupling problem, and also we don’t want both view and presenter can call model’s update function.

If all the update operation have to go through presentor, this pattern becomes to ‘Passive View’. (So nice to know Chinese, Rader’s post explained it very well to me. Though I did read Billy MacCarfferty’s similar one ealier but could not understand completely. )

Now I got it why both JP and Billy adopt DTO in their demo projects. By transfer DTO-only-data between presenter and view, all the update operation will be centralized to presenter then.

Billy’s idea of ‘Update method in view’ might solve my problem: all the update operations have to go through stored procedure which needs both original and new data as calling parameters. Presentor should have no problem to get all of them, old data from model and new data from view.

As other reader pointed out, in Billy sample code, the presenter’s constructor really confused people.

Presenter(commentListing, DaoFactory.GetCommentDao());

Which means presenter needs to instantiate Dao layer stuff? Billy added a SessionFactoryConfigPath parm in DaoFactory’s constructor, this forced me created a dbConfig.cs in data layer and overrode constructor to make web and presenter both db-config-free.

This also reminds me Steven Yang and JP’s code in which they added a service layer or task layer to entity object back to presenter. Like Jeremy’s sample,

Presenter(IWorkItemView view, IWorkItemService service)

JP did the same thing in his MVP sample, without NHibernate, he put connection string in DataAccess layer.

namespace EnterprisePatterns.DataAccess
{
public class DbConnectionFactory : IDbConnectionFactory
{
public IDbConnection Create()
{
IDbConnection connection = DbProviderFactories.GetFactory(“System.Data.SqlClient”).CreateConnection();
//connection.ConnectionString =
// “data source=(local);Integrated Security=SSPI;Initial Catalog=EnterprisePatternsEdmonton”;
connection.ConnectionString =
“data source=’.\\SQLEXPRESS’;Integrated Security=SSPI;Initial Catalog=Northwind”;
return connection;
}
}

I think this is the right place to configure db, should not be in web, or somewhere else.

In fact, in his task layer, the FindById and FindAll / GetList could be done by NHinberat.Generic. We might cut down one extra layer then.

Advertisements

One thought on “How loose is loose enough?

  1. Principles in Action from http://book.javanb.com/better-faster-lighter-java/bfljava-CHP-10-SECT-6.html

    * Keep it simple: domain objects remain unaware of persistence logic, Hibernate manages all configuration
    * Choose the right tools: Hibernate
    * Do one thing, and do it well: the domain model is focused on the business problem, the DAOs focus on data manipulation and are database-agnostic
    * Strive for transparency: domain model is completely unaware of persistence layer
    * Allow for extension: Spring configuration and IoC allow us to change persistence layers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s