Wednesday, April 11, 2007

Presenter First #2 (Application Data)

Normally there are 3 data states in Presenter First design.

  • Record State. The persistent data store residing in the Data Layer. This could include RDBMS, XML, or text flat files.
  • Session State. Stored in-memory within application. Usually Data Transfer Objects.
  • Screen State. Data stored in the view.
It is very important the Session State and the Screen State remain synchronized. That is where the presenter object comes into play. The session data becomes dirty and generates an event to the Presenter. In my implementation the Presenter would then query the view for actiontype, check if view is in dirty state, if dirty retrieve DTO from view. Presenter would then pass DTO to the Domain Service to save the DTO information. Code Example follows:
public class ReformatAction : CommandAction
public ReformatAction(ViewAggregator viewAggregator,
 DomainService domainService) : base(viewAggregator, domainService){}

public override void Execute()
     string formatModuleName = ViewAggregator.GetSelectedModuleName();
        ModuleDTO moduleDTO = ViewAggregator.GetModule();

        if (ViewAggregator.IsDirty)
This code example is the wiring between the view and domain. It is a command object. It pulls the module name from the view, pulls the module, if view is dirty then save module, reload saved module to view. The formating comes from where? See what happens in the service:
      public void SetModule(ModuleDTO module)
          mModuleTable[module.GetModuleName()] = module.GetBody();
So we see that it is all done in the service. But you say your unformatting and saving the unformatted int the module table. Correct. Look at this piece:
        public string GetBody()
          ModuleUnformatter mu = new ModuleUnformatter(mBody);

          return mBody = mu.Module;

      public string GetFormattedBody()
          ModuleFormatter mf = new ModuleFormatter(mBody);
          return mf.Module;

Ask yourself why didn’t I just transfer the formatted directly into the text box…

Next Post #3: How to utilize command pattern so that the Presenter holds no instance of Domain or View and is nothing but command messages and event messages.

Post #4: Either Mocking and Unit Testing Presenter First or Event Handling.

No comments: