XAF Application From Scratch



Comments



Description

In my previous post, I listed all Visual Studio templates that speed-up XAF application development.The goal of this post, however, is to demonstrate how to create a desktop XAF application without using any XAF templates and designers. We will look under the XAF application hood, so to speak, to see that it is a regular .NET WinForms application. Although, I can't suggest that you use this approach in your daily development (because templates speed up your progress greatly), I do believe that it is good practice to try building an application from scratch at least once to better understand XAF architecture. I will also demonstrate how to add extra modules, controllers and security in code - you may find that it is much quicker to type several lines than wait for the designer to load and toolbox to populate. Of course, I don't want to underestimate the value of XAF design-time tools. They are great for beginners, but when you become an XAF expert, you may find that it is handier to make many of the "designable" tasks in code. Create an XAF Application Project Let us begin with the Empty Project template supplied with Visual Studio. The Windows Forms Application template is not suitable for our task because it contains the Form1 class and unneeded references. So, start the Visual Studio and execute FILE | New... | Project command. In the Templates | Visual C# | Windows category, choose the Empty Project template, and specify project and solution names (e.g.,MyXafApplication and MyXafAppplcationSolution and click OK). change it to Windows Application.Open the newly added project properties . The project's Output typeis Console Application by default. .right-click the project in the Solution Explorer and choose Properties. Xpo. the project's References list is empty. 5. . So declare the following MyXafApplication class. DevExpress.For now.1.1. using System. System.contains the WinForms XAF functionality (the WinApplication class in particular).we are making a . Now all initial preparations are done and we can start coding (XAF team recommends that you use CodeRush to speed-up).our application is data-aware (XPO uses this assembly).ExpressApp. 1. we will use XPO to create and access the application database.dll . Add theProgram.dll .Data. We need an instance of the WinForms XAF application to be started from ourMain method.v12. DevExpress.cs file with the following code. 2.ExpressApp. DevExpress.dll . we will require the following assemblies. using DevExpress. using DevExpress. aren't we? System.contains the base XAF functionality (the XafApplication class in particular).1. 3.Win. In the beginning.ExpressApp.ExpressApp.ExpressApp.dll . 4.provides XPO support. namespace MyXafAppplication { static class Program { [STAThread] static void Main() { } } } The application can be launched already.dll .Win.NET application.v12.v12. but it does nothing. g. Connection). myXafApplication. Model Editor) when the application is launched.. Now.Handled = true. configure it.Pooling=false. The minimal required configurations are the ApplicationName and theConnectionString (we connect to the local instance of the Microsoft SQL Server here). myXafApplication. we state that we will use XPO as an ORM tool . In the overridden OnDatabaseVersionMismatch. .Setup(). we instruct XAF to always update the application database when the version mismatch occurs. static void Main() { MyXafApplication myXafApplication = new MyXafApplication(). the empty main window is shown and certain basic functionality is available (e.ExpressApp.Data Source=(local).Update().ObjectSpaceProvider = new XPObjectSpaceProvider(ConnectionString.using DevExpress. args. //.ApplicationName = "MyXafApplication".all Object Spaces in our application will be of the XPObjectSpace type.ConnectionString = "Integrated Security=SSPI. public class MyXafApplication : WinApplication { protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) { args.Start(). } Although our solution contains a single application project and no module projects at all. } protected override void OnDatabaseVersionMismatch(DatabaseVersionMismatchEventArgs args) { args. and run it. myXafApplication... we can instantiate MyXafApplication.Updater.Initial Catalog=MyXafApplication". } } In the overridden CreateDefaultObjectSpaceProvider method. myXafApplication.Xpo. so the Application Designer can be used. Reference the DevExpress.ExpressApp. Add another Empty Project and call it MyXafModule. namespace MyXafModule { public class MyModule : ModuleBase { } } Then change the project's output type to Class Library and specify the assembly version as it is shown in the image below. It the designer. Add the Module Project With Business Model Let us add some functionality to our application. using DevExpress.ExpressApp. the ApplicationName and ConnectionString properties can be initialized in the Properties window. .Note: The default XAF application project created from the template has the WinApplication descendant implemented in a separate file.dll assembly and add the followingMyModule class.v12. The XAF Module is simply a class library that contains the ModuleBase descendant class.1. With the asterisk sign. myXafApplication.Data Source=(local). using MyXafModule. myXafApplication.. } .Add(new MyModule()).Initial Catalog=MyXafApplication".Modules. // .ApplicationName = "MyXafApplication". myXafApplication. static void Main() { MyXafApplication myXafApplication = new MyXafApplication().Pooling=false. myXafApplication. Now we can reference the MyXafModule project in MyXafApplication and add our new module to the application's Modules collection. we instruct Visual Studio to increment the build and revision numbers automatically..ConnectionString = "Integrated Security=SSPI.Start(). This is required for correct database updating by XAF. myXafApplication.Setup(). value). For instance. } } private string email. using DevExpress. we can add the following Contact class.Base.they were added in our application’s ancestor classes (XafApplication and WinApplication respectively). Now we can define a business model within the module in a regular fashion .Persistent. ImageName("BO_Contact")] public class Contact : BaseObject { public Contact(Session session) : base(session) { } private string name. invoke the Model Editor and click the Loaded Modules button.BaseImpl. Note that the SystemModule and SystemWindowsFormsModule are on the list as well . } set { SetPropertyValue("Name".by adding business classes to the module project. using DevExpress. . namespace MyXafModule { [DefaultClassOptions. ref name. we will see that our module is on the list.Xpo. using DevExpress.If we run the application. public string Name { get { return name.Persistent. public class MyModuleUpdater : ModuleUpdater { public MyModuleUpdater(IObjectSpace objectSpace.FindObject<Contact>( new BinaryOperator("Name".FindObject<Contact>( new BinaryOperator("Name". using System.BaseImpl.Name = "Jane Smith". contactJohn. currentDBVersion) { } public override void UpdateDatabaseAfterUpdateSchema() { base.Name = "John Smith".. } set { SetPropertyValue("Email".UpdateDatabaseAfterUpdateSchema().Data. using DevExpress. the DevExpress. ref [email protected]. } Contact contactJohn = ObjectSpace.Updating. Contact contactJane = ObjectSpace.v12.Xpo.dll and DevExpress. Version currentDBVersion) : base(objectSpace. using DevExpress. contactJohn. //. a reference to this assembly is required to compile the code above.Filtering. Supply Initial Data To add several Contact records to the database.ExpressApp.Collections..ExpressApp.v12. "Jane Smith")).com". . Since we use XPO. "John Smith")). let's implement a Module Updater class. value). using System.1. if (contactJane == null) { contactJane = ObjectSpace.simply choose Add | New Class. As the ancestor BaseObject class resides within the DevExpress. you can add this class without using a special XAF template .1..CreateObject<Contact>()[email protected]<Contact>().Email = "jane.Data. contactJane. } } } } To follow our "from scratch" concept.v12.public string Email { get { return email.dll references are required as well. if (contactJohn == null) { contactJohn = ObjectSpace. } } } The image below illustrates the result.. contactJane..Email = "john.dll assembly. using DevExpress.com". For instance.v12.dll assembly and add an instance of ReportsWindowsFormsModule class to the application'sModules collection. Version versionFromDB) { return new ModuleUpdater[] { new DatabaseUpdate. . if you are reading this article when 12. versionFromDB) }.Updater(objectSpace. It is recommended to use this method to improve performance. So. public class MyModule : ModuleBase { public override IEnumerable<ModuleUpdater> GetModuleUpdaters(IObjectSpace objectSpace. modify the MyModule class as follows. the ModuleUpdater descendants are automatically collected via the reflection.2 release.1.1).Win. } } Use Extra Modules You can plug extra modules supplied with XAF in the same manner as our custom MyModule.Reports.In the current version of XAF (12. In the upcoming 12. we have added an option to explicitly register Module Updater classes that should be used by the module in the overriddenGetModuleUpdaters method.2 is already published and you have it installed. reference theDevExpress. to enable reporting.ExpressApp. using DevExpress. we can design and print reports! . myXafApplication.Modules. static void Main() { MyXafApplication myXafApplication = new MyXafApplication().Add(new MyModule()).. //.ConnectionString = "Integrated Security=SSPI. } With a single line of code.Modules. myXafApplication.Win.Add(new ReportsWindowsFormsModule()).Pooling=false. myXafApplication. myXafApplication. myXafApplication.ApplicationName = "MyXafApplication".Start().Setup().Reports.ExpressApp..Data Source=(local). myXafApplication.Initial Catalog=MyXafApplication". 1. reference the DevExpress.Note: In the default XAF application project. and initialize the application's Security property.Security. but experienced XAFers can do the task in code more quickly.v12.ExpressApp. instantiate the Security Strategy and Authentication. the Application Designer is used to populate the modules collection. It is convenient for novices to browse the toolbox and pick the required modules. . Additionally.dll assembly. Secure Everything To enable the security system. ExpressApp. myXafApplication. myXafApplication.Add(new MyModule()).Add(new ReportsWindowsFormsModule()). of course.Modules. when you know what code to type.Pooling=false.Xpo.1. authentication).references to the DevExpress.Initial Catalog=MyXafApplication". myXafApplication.Strategy. and the application is secure! No need to wait for the designer to load and search for the security components in the toolbox.Setup(). using DevExpress.dll andDevExpress. AuthenticationActiveDirectory authentication = new AuthenticationActiveDirectory() { CreateUserAutomatically = true}. myXafApplication.Security = new SecurityStrategyComplex(typeof(SecuritySystemUser). myXafApplication..ApplicationName = "MyXafApplication"..1.v12.Data Source=(local).Base. . static void Main() { MyXafApplication myXafApplication = new MyXafApplication(). This is a very quick approach.Modules. } Two lines of code are added.Security.ConnectionString = "Integrated Security=SSPI.Persistent.dll assemblies that are used by the Security System are required. using DevExpress.ExpressApp. myXafApplication. typeof(SecuritySystemRole). myXafApplication. //.Start().Security.v12. "SendMessage".Execute += delegate(object sender. //.Add a Controller with Action Adding a Controller and Action can be easily done in code.Base..Diagnostics. sendMessageAction. Contact> { public SendMessageController() { SimpleAction sendMessageAction = new SimpleAction(this.ImageName = "BO_Contact".ExpressApp.RequireSingleObject. using System. Let's add the following class to our module project. sendMessageAction. using DevExpress.Actions.View).ExpressApp. SimpleActionExecuteEventArgs e) { string startInfo = String.SelectionDependencyType = SelectionDependencyType. sendMessageAction.Format( . using DevExpress. PredefinedCategory.. using System. without the use of a template and designer.Persistent. using DevExpress. public class SendMessageController : ObjectViewController<ListView. ViewCurrentObject. }. . Since the target object type (Contact) is known."mailto:{0}?body=Hello. Process. Below is the implemented SendMessage Action. is unavailable.xafml file to the MyXafModule project.Email.Name). It is convenient to pass a target view and object types as generic parameters . } } This Controller invokes the email client to compose a message addressed to the chosen contact. a great tool for the Application Model browsing and customizing.DesignedDiffs.Start(startInfo). Also. note that we use the generic ObjectViewController class here. ViewCurrentObject. Enable the Design-Time Model Editor A big drawback of our hand-made solution is that the Model Editor.no need to initialize the corresponding controller's properties. Fortunately. Don't forget to declare the Controller aspublic. there is no need to cast the current object value to the Contact type. The Controller's ViewCurrentObject property simplifies access to the current object. we can fix this easily. Just add the Model. {1}!%0A%0A". The next required step is to open this file in an XML editor and add the following code (the Model Editor won't start on the empty file). . <?xml version="1.0" ?> <Application/> Now. the Model Editor can be used to change the model differences for the MyXafModule module. it will be ignored. to apply changes at runtime. As this class name implies.bo).By default.xafml and *. it reads application model customizations from the module assembly’s resource files (*. change the XAFML file's Build Action to Embedded Resource. an XAF module uses the ResourcesModelStore model differences storage. . If the model differences file isn’t built as an embedded resource. So. Analogously. the Model Editor can be enabled for the MyXafApplication project. The required file name for the application project isModel. The file's Build Action property should be set to Content.xafmlfile located in the application’s working folder. and the application-level customizations are loaded from the Model. .to Copy Always.xafml. In WinForms XAF applications. and Copy to Output Directory . the FileModelStore model differences storage is used. aspx. Meantime. you can use lightweight modules and controllers demonstrated here. . etc.As a result. in web. In this article.Login.).aspx. we have a fully functional WinForms XAF solution. P.NET XAF application from scratch due the volume of code required to create XAF web pages (Default.: You can download the complete source code demonstrated here from the Code Central. I have omitted web application implementation intentionally. It is not really easy to create an ASP.S.
Copyright © 2024 DOKUMEN.SITE Inc.