Daniel Flower Dot Com Banner

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.
 
Comments for this page
LxfiqLxYfHjtg
posted by Duarte on 3/10/2012 8:08:59 p.m. (NZ time)
The History of : information hiotsry An automobile powered by a Otto gasoline engine was built in Germany by in 1885 and granted a patent in the following year. Although several other engineers (including Gottlieb Daimler, Wilhelm Maybach and Siegfried Marcus) were working on the problem at about the same time, Benz is generally credited with the invention of the modern automobile.The large-scale, production-line manufacturing of affordable automobiles was debuted by Ransom Eli Olds at his Oldsmobile factory in 1902. This assembly line concept was then greatly expanded by Henry Ford in the 1910s. Development of automotive technology was rapid, due in part to the hundreds of small manufacturers competing to gain the world's attention. Key developments included electric ignition and the electric self-starter (both by Charles Kettering, for the Cadillac Motor Company in 1910-1911), independent suspension, and four-wheel brakes.Although various pistonless rotary engine designs have attempted to compete with the conventional piston and crankshaft design, only Mazda's version of the Wankel engine has had more than very limited success.Since the 1920s, nearly all cars have been mass-produced to meet market needs, so marketing plans have often heavily influenced automobile design. It was Alfred P. Sloan who established the idea of different makes of cars produced by one company, so that buyers could move up as their fortunes improved. The makes shared parts with one another so that the larger production volume resulted in lower costs for each price range. For example, in the 1950s, Chevrolet shared hood, doors, roof, and windows with Pontiac; the LaSalle of the 1930s, sold by Cadillac, used the cheaper mechanical parts made by the Oldsmobile divisionSource:
Add your comment below
Your Name:
Comment Title:
Comment: