LiveUI Components.

Components are building blocks of LiveUI applications. There are important component properties :

  • Components can be initialized
  • Components can have state
  • Components can be extended
  • Components can contain other components

Technically Component just is a .NET class looking almost as simple as this:

  1. public class Component
  2. {
  3.   public Component Parent { get; }
  4.   public ComponentCollection Children { get; }
  5.  
  6.   public void Initialize()
  7.   public void Destroy()
  8. }


Though Component is that simple it turns out to be usefull because it provides common application structure and predictable behaviour model.

Component life cycle

There are stages each Component goes through:
  1. Construction
    Usually Components support argumentless constructor so one can just call new MyComponent() to create new instance.
  2. Configuration
    After creating component's properties can be used for configuration. LiveUI best practice for components is not to throw exceptions at property setters even if property values are not valid.
  3. Initialization
    When initialized, Component validates itself and throws Exception if configuration is not valid. It can also perform some strartup actions on this stage.
  4. Child Components Initialization
    When Component is Initialized all children components are also automatically initialized, For example, If ComponentA is child of ComponentB then ComponentB.Initialize() call also initiates ComponentA.Initialize().
  5. Working
    If there are public methods in Component, typically, it's the best moment to call them. For example, if we have DocumentViewer Component with PrintDocument method. Typically DocumentViewer should be initialized before Print method is called or exception should be thrown. 
  6. Child Components Destruction
    When Component.Destroy() method is called Component destroys child components first and then itself. Usually Components do nothing when destroyed; thanks to .NET garbage collector.
  7. Destruction
    It is the best moment to perform ending actions.

Component State

State is important for web applications. Every time http request received new compont tree is created, but application code should look as if component tree were the same while user works with the same page. This pattern it familiar for those who worked with ASP.NET, where contols works the same way. In ASP.NET there are control state and ViewState to solve the problem, LiveUI provides similar tools for Components. Component has State property to keep persistent data. Following sample illustrates how to deal with component state:

  1. public class DocumentViewer
  2.   : Component
  3. {
  4.   public string DocumentID {
  5.     get { return State.Get<string>("DocumentID");}
  6.     set { State.Set("DocumentID", value);}
  7.   }
  8. }

State is accessible for reading when component is initialized and for writing when component is constructed. Internal state mechanics can be customized, see IComponentStateProvider for details.

Extending Components

There are two ways to extend component : Plugins and Contracts.

Contracts. Sometimes It's especiallly important to keep components loousely coupled. For example, if we implement MVP pattern, it is important for Presenter to be loosely coupled with View. Contracts are used to manage dependencies like this. Contracts also allow any object to be associated with Component without event Component knowing about it. See Contracts for details.

Plugins. Usually asp.net applications are not resuable and there is no need to customize something. But if application is designed as a module it may be important for users to be able to customize module components. Plugins allow custom code to affect module  components without source code modification. See Plugins for details.