Code Example
The following is an example of how you could save objects and load objects from the database, in order to illustrate the type of code that would be written to handle common tasks.
In this example, we are looking at a Car object. A car has a "Make" (a string, which must be at least 1 character and at most 50), the year it was made (an integer, at least 1900, with no upper limit), a description (multi-line text), and an owner (a Person object).
The following code shows the C# class model which would be created using Shunde:
using System;
using Shunde.Framework;
namespace TestSpace
{
/// <summary>A Car object, which extends the Shunde DBObject class</summary>
public class Car : DBObject
{
private string make;
private int year;
private Person owner;
//...
// Get and Set methods go here
//...
/// <summary>Declares the fields, and their accepted values, for this class</summary>
static Car() {
// Declare a database table, named "Car", with the following columns:
DBTable tbl = new DBTable("Car", new DBColumn[] {
// The "make" is a string, with minimum length 1, maximum length 50
new DBColumn( "make", typeof(string), 1, 50 ),
// The "year" is an integer, null not allowed, minimum value 1900, no maximum value
new DBColumn( "year", typeof(int), false, 1900, null ),
// The owner of the Car, which is a DBObject type
new DBColumn( "owner", typeof(Person), false )
});
// Tell the Shunde Framework about this class
ObjectInfo.RegisterObjectInfo(typeof(Car), tbl);
}
/// <summary>Gets all the Car objects in the database filtered by the given <see cref="Person">owner</see></summary>
public static Car[] GetCarsBelongingTo(Person owner)
{
// To get all objects in the database is very easy:
// First get reference to the type of DBObject in question:
Type t = typeof(Car);
// Then get the info Shunde holds about this DBObject type:
ObjectInfo oi = ObjectInfo.GetObjectInfo(t);
// Get the "SELECT ... FROM ... " SQL from the Object Info, and append a where clause:
string sql = oi.GetSelectStatement() + " WHERE [Car].[ownerId] = " + owner.Id;
// Execute the query:
return (Car[])DBObject.GetObjects(sql, t);
}
}
}
As you can see, it is not a lot of code to create an object which can be saved and retrieved from a database! What's more, the above code was generated with a Shunde program which takes as input the name of the class and column information, and creates C# code (click the thumbnail to the right to see a screenshot of this program).
We can now create a new car, save it, and then retrieve all the cars:
using System;
using TestSpace;
/// <summary>An ASP.NET webpage</summary>
public class Default_aspx : TextSpacePage
{
public override void Start()
{
Person frank = GetPerson(); // some method to return a Person object
// Declare the values:
Car car = new Car();
car.Make = "BMW";
car.Year = 2006;
car.Owner = frank;
// Now save:
car.Save(); // this method is inherited from DBObject
// Now get all of Frank's cars:
Car[] franksCars = Car.GetCarsBelongingTo(frank);
foreach (Car aCar in franksCars)
{
// display to screen
}
// Now get the car whose ID is passed as a querystring value to the page:
Car anotherCar = new Car();
anotherCar.Id = GetIntParam("carId");
anotherCar.Populate(); // this method is inherited from DBObject
Response.Write("The car is: " + anotherCar.Name);
// anotherCar.Owner is instantiated but not populated, i.e. it has an ID,
// but the owner's name is not populated. We can populated it thusly:
anotherCar.Owner.Populate();
Response.Write(" and it is owned by " + anotherCar.Owner.FullName);
}
}
In the example above we have said that a car is owned by a person. But a company can also own a car, so how will that be represented? See the next page for details on how this problem is solved with Shunde - it is this solution that sets Shunde apart from other frameworks.
|