Entity Splitting

Having understood what Table Splitting is all about, we would want the opposite of that strategy Entity Splitting to be supported by the framework, and for good reasons, Rapid Entity supports this strategy out of the box. Sometimes we want to map a single Entity class into multiple tables. This strategy is called Entity Splitting , meaning entity data's are contained in multiple tables. To bring this data's into one single Rapid annotated entity, we will follow strategies that the framework understands. Before we go ahead, let us examine the following table diagrams to give us a clearer picture of the problem statement.

Using the good old customer table from Northwind and a simple table called Little Customer to back up our claims.
entity-splitting.PNG

The diagram above shows a relationship between the Customer table and our own table Little Customer . How do we map the two tables into one single Entity . What SQL query will be generated for this kind of mapping, hold your breath we shall get there soon. Now... here is a simple to read mapping.

Merging Customer Table with Little Customer Table
    [Entity("Customers")]
    [EmbeddedEntity( Name = "LittleCustomer")]
    public partial class Customer
    {        
        [Key("CustomerID", AutoKey=false)]
        public virtual string CustomerID { get ; set; } 

        ... other mappings

        //Entity Splitting Mappings
        [Field( "FriendlyName" , TableRef = "LittleCustomer" )]
        public virtual string FriendlyName { get; set; }

        [Field( "Discriminator" , TableRef = "LittleCustomer" )]
        public virtual string Discriminator { get; set; }
    }

The above code makes use of the EmbeddedEntity( Name = "LittleCustomer") attribute at the class level, this is to make sure that the table LittleCustomer is embedded into the Customer entity, the query for both entities are are combined. You will also notice that the two properties are decorated with Field( "FriendlyName" , TableRef = "LittleCustomer" ) , this mapping is not different from any other Field mapping except for the TableRef property of the attribute, this tells rapid that the property mapped is derived from the Table Reference LittleCustomer .

Now let us issue a simple LINQ query to project the LittleCustomer

 var customers = from customer in manager.AsQueryable<Customer>()
                                where customer.FriendlyName != null
                                select new
                                {
                                    FriendlyName = customer.FriendlyName ,
                                    Discriminator = customer.Discriminator
                                };

                foreach( var customer in customers )
                {
                    Console.WriteLine( "{0} {1} " ,
                                            customer.FriendlyName ,
                                                customer.Discriminator );
                }

Generated SQL
SELECT  
       [em0].[FriendlyName] AS [R0],
       [em0].[Discriminator] AS [R1]
 
FROM  [Customers] [t0]  LEFT OUTER JOIN [LittleCustomer] [em0] 
      ON ( [t0].[CustomerID] = [em0].[CustomerID])

WHERE  [em0].[FriendlyName]  IS NOT NULL

You can issue a CRUD operation on the entity as well. Now let us see how RQL queries this entity.

                var t6 = manager.AsAlias<Customer>();
                var results = new From( typeof(Customer) ).As( t6 )
                                    .Where( t6.FriendlyName ).IsNotNull()
                                    .SelectNew( () =>
                                                new
                                                {
                                                    FriendlyName = t6.FriendlyName,
                                                    Discriminator = t6.Discriminator
                                                }
                                              ).Execute();

Generated SQL
SELECT  
       [em0].[FriendlyName] AS [R0],
       [em0].[Discriminator] AS [R1]
 
FROM  [Customers] [t0]  LEFT OUTER JOIN [LittleCustomer] [em0] 
      ON ( [t0].[CustomerID] = [em0].[CustomerID])

WHERE  [em0].[FriendlyName]  IS NOT NULL

Last edited Feb 8, 2010 at 12:58 PM by ahmedsalako, version 29

Comments

No comments yet.