Applications and Modules.
LiveUI provides modular architecture and helps creating reusable application
blocks. Technically LiveUI allows sharing .ascx user controls and resources between
asp.net
web applications. For example, given Forum web application we can plug it to MyWebSite
application and use Forum's user controls. So if web application consists
of .ascx user controls we can name it "module" and plug everywhere.
This document describes how to configure and use modules.
Modules registration.
The easiest way to register modules is to use LiveUI configuration section, typically
looking like this
Module configuration sample
- <configuration>
- <configSections>
- <section
name="LiveUI"
type="Xtensive.Applications.Model.ModuleConfigurationSection,
Xtensive.Applications" />
- </configSections>
- <LiveUI>
- <Modules>
- <add
moduleName="..."
moduleAssembly="..."
location="..."
moduleType="..."/>
- </Modules>
- </LiveUI>
So line like this <add
moduleName="..."
moduleAssembly="..."
location="..."
/> should be added to Modules node
for each module. There are module configuration parameters
- moduleName
-
this is unique name used to access module resources. For example, if we name
module 'MyModule' then module resources will be
accessible by url like 'Modules.axd/MyModule/MyImage.gif'
and virtual path like this '~/Modules.axd/MyModule/MyControl.ascx'.
This parameter is required.
- moduleAssembly
- this is name of module assembly. This parameter is
required unless moduleType parameter is specified.
- location
- this is application relative or absolute path to module directory where
ascx files and resources are located. If this parameter is not specified
modules resources won't be accessible.
- moduleType
- name of type to be used for module configuration and initialization, this
type should be derived from Xtensive.Application.Model.Module type. If
moduleType is specified then new moduleType instance will be created on
application start and notified about application events.
Application lifecycle
LiveUI provides Application class to load and initialize modules. When LiveUI
based application starts new Application instance should be created and
initialized. There is an example of how to create and start application:
Application start sample
- Application application =
new
Application();
- application.Run();
Usually code like this is used just in Tests, becuase for web application
LiveUI provides special WebApplication class, which is typically used in
Global.asax.
Web Application start sample
- public
class
Global : HttpApplication
- {
- protected void Application_Start()
- {
- var webApplication =
new
WebApplication(this);
- webApplication.Run();
- }
WebApplication not only loads modules but also registers VirtualPathProvider
to make module resources accessible via virtual path.
Once application object was created and strated, it goes through following
stages:
- Loading ModulesAt this stage Application reads
configuration and creates Module instances. Once module instance is created
it is placed to Application.Modules collection and Module.Init
method is called.
Configuring ModuleOnce all modules are Loaded,
Module.OnModulesLoaded method is called for each module and modules
can find to configure each other.
- RunningOnce all modules are Loaded and Configured,
Module.OnApplicationRunning method is called for each
module and modules can perform startup actions.
Modules usage
There are typicall tasks for using modules
How to get application object ?
- var application =
Application.Current;
How to get module ?
- var application =
Application.Current;
- MyModule myModule =
application.Modules.Get<MyModule>();
How to get module resource url ?
- var application =
WebApplication.Current;
- string url =
application.GetModuleFileUrl(typeof(MyModule).Assembly,
"Resources/Image.gif");
How to get module resource virtual path ?
- var webApplication =
WebApplication.Current;
- var virtualPath =
webApplication.GetModuleFileVirtualPath(
- (typeof(MyModule).Assembly),
"MyUserControl.ascx");
- Page.LoadControl(virtualPath);
WebComponent class also provides LoadControl utility method to load controls,
so usually code looks more friendly
- control = LoadControl<MyControl>("MyControl.ascx");
|