Discriminatory Relationship

For example, let us look at the following class diagram :

discriminator.png

In the diagram above, the Product class is the base class of PerishableProduct and NonPerishable product. The Product class is markd with the Entity attribute while its subclasses are mapped with Discriminator attribute. Note the values in the access database with distinct values in the column discriminator with "Perishable" and "NonPerishabe" values that is used to identify the subclass of the Product class. Also note that in the inheritance tree, we are only binding to one table which is Product table but the discriminator values are different per subclass.

Example: saving a discriminated Relationship:


        PerishableProduct perishable = new PerishableProduct();
        perishable.ItemName = "FUFU";
        perishable.Price = 20;
        
        //Note that we are persisting the PerishableProduct. And this will generate "Perishable" in the discriminator column.
        manager.CreateNewEntity<PerishableProduct>(ref perishable);


Mapping a Discriminated Relationship. (The Base Product)


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

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

        [Field("Price", false, true)]
        public double Price { get; set; }

        [OneToMany(typeof(CustomerOrders), RelationColumn = "ProductId", Cascade = Cascade.ALL)]
        public virtual IList<CustomerOrders> CustomerOrders
        {
            get;
            set;
        }

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

        }
    }


Mapping a Discriminated Relationship. (The Sub-class PerishableProduct)


using System;
using PersistentManager.Mapping;
using System.Collections.Generic;

namespace entities
{
    [DiscriminatorValue("Discriminator", "Perishable")]
    public class PerishableProduct : Product
    {
        public PerishableProduct():base()
        {
            
        }
    }
}

Last edited Sep 28, 2008 at 12:19 PM by ahmedsalako, version 2

Comments

No comments yet.