Component

A component can be a user interface control or any other .NET object. The component works using the following pattern:
  • receive request message
  • execute command
  • post response message
According to incoming messages the component can update its state. The component can access external services, global application singletons and ask states of other components.

var now = Env.Components.First<ITimeProvider>().Now;
Every component is examined during adding it to the ComponentContainer.

Env.Components.Add(new LoginComponent());
Each method marked with MessageSubscriberAttribute is added to the invocation list of the according message channel.
You should avoid sophisticated logic in a component. Advanced algorithms should be located in Commands and helper classes which are easy testable with unit tests.

Example

public class LoginComponent : IMyComponent
{
    private CommandAsyncExecutor<LoginCommand> _loginCommandAsyncExecutor;

    public LoginComponent()
    {
        _loginCommandAsyncExecutor=new CommandAsyncExecutor<LoginCommand>();
        _loginCommandAsyncExecutor.ExecuteCompleted += on_loginCommandAsyncExecutor_ExecuteCompleted;
    }

    [MessageSubscriber(Env.MainMessageChannelName)]
    private void handleLoginRequestMessage(LoginRequestMessage m)
    {
        // Each command has Argument and Result properties
        var arg = new LoginCommandArgument(m.Name, Env.Components.TimeProvider);
        var cmd = new LoginCommand(arg);

        _loginCommandAsyncExecutor.ExecuteAsync(cmd);
    }

    private void on_loginCommandAsyncExecutor_ExecuteCompleted(object sender, CommandEventArgs<LoginCommand> e)
    {
        var mes = new LoginResponseMessage(e.Command.Error);
        if (!mes.HasError)
        {
            // no error
            mes.Greeting = e.Command.Result.Greeting;
        }

        Env.MainMessageChannel.Post(mes);
    }
}

Naming

Each component should end with the suffix Component.

Last edited Jan 13, 2013 at 7:43 AM by polo, version 2

Comments

No comments yet.