The second important class is SelectFrom (or the Generic version SelectFrom<T>) which provides methods to query the database and return business objects/trees using a syntax similar to the SQL SELECT statement.
The third piece of the framework is a set of attributes which can be used to define object trees.
Raw performance is similar to that of the ADO.NET Dataset, but thanks to the flexibility of the OO design promoted by the framework and the automatic handling of object trees, overall application performance is often improved by an order of magnitude.
Walkthrough: Creating a business class by using the wizard integrated with Visual Studio
Variations:
a) The business object contains a child object
Let's assume that we have created two business classes Account and Address by using the wizard and now we want the Account class to have a child property holding the current address of the account. Let AccountID be the name of the primary key in table Account and also the the foreign key in table Address. Then the steps are:
[On("AccountiD")] [Where("IsActive", Op.Equal, true)] public Address CurrentAddress { ... }
Now when an Account object is loaded from the database its current address will be automatically loaded into the field _currentAddress. The [On] attribute is short for 'JOIN ON'.
Note that in .NET 2.0 the wizard will create two partial classes - Account.cs and Account.custom.cs. It is recommended that custom methods and properties such as the CurrentAddress property should be put in the second class (*.custom.cs).
b) The business object contains a collection of child objectsLets assume that the class created above is named Account with a primary key AccountID and we must add to it a collection of charges for the past six months (a collection of objects of type Charge). Then the steps are:
[On("AccountID")] [Where("CreateDate", Op.GreaterThan, DateTimes.Today, DateTimeOperation.AddMonths, -6)] public List<Charge> Charges;
Now when an Account object is loaded from the database it will automatically contain a collection of charges for the past six months.
Then, for example, to print the charges you can use code similar to this:
foreach (Charge charge in account.Charges) { System.Console.WriteLine(charge.ToString() + "\t on " + charge.CreateDate.ToShortDateString()); }
Walkthrough: Creating a business class manually.
Creating a business class manually is almost as easy as with the wizard.
using System; using Kredor.BO; /// <summary> /// This class is a base class for all classes bound to database DbMyCompany /// </summary> [DateSource("DbMyCompany")] public abstract class DbMyCompany : BusinessObject { public DbMyCompany() : base () { } }
using System; using Kredor.BO; /// <summary> /// Represents an account /// </summary> class Account : DbMyCompany { int _accountID; private string _name; private DateTime _createDate; private Address _currentAddress [On("AccountiD")] [Where("IsActive", Op.Equal, true)] public Address CurrentAddress { get { return _currentAddress; } set { _currentAddress= value; } } public string Name { get { return _name; } set { _name = value; } } public DateTime CreateDate { get { return _createDate; } set { _createDate = value; } } public Account() : base() { } protected override void OnLoad() { // Add code to be executed after the object is loaded. // Normally this should be empty. } // Add here business methods and joined or other custom fields and properties }