Monday, August 17, 2009

Bend Xaf Applications with Model Artifact State Module

How many times have you said “What a great pattern MVP is!!!” Look for example how is implement the hint engine of main Xaf demo. I will try to explain that implementation.

Controllers

There is a windowcontroller called AdditionalInfoController and from it 2 controllers derive WebShowAdditionalInfoController and WinShowAdditionalInfoController.

What they do? they enable inserting any kind of control as the first control of a view. They are the engine, they implement a behaviour and they also serve as the Observer design pattern

Observer pattern: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

I am going to refactor the code from the main demo and create 3 new modules for eXpand

eXpand.ExpressApp.AdditionalViewControlsProvider
eXpand.ExpressApp.AdditionalViewControlsProvider.Win
eXpand.ExpressApp.AdditionalViewControlsProvider.Web

So any controller needs to use the hint engine all it has to do is implement IAdditionalViewControlsProvider interface and register to the engine at its activation events look for example the controllers that are resposible for sawing the hint panel

for windows

public partial class WinControlsController : ViewController, IAdditionalViewControlsProvider
{
    protected override void OnActivated()
    {
        base.OnActivated();
        Frame.GetController<WinShowAdditionalViewControlsController>().Register(this, View, Frame);
        
    }
    protected override void OnDeactivating()
    {
        Frame.GetController<WinShowAdditionalViewControlsController>().Unregister(this);
        base.OnDeactivating();
    }
    public object CreateControl()
    {
        return new WinHintPanel();
    }

    public AdditionalViewControlsProviderDecorator DecorateControl(object control)
    {
        return new WinHintPanelDecorator(View, (HintPanel)control);
    }
}

and for web

public partial class WebControlsController : ViewController, IAdditionalViewControlsProvider
{
    protected override void OnActivated()
    {
        base.OnActivated();
        Frame.GetController<WebShowAdditionalViewControlsController>().Register(this, View, Frame);
    }
    protected override void OnDeactivating()
    {
        Frame.GetController<WebShowAdditionalViewControlsController>().Unregister(this);
        base.OnDeactivating();
    }

    public object CreateControl()
    {
        return new WebHintPanel();
    }

    public AdditionalViewControlsProviderDecorator DecorateControl(object control)
    {
        return new WebHintPanelDecorator(View, (HintPanel) control);
    }
}

and now we can decorate any baseobject with a AdditionalViewControlsAttribute like

[AdditionalViewControls("Message for all customer listview",ViewType.ListView,"PropertyNameWithTextToDisplayUnderText")]
public class Customer : BaseObject
{
    
    public string PropertyNameWithTextToDisplayUnderText
    {
        get
        {
            return "another message";
        }
        
    }
}

and have a message that will be displayed to all Customer’s list views for both Web and Win applications. AdditionalViewControls attribute has some parameters that help controlling the visibility, the text of the message or the control to display that message.

[AdditionalViewControls("Message for all customer listview", ViewType.ListView, AdditionalViewControlsProviderPosition.Bottom, typeof(HintPanel), typeof(WinHintPanelDecorator))]
public class Customer : BaseObject
{

image

Nice feature eh? what do you think?

I think its nice but I also think that is very limited, cause I cannot change its visibility according a business rule. For example I would like to display a hint panel only for Customers that live in Paris or only to a named view with id Customer_Grouped_By_Name_ListView or to a Customer Nested listview or to a user that has permissions to do so or … etc you get my point here

To accomplish such scenario that is to enable or disable controllers and actions against a bussiness rule I have develop ModelArtifactState module

so together with AdditionalViewControlsProvider module I will also release the following  modules

eXpand.ExpressApp.Validation
eXpand.ExpressApp.ModelArtifactState
eXpand.ExpressApp.ModelArtifactState.Win

Video 1 

Video 2 
/p>

Ps: Remember Favor composition over inheritance? Do you see that if you apply that principle is going to make your life with ModelArtifactState module easier?

Imagine having 2 controllers A and B. B inherits A. Now you want to use ModelArtifactState to make B conditional, but since it inherits A that behavior(conditionality) is going to be applied to A as well. If you used composition over inheritance you could change the behavior to any of A or B controllers

You can find the above modules as part of eXpand hosted at google code

Ps (Again): My good friend Martin Praxmarer has join eXpand team and contribute its wizard module so now eXpand is wizard Driven, I will post more on that on a later post. So another additional module for eXpand is

eXpand.ExpressApp.WizardUI.Win

I am going to wait DevExpress to release its final 9.2.x version to release any precompiled version of eXpand but you can find eXpand 0.0.0.10 at google code trunk

http://code.google.com/p/expandframework/

Subscribe to XAF feed
Subscribe to community feed

DiggIt!

10 comments:

  1. Tolis - yet more excellent contributions to the XAF community. Many thanks!

    Jascha

    ReplyDelete
  2. thnks Gary hope community will find it usefull

    ReplyDelete
  3. great job mate ... looking forward to using it.

    ReplyDelete
  4. thnks for the help, ideas , and naming you provide to the modules. I want to state the cool ModelArtifactState naming was choosen by you

    ReplyDelete
  5. Tolis, does expand support roles for individual records? Such as a list of reports and a user is not allowed to see Report1 or Report4. Can expand allow you to set those types of security levels?

    ReplyDelete
  6. @Gary have you seen FilterDataStore module. Are you looking for something similar?

    ReplyDelete
  7. Hi Tolis,

    I was wondering if the AdditionalViewController is still working or if there have been changes to it? I can't get it to run.

    Thanks

    ReplyDelete
  8. It should do
    You could post an issue at GitHub Issues describing your problem and will be happy to help

    ReplyDelete