Hello Rapid

For consistency reasons, we have to follow the normal introduction to a new platform, and this is why we have introduced the hello world section. In this section, you will create a single console application, you will configure rapid and you will save , load , update and delete entity from the database.

With the following schema , using SQL Server database as an example : (The good old customer table from Northwind)

customer-northwind.PNG

You will create a new console application and add an entity class called Customer with the following properties and attributes. You will then need to reference the PersistentManager.dll from your project.

1. Configuring Rapid
            string connectionString = ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString;
            IConfiguration configuration = ConfigurationFactory.GetInstance(connectionString);
            configuration.CacheSettings(20, 1000);
            configuration.ProviderDialect = ProviderDialect.SqlProvider;

2. Map the Customer Class with Rapid Attributes

    [Entity("customers")]
    public class Customer
    {
        [Field("CompanyName")]
        public string Companyname { get; set; }

        [Field("Contactname")]
        public string Contactname { get; set; }

        [Field("Contacttitle")]
        public string Contacttitle { get; set; }

        [Field("Address")]
        public string Address { get; set; }

        [Field("City")]
        public string City { get; set; }

        [Field("Region")]
        public string Region { get; set; }

        [Field("Postalcode")]
        public string Postalcode { get; set; }

        [Field("Country")]
        public string Country { get; set; }

        [Field("Phone")]
        public string Phone { get; set; }

        [Field("Fax")]
        public string Fax { get; set; }

        [Key("Customerid", AutoKey = false)]
        public string Customerid { get; set; }
    }

The mapping above makes use of Entity, Key and Field attributes to map the customer class and properties to the customers table. The Entity attribute is to mark the Customer as an object representation of the Customers table. The Field attribute is used to map the columns of the Customers table to the customer object. The Key attribute requires you to map a property as a key. Now let us use rapid to create, update, delete and load an Customer entity.

3. Using Rapid to Create Update Load and Delete

            using (EntityManager manager = new EntityManager())
            {
                Customer customer = new Customer();
                customer.Address = "No 1 Rapid Street Okland avenue";
                customer.City = "N Land";
                customer.Companyname = "Consistent Soft";
                customer.Contactname = "Hello world";
                customer.Contacttitle = "Mr Rapid";
                customer.Country = "United OOP Platform";
                customer.Customerid = "RAPD009";
                customer.Fax = "6647785898";
                customer.Phone = "765778568";
                customer.Postalcode = "66574";
                customer.Region = "East/West/Anywhere";

                manager.OpenDatabaseSession(); //Opens new session/context . This will open database connection

                manager.CreateNewEntity<Customer>(ref customer); //Creates New Entity

                customer.Contactname = "Mrs Rapid"; //Change something

                manager.SaveEntity(customer, customer.Customerid); //Update the changed property

                customer = manager.LoadEntity<Customer>(customer.Customerid); //Load the entity

                manager.RemoveEntity(customer, customer.Customerid); //Delete or Remove the entity

            } //Commits and close the session. Flushes all changes bound to this transaction

Last edited May 8, 2010 at 6:57 PM by Chuks8, version 14

Comments

No comments yet.