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
  1. <configuration>
  2.   <configSections>
  3.     <section name="LiveUI" type="Xtensive.Applications.Model.ModuleConfigurationSection, Xtensive.Applications" />
  4.   </configSections>
  5.   <LiveUI>
  6.     <Modules>
  7.       <add moduleName="..." moduleAssembly="..." location="..." moduleType="..."/>
  8.     </Modules>
  9.   </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
  1. Application application = new Application();
  2. 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
  1. public class Global : HttpApplication
  2. {
  3.   protected void Application_Start()
  4.   {
  5.     var webApplication = new WebApplication(this);
  6.     webApplication.Run();
  7.   }

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 Module
    Once 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 ?
  1. var application = Application.Current;


How to get module ?
  1. var application = Application.Current;
  2. MyModule myModule = application.Modules.Get<MyModule>();


How to get module resource url ?
  1. var application = WebApplication.Current;
  2. string url = application.GetModuleFileUrl(typeof(MyModule).Assembly, "Resources/Image.gif");


How to get module resource virtual path ?
  1. var webApplication = WebApplication.Current;
  2. var virtualPath = webApplication.GetModuleFileVirtualPath(
  3.   (typeof(MyModule).Assembly), "MyUserControl.ascx");
  4. Page.LoadControl(virtualPath);

WebComponent class also provides LoadControl utility method to load controls, so usually code looks more friendly

  1. control = LoadControl<MyControl>("MyControl.ascx");