Adding Entities To A Catharsis Application
This document is a step by step guide to adding new entities to a Catharsis web application using the automated guidance which creates the skeleton classes and multi-teir architecture automatically.
In this document we will add a new entity to the demo solution for the Catharsis Framework which is called Firm.SmartControls.
A good way to lean more about Catharsis is to install the demo solution and follow the step by step guide below.
Prerequisites
The author of Catharsis, Radim Kohler, has published many documents on the ASP.NET MVS Catharsis Framework and these can be found here:
http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=5517102
The Catharsis source code and the installer for the demo project (Firm.SmartControls) can be found here:
http://www.codeplex.com/Catharsis
Before you read this document please read my previous one which takes a brief look at the demo project and therefore acts as a good primer for this document. You can find the document, entitled The Catharsis Demo Project, here:
http://www.codeproject.com/KB/aspnet/TheCatharsisDemoProject.aspx
Using the Guidance
The database create script for the Firm.SmartControls solution actually contains two tables which are not yet mapped so we will use one of these as an example of how to add a new entity.
'Client' will be our new entity. (We will add it to the Insurance namespace).
Before we can add the new entity using Guidance we need to enable the Guidance package.
Click on Tools -> Guidance Package Manager
Click Enable/Disable Packages on the dialog that appears, select ProjectBase.Guidance and click okay.
Close the next two dialogs that appear as we do not need them now.
Note that if you want to add a complete web infrastructure the best place to add it is via the Entity (or Web project). It is also possible to add it in the DataLayer but this would exclude the GUI elements which we will require in this instance.
The folder into which the new entity is added will become part of the namespace for that new entity. If you want the entity to be in a new namespace you should create a folder in the entity project and add it there, alternatively you can add it to an existing folder as we will do in this case because we want our new entity to be in the Insurance namespace.
Right click on the folder and select "(Re) Create COMPLETE WEB infrastructure" from the menu. ‘(Re)’ signifies that if the entity has already exists in the folder the files will be overwritten with new empty skeleton classes, this offers a way to undo code or correct mistakes.
"COMPLETE WEB" means, that skeleton class-files will be added to every project (even unit tests).
If you select the "(Re) Create Project - (Entity, Business, Common, Tests)" - no files in the Models, Controllers and Web projects will be added (or changed). This is for cases where no GUI elements are required.
The namespace of the new entity should be Firm.SmartControls.Entity.Insurance.Client so we click on the Insurance folder as shown to generate the web infrastructure via Guidance.
Here is the main dialog which we need to fill. Giving as much information here as possible will reduce the amount of work that we need to do later.
Type the name of the new entity in the dialog. You can now add up to three additional properties.
In SQL Server 2005 we saw the columns of the InsuranceClient table so we can add the first three, Code, Name and BankCode. Guidance will automatically generate checks to ensure that Code is unique (this can be deleted if it is not required). Adding properties here reduces the amount of work that we will have to do later, however we can only add value types, for example a string property ‘name’, we cannot add Entity types, for example a foreign key which references another table such as Country.
The Namespace is provided because it is determined by where you added the entity in the solution.
The entity type in this case should be 'Persistent'. That's will create the skeleton for business object which has no ancestors for the business type (it is derived directly from the Persistent object).
Other types allow reusing previously implemented functionality.
The second and third options are for CodeLists, you can choose ‘simple’ or ‘separate entity’. First let we need to be clear on what a CodeList is. CodeList entities are often used to populate comboboxes, for allowing the user to selct one option from a collection of predefined options. All of the countries in the EU could be represented in this way, the genders Male and Female are another good example. Another general propertu of CodeList entities is that the data is static, it will not be necessary to add or delete objects of this type. Gender, for example will never need more than ‘Male’ and ‘Female’.
The base classes give CodeList entities a code and a name, so for Gender the name could be Male and the code could be M. These are simple entities because no additional information is required. Therefore using the option for simple CodeList is suitable for something like Gender or Country. If you need a simple entity like Gender you can use the ICodeList option. In that case, you do not have to implement anything, your new ICodeList property will be working immediately without any additional coding.
The framework also gives the option to create a CodeList but allows for the entity to be extended with additional information. Currency, for example could have an object with the Name ‘Dollar’ and the Code ‘USD’ but we might also want to add a column for subunit and give it a value of ‘cents’ or ‘c’. If we need to extend the basic functionality of a CodeList the ‘separate entity’ can be used. In this case a column in the database table should hold a value for the subunit.
The Tracked entity type is the same as a Persistent type but additional code is provided which allows an ‘Audit Trail’ to be maintained for the entity. If you need to track when an entity is changed, who changed it and what state it is in this is the best option.
Click Finish and after some time all of the files will be automatically generated and a pop-up will appear to tell you what you should do next:
So we follow these instructions and open the Str.Controller.cs file and add the highlighted line:
Now we open the menu.config file
The highlighted code should be added:
It is added at the same level as Agent so it will appear as a sibling of this node in the Navigation tree.
Attempting to use this new entry in the navigation menu will cause an error of course because the database table has not yet been mapped via NHibernate:
Mapping the database table to the entity via NHibernate
The table that we are mapping looks like this:
Open the NHibernate file which was automatically generated for this entity.
Firm.SmartControls.Data.Insurance.Clicne.hbm.xml
The data which you supplied during the creation of the entity is already added:
The following sections need to be changed:
The table name is InsuranceClient, not Cient.
The ID column is InsuranceClientId, not ClientId.
CountryId and GenderId are CodeLists and will require many-to-one mappings.
We can see a reference to Firm.SmartControls.Entity.Insurance in the file above so this will need to be changed to reflect the changes we made in the mapping file.
The DAO (Data Access Object) will also need to be changed but before we do that we will add the properties to the Entity file which were not automatically generated by the Guidance.
Open the Client entity file
Three properties exist, Code, Name and BankCode. We will now add Gender and Country. These are CodeList objects so we need to add a using directive for Firm.SmartControls.Entity.CodeLists in order for the Gender and Country datatypes to be recognised. The code we should add is included below.
using System;
using System.Collections.Generic;
using System.Linq;
using ProjectBase.Core;
using ProjectBase.Core.PersistentObjects;
using ProjectBase.Core.Collections;
using Firm.SmartControls.Entity.CodeLists;
namespace Firm.SmartControls.Entity.Insurance
{
[Serializable]
public class Client : Persistent
{
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual string BankCode { get; set; }
public virtual Gender Gender { get; set; }
public virtual Country Country { get; set; }
Now we add these additional fields to the DAO
(Firm.SmartControls.Data.Insurance.ClientDao)
The newly added Gender and Country should be available in intellisense when we add the two new entries, the is obviously because they are now properties of the Client entity.
Running the application with the new entity
Now we have made the necessary changes to the NHibernate file, the Entity and the Dao so the 'Client' menu item will work.
Of course there are no Clients in the database yet so we will need to add these. The next step is to extend the functionality behind the ‘New’ button to allow us to add new Clients.
Adding new Clients
If we click the 'New' button now we will see that the properties which we specified during the Guidance setup (Code, Name and BankCode) are automatically added.
Now we will add the Gender and Country properties.
Open the ClientDetailsWC.ascx file (The abbreviation WC is for ‘Web Control’)
This file shows the HTML Markup used to create the page shown above.
We will reduce the size of the two columns (Identification and Description) and add a third column for CodeLists and will add CodeLists for Gender and Country.
Each Row in the HTML contains a number of fieldsets. There is currently one fieldset for Identification and one for Description. We will reduce the percentage width of these two to 32% so we will have enough room in the row for three fieldsets.
License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here