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:
- public
class
Component
- {
- public
Component Parent {
get; }
- public
ComponentCollection Children {
get; }
-
- public
void Initialize()
- public
void Destroy()
- }
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:
- Construction
Usually Components support argumentless constructor
so one can just call new MyComponent() to create new instance.
- 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.
- Initialization
When initialized, Component validates itself and
throws Exception if configuration is not valid. It can also perform some strartup
actions on this stage.
- 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().
- 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.
- 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.
- 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:
- public
class
DocumentViewer
- : Component
- {
- public
string DocumentID {
- get {
return State.Get<string>("DocumentID");}
- set { State.Set("DocumentID",
value);}
- }
- }
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.
|