Mapping Semantics

The aim of this section is to familiarise you with the rapid mapping grammar. We are going to be looking at various attributes that we will use to map our Customer and Address class. In this example. And for those of us that does'nt understand .NET attributes and their usage; attributes are used to add metadata information into our code:

I will be using the customer and Address class for the example in this section. The following is how we intend to map customer class to the customer table in our database:

using System;
using PersistentManager.Mapping;  //Note: for entities, we need this using statements for Mapping.
using System.Collections.Generic;
using System.Collections;

namespace entities
{
    [Entity("Customer")]
    public class Customer
    {
        private IList customerOrders;
        private IList<Product> products;
        private Address address;

        [Key("Id", AutoKey = true)]
        public long Id
        {
            get;
            set;
        }

        [Field("FirstName", false, true)]
        public virtual string FName
        {
            get;
            set;
        }

        [Field("LastName", false, true)]
        public virtual string LName
        {
            get;
            set;
        }

        [OneToMany(typeof(CustomerOrders), RelationColumn = "CustomerId")]
        public virtual IList CustomerOrders
        {
            get
            {
                return customerOrders;
            }
            set { customerOrders = value;}
        }

        [OneToOne(typeof(Address), RelationColumn = "AddressId, Id", Cascade = Cascade.ALL)]
        public virtual Address Address
        {
            get{return address;}
            set{address = value;}
        }

        [Field("Country", false, true)]
        public virtual string Country
        {
            get;
            set;
        }

        [Field("Greeting", false, true)]
        public virtual string Message
        {
            get;
            set;
        }

        [Field("Picture", false, true)]
        public virtual byte[] MyPix
        {
            get;set;
        }        

        [ManyToMany(JoinTable = typeof(CustomerOrders), Type = typeof(Product), JoinColumn = "ProductId", OwnerColumn = "CustomerId", Cascade = Cascade.ALL)]
        public virtual IList<Product> Products
        {
            get {return products;}
            set{products = value;}
        }

        //Compulsory, we need a parameterless constructor per each entity
        public Customer()
        {

        }
    }
}

customerAdressclassdiagram.JPG




Introducing OORM (Object Oriented Relational Mapping)

Last edited Sep 21, 2008 at 11:41 PM by ahmedsalako, version 5

Comments

No comments yet.