Xafari ERP Concepts. AppHosts
Application Host
Every Xafari Application derives from the DevExpress.ExpressApp.XafApplication class and implements Xafari.IXafariApplication interface. This interface declares the ApplicationHost property which refers to a very important object for the Xafari Application. The Application Host creates, initializes, and starts the Xafari Application.
The Xafari.ApplicationHost class provides standard containers for the Xafari Applications. The main functions of the ApplicationHost are:
- Initialize the environment for the application.
- Create and initialize the XafApplication object. The host is a factory for the XafApplication objects.
- Load all platform-agnostic modules specified in AppModule and determine which platform-specific modules it is necessary to add to the project.
- The host ensures the availability of standard features for Xafari applications.
The Xafari framework ships application classes and the appropriate hosts for various platforms, they are listed below:
- Xafari.Win.XafariWinApplication hosted by the Xafari.Win.XafariWinApplicationHost. XafariWinApplication represents Xafari Application for the Windows Forms platform.
- Xafari.Web.XafariWebApplication hosted by the Xafari.Web.XafariWebApplicationHost. XafariWebApplication represents Xafari Application for the ASP.NET Web Forms platform.
- Xafari.BC.Console.ConsoleApplication hosted by the Xafari.BC.Console.ConsoleApplicationHost. ConsoleApplication represents Xafari Application for the Command Prompt.
- Xafari.Mvc.MVCx.MVCxApplication hosted by the Xafari.Mvc.MVCx.XafariMVCxApplicationHost. MVCxApplication represents Xafari Application for the ASP.NET MVC platform.
- Xafari.Mobile.XafariMobileApplication hosted by the Xafari.Mobile.XafariMobileApplicationHost. XafariMobileApplication represents Xafari Application for the mobile interfaces.
- Xafari.Utils.ModelEditorApplication is intended for the Xafari Model Editor utility.
Note
All classes listed above are supported by the appropriate Visual Studio templates. The final set of supplied classes for version х011 is still under discussion.
In the application project, the Application Host creates a host object with the desired parameter values, initializes the host, and starts the Xafari Application within the host. The code snippets below demonstrate the use of the host for various application types:
Win app
Program.cs:
- c#
- VB
static void Main(params string[] args)
{
using (var host = new XafariSolutionWinApplicationHost { Arguments = args })
{
host.Setup();
host.StartApplication();
}
}
Private Shared Sub Main(ParamArray args As String())
Using host = New XafariSolutionWinApplicationHost() With {.Arguments = args}
host.Setup()
host.StartApplication()
End Using
End Sub
Web app
Global.asax:
- c#
- VB
protected void Application_Start(Object sender, EventArgs e)
{
XafariSolutionWebApplicationHost.SetInstance(new XafariSolutionWebApplicationHost { UseSharedApplication = true }).Setup();
}
protected void Session_Start(Object sender, EventArgs e)
{
XafariSolutionWebApplicationHost.Instance.StartApplication(Session);
}
protected void Application_Error(Object sender, EventArgs e)
{
XafariSolutionWebApplicationHost.HandleException();
}
protected void Session_End(Object sender, EventArgs e)
{
XafariSolutionWebApplicationHost.DisposeApplication(Session);
}
protected void Application_End(Object sender, EventArgs e)
{
XafariSolutionWebApplicationHost.DisposeInstance();
}
Protected Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
XafariSolutionWebApplicationHost.SetInstance(New XafariSolutionWebApplicationHost() With {.UseSharedApplication = True}).Setup()
End Sub
Protected Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
XafariSolutionWebApplicationHost.Instance.StartApplication(Session)
End Sub
Protected Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
XafariSolutionWebApplicationHost.HandleException()
End Sub
Protected Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
XafariSolutionWebApplicationHost.DisposeApplication(Session)
End Sub
Protected Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
XafariSolutionWebApplicationHost.DisposeInstance()
End Sub
Console app
Program.cs:
- c#
- VB
static int Main(string[] args)
{
using (var host = new ConsoleApplicationHost(args) { Name = "XafariSolution.Console" })
{
return host.Start();
}
}
Private Shared Function Main(ByVal args As String()) As Integer
Using host = New ConsoleApplicationHost(args) With {.Name = "XafariSolution.Console"}
Return host.Start()
End Using
End Function
Application Host objects are customizable. To change or extend the behavior of the host, the developer can override virtual methods. The default application provides all the standard Xafari features and custom improvements must be justified. The code snippet below shows the host class for the Win Application project:
- c#
- VB
public class XafariSolutionWinApplicationHost : XafariWinApplicationHost
{
public XafariSolutionWinApplicationHost()
{
}
protected override void OnApplicationCreated(XafariWinApplication application)
{
base.OnApplicationCreated(application);
// add here code to customize application after it is created
}
protected override void OnApplicationInitialized(XafariWinApplication application)
{
base.OnApplicationInitialized(application);
// add here code to customize application after it is initialized
}
protected override void OnApplicationStarted(XafariWinApplication application)
{
base.OnApplicationStarted(application);
// add here code to customize application after it is started
}
protected override void SetupCore()
{
base.SetupCore();
// add here code to customize application environmemt
}
protected override void HandleExceptionCore(XafariWinApplication application, Exception exception)
{
base.HandleExceptionCore(application, exception);
// add here code to customize exception handling
}
}
Public Class XafariSolutionWinApplicationHost
Inherits XafariWinApplicationHost
Public Sub New()
End Sub
Protected Overrides Sub OnApplicationCreated(ByVal application As XafariWinApplication)
MyBase.OnApplicationCreated(application)
' add here code to customize application after it is created
End Sub
Protected Overrides Sub OnApplicationInitialized(ByVal application As XafariWinApplication)
MyBase.OnApplicationInitialized(application)
' add here code to customize application after it is initialized
End Sub
Protected Overrides Sub OnApplicationStarted(ByVal application As XafariWinApplication)
MyBase.OnApplicationStarted(application)
' add here code to customize application after it is started
End Sub
Protected Overrides Sub SetupCore()
MyBase.SetupCore()
' add here code to customize application environmemt
End Sub
Protected Overrides Sub HandleExceptionCore(ByVal application As XafariWinApplication, ByVal exception As Exception)
MyBase.HandleExceptionCore(application, exception)
' add here code to customize exception handling
End Sub
End Class
Note
All code described in this topic is available in the projects that are supplied by the Xafari Solution Wizard.
Application Context
Application Context determines the final set of the required modules for the Xafari application on the target platform. The system is designed in such a way that all platform-independent modules are listed in AppModule specified in the application's config-file. And platform-specific modules must be added during the application initialization. Application Host performs this operation using Xafari.XafariApplicationContext.
For instance, the figure below shows the host of the Galaktika Win-application . The application is supplemented with Win-modules from three suppliers: DevExpress XAF, Xafari, and Galaktika custom modules.

Important
There is an agreement for modules names. If the platform-specific module is named Module1, then Application Host will look for Module1.Win, Module1.Web and other similar titles. The platform suffix depends on the current application context. This naming agreement also covers all DevExpress modules.
Additional modules are first loaded from the GAC to fully qualified name. If the module is not found in the GAC, it is loaded on an incomplete name of the available applications for places.
Available contexts are listed below:
- XafariApplicationContext.RegisterSuffix(Win, ".Win");
- XafariApplicationContext.RegisterSuffix(Web, ".Web");
- XafariApplicationContext.RegisterSuffix(Mvc, ".Mvc");
- XafariApplicationContext.RegisterSuffix(Cfg, ".Cfg");
- XafariApplicationContext.RegisterSuffix(Console, ".Console");
- XafariApplicationContext.RegisterSuffix(Xpo, ".Xpo");
- XafariApplicationContext.RegisterSuffix(DC, ".DC");
- XafariApplicationContext.RegisterSuffix(EF, ".EF");
- XafariApplicationContext.RegisterSuffix(Mobile, ".Mobile");
The XafariApplicationContext must be specified in the application project, which is a container for a particular Xafari Application. To do this, modify the appropriate AssemblyInfo.cs file and set the XafariApplicationContext via the Xafari.XafariApplicationContextAttribute. For instance, the code snippet below shows the XafariApplicationContextAttribute for the Win app project:
This attribute is required by the Model Editor to detect application context usage. If the project is created by Xafari Solutions Wizard, it already contains all the necessary code.
