Xafari ERP Concepts. AppModule
Reasons to use AppModule in the ERP system are described in the Xafari ERP Concepts. System Design topic. Its specific role is such that it does not contain business classes and controllers, it does not define any business logic, it does not participate in the UI construction.
It must configure the following properties of an XafApplication object as security, authentication, objectspace, required modules etc. This will allow you to run XafApplication instances on different platforms and be sure that they maintain the integrity of the system and interact uniformly with the data model and business functions (that is, the core of the system).
To add AppModule project to the solution, inherit from the Xafari.AppModules.AppModuleBase class. You can also run the Xafari Solution Wizard for an existing solution. Right-click the solution in the Solution Explorer, choose Add | New Project... and select Xafari vXX.X.XXX ERP Solution Wizard. The image below shows the Wizard's start page in this case.
Starting with version x011, the appropriate XXX.App module project is included by default in the Xafari solution. You can see AppModule in the Northwind demo instaled with Xafari.
To set or change main parameters, you can use special AppModule Designer.
Of course, it is possible to make all valid manipulations programmatically. The code snippet below shows some operations in the NorthwindAppModule class.
- c#
- VB
public sealed partial class NorthwindAppModule : AppModuleBase
{
public NorthwindAppModule()
{
InitializeComponent();
}
public override void InitializeApplication(XafApplication application)
{
base.InitializeApplication(application);
application.LinkNewObjectToParentImmediately = false;
if (System.Diagnostics.Debugger.IsAttached && application.CheckCompatibilityType == CheckCompatibilityType.DatabaseSchema)
application.DatabaseUpdateMode = DatabaseUpdateMode.UpdateDatabaseAlways;
application.CustomizeLanguagesList += this.NorthwindAppModule_CustomizeLanguagesList;
application.Xafari().CustomizeApplicationContext += this.NorthwindAppModule_CustomizeApplicationContext;
application.DatabaseVersionMismatch += Application_DatabaseVersionMismatch;
XafariSecuritySystem.SyncWithModel = true;
}
void Application_DatabaseVersionMismatch(object sender, DatabaseVersionMismatchEventArgs e)
{
e.Updater.Update();
e.Handled = true;
}
void NorthwindAppModule_CustomizeApplicationContext(object sender, Xafari.Helpers.CustomizeApplicationContextEventArgs args)
{
args.Context.Add(XafariApplicationContext.Xpo);
}
protected override void CreateObjectSpaceProviders(CreateCustomObjectSpaceProviderEventArgs args)
{
base.CreateObjectSpaceProviders(args);
}
private void NorthwindAppModule_CustomizeLanguagesList(object sender, CustomizeLanguagesListEventArgs e)
{
string userLanguageName = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
if (userLanguageName != "en-US" && e.Languages.IndexOf(userLanguageName) == -1)
{
e.Languages.Add(userLanguageName);
}
}
}
Public Partial NotInheritable Class NorthwindAppModule
Inherits AppModuleBase
Public Sub New()
InitializeComponent()
End Sub
Public Overrides Sub InitializeApplication(ByVal application As XafApplication)
MyBase.InitializeApplication(application)
application.LinkNewObjectToParentImmediately = False
If System.Diagnostics.Debugger.IsAttached AndAlso application.CheckCompatibilityType = CheckCompatibilityType.DatabaseSchema Then
application.DatabaseUpdateMode = DatabaseUpdateMode.UpdateDatabaseAlways
End If
application.CustomizeLanguagesList += Me.NorthwindAppModule_CustomizeLanguagesList
application.Xafari().CustomizeApplicationContext += Me.NorthwindAppModule_CustomizeApplicationContext
application.DatabaseVersionMismatch += Application_DatabaseVersionMismatch
XafariSecuritySystem.SyncWithModel = True
End Sub
Private Sub Application_DatabaseVersionMismatch(ByVal sender As Object, ByVal e As DatabaseVersionMismatchEventArgs)
e.Updater.Update()
e.Handled = True
End Sub
Private Sub NorthwindAppModule_CustomizeApplicationContext(ByVal sender As Object, ByVal args As Xafari.Helpers.CustomizeApplicationContextEventArgs)
args.Context.Add(XafariApplicationContext.Xpo)
End Sub
Protected Overrides Sub CreateObjectSpaceProviders(ByVal args As CreateCustomObjectSpaceProviderEventArgs)
MyBase.CreateObjectSpaceProviders(args)
End Sub
Private Sub NorthwindAppModule_CustomizeLanguagesList(ByVal sender As Object, ByVal e As CustomizeLanguagesListEventArgs)
Dim userLanguageName As String = System.Threading.Thread.CurrentThread.CurrentUICulture.Name
If userLanguageName <> "en-US" AndAlso e.Languages.IndexOf(userLanguageName) = -1 Then
e.Languages.Add(userLanguageName)
End If
End Sub
End Class
As you can see in the code above, AppModule initialize a various parameters of the XAF application. The whole process of configuring the application has been migrated from .exe project to AppModule project. The only thing that remained in the .exe project, this is the code that creates the Application Host object, initializes the host, and starts the Xafari Application within the host.
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
AppHost takes into account the specifics of a particular platform, DC or XPO data model, and, then performs the final preparation of the application object.
Xafari Northwind demo solution contains two application projects: Win.App and Web.App. Both the applications are based on the common settings that are implemented in AppModule. These applications share common sets of platform independent modules, security system, Object Space providers etc. In a situation when you need to change the set of modules in the core system, it is enough to modify the AppModule only, and all the target applications will be configured automatically. It applies to all application settings.
This approach to configuration and administration makes the system flexible and reliable. You can easily extend the ERP system with a several target applications. The new app project just should refer to the AppModule and use the appropriate AppHost. Xafari supplies a set of templates for Visual Studio, which represent the most popular app projects. They are available via the Xafari Solution Wizard.