Map Multi-Table Entities with OpenAccess ORM Q3'12 SP1

by OpenAccess ORM Team | Comments 4
We are happy to announce that Q3 2012 SP1 of OpenAccess ORM is now released. In addition to the number of bug fixes, we have included a bunch of useful enhancements. Check the entire release notes to see the full list of improvements shipped with the latest update. Here are the highlights:
  • Multi-Table Entities Mapping - the ability to map multiple tables to one single Domain Class
  • Additional API for working with String arrays Code-Only
  • GUID generator ranges bring easier data synchronization
Probably the most exciting new enhancement in OpenAccess Q3'12 SP1 is the Multi-Table Entities support. In order to get you acquainted with this new feature's usage, we would like to present to you a simple scenario that illustrates how it could come in very handy. Let’s assume you have the following tables in your database:
DB diagram

The design of the database above probably meant to fit a use case where you would need the CustomerMain data much more frequently so it contains the most important set of columns, whilst the CustomerDetail table contains those that are not mandatory and would be rarely used. Also it might be the case that the columns in each table are dozens, hundreds or even thousands.
As we would like to benefit from the complete flexibility of implementation offered by OpenAccess ORM, we would choose to go with Code-Only mapping using the Fluent Mapping API instead of the more simple Visual Designer. By default we define one Domain Class for each table, so we expose the following entry points for accessing the collections of objects:

public IQueryable<CustomerMain> CustomerMains
{
      get
      {
           return this.GetAll<CustomerMain>();
      }
}
 
public IQueryable<CustomerDetail> CustomerDetails
{
      get
      {
           return this.GetAll<CustomerDetail>();
      }
}

However, from the perspective of your business logic, it doesn't make much sense to have two different collections. In fact, we would like to have a single point of accessing the customer data, regardless of the database schema separation. Something like this:

public IQueryable<Customer> Customer
{
      get
      {
           return this.GetAll<Customer>();
      }
}

In this case we would prefer to have easy access to all the columns through this Customer class. For instance:

Customer customer = new Customer()
{
    Name = "John Smith", // from CustomerMain
    Type = "Individual", // from CustomerMain
    Country = "US"              // from CustomerDetail
};


This is where the new overload we have created for the ToColumn() method in Fluent API can prove its value. Previously the only argument in it was the name of the column mapped to the property, but now you can also optionally specify the table name. Thus, the mapping for the Customer type looks like:

MappingConfiguration<Customer> configuration = new MappingConfiguration<Customer>();
configuration.MapType(x => new { }).ToTable("CustomerMain");
  
configuration.HasProperty(x => x.Id).IsIdentity().HasFieldName("_id")
    .ToColumn("Id").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0);
//... the rest of CustomerMain properties
              
configuration.HasProperty(x => x.Country).HasFieldName("_country")
    .ToColumn("Country", "CustomerDetail").IsNullable().HasColumnType("nvarchar").HasLength(30);
//.. the rest of CustomerDetail properties

In other words, all we have to do in order to map a class to a table different than the master table defined with ToTable is to mention the name of the other table in the ToColumn statement. 

After this mapping is defined, you can just work with the Customer object and OpenAccess ORM will take care for the rest implicitly! 

We hope that the flexibility that this new feature provides "out of the box" will be useful in your applications. Download OpenAccess ORM Q3'12 SP1 and try it for yourself. Stay tuned as we will soon present the latest enhancements in the OpenAccess SDK!

Click to download your free copy of OpenAccess


Team Lead
Telerik OpenAccess ORM

4 Comments

David
That's great! Just what I'm in need of for our system. Only problem is, we have been using the visual designer for the last few years, and moving to the fluent mapping isn't an option.

How do we implement this with the visual designer?

Thanks.
Telerik Admin
Hi David,
The good news are that introducing this feature in the Visual Designer is already in our backlog. However, we would like to hear how many people would use it there - so if you want to increase the priority of this feature, please vote in our feedback portal for the item: 

http://feedback.telerik.com/Project/114/Feedback/Details/39237-visual-designer-support-for-multi-table-entities

We appreciate your feedback!
Phil Short
As I have no idea what fluent mapping is, the only option for me is the visual designer, so if it isn't available in there it isn't available for me.
Ivailo
Indeed, this feature is for the time being limited to our Code-Only users and we in the OpenAccess team recognize that. Feel free to vote for this functionality to enter the Visual Designer in the Ideas & Feedback portal here. We might be implementing it in the future based on the interest shown.

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by OpenAccess ORM Team - or - read latest articles in Developer Tools
Product Families