Search code examples
asp.netdesign-patternswebformsmvp

MVP pattern with webforms and .ascx controls


Let's say I have a domain object called a TrainingPlan. The TrainingPlan object is composed of other domain objects, like so:

public class TrainingPlan
{
    public DelegateInfo DelegateInfo { get; set; }   // name, assessor, course name
    public IEnumerable<Test> Tests { get; set; }     // tests delegate undertook
    public IEnumerable<Comment> Comments { get; set; } // comments by assessor
} 

I have created 3 .ascx controls:

DelegateInfo.ascx

Test.ascx (to display info for a single Test domain object)

Comment.ascx (to display info for a single Comment domain object)

Each ascx control is declared with a view:

 public partial class TestControl : System.Web.UI.UserControl, ITestView
 {     
     // ... implementation of view interface here. Simple properties
 }

My presenters are middle man between view and domain object:

public class TestPresenter: ITestPresenter
{
    ITestView _view;
    ITest _domainObj;   

    // Test object in domain implements ITest
    public TestPresenter(ITestView view, ITest domainObj)
    {
        // removed: checks for null etc

        _view = view;
        _domainObj = domainObj;
    }

    // etc etc

 } 

The domain object is the object that is going to supply the data to the presenter, and receive updated input from the view via the presenter. It will validate the data (hence why its in the domain layer.)

Questions:

  1. Is the presenter speaking directly to a domain object correct in MVP? I have Googled and in the samples presenters access the model directly, but what about a domain object with inbuilt business logic?

  2. Is the logic below in keeping with the MVP pattern?

// Page_Load of PageThatDisplaysTrainingPlan.aspx:  
// load full trainingPlan object  
var trainingPlan = repository.LoadTrainingPlan(idOfPlan);  
// populate test controls  
foreach(var test in trainingPlan.Tests)  
{  
    // create test.acx control on this line. Code not added as it is unnecessary  
    var pres = new TestPresenter(controlJustAdded, test);                   
    //  ... add pres to some collection somewhere  
}

Any advice or links to sample code welcome.


Solution

  • Yes, in MVP pattern the presenter working directly with model, it's absolutely normal. And a domain object with inbuilt business logic it's normal too.

    About your second question - TestPresenter must be responsible for loading trainingPlan object from database and also for updating UI with newly created controls. Seems like you have put this code into Page_Load event handler, I think this is not good idea, it's looking like SmartUI antipattern.