Back in Q2 we released a beta version of the OpenAccess Fluent Mapping API and since have been constantly working on improving the experience based on the feedback that we got from our early adopters. While the API is still in beta it is much more mature and already provides most of the functionality provided by the visual designer.

Today we will try and get familiar with some of the changes and improvements to the API that have taken place in the Q2 to Q3 timeframe.

First of all we decided that some of the existing API  members should be changed. So the following methods have been renamed: Property is now HasProperty, Association is now HasAssociation, FromInverse is now WithOpposite and HasColumnName has become ToColumn. You will surely notice how the Fluent Mapping API has become more readable and easier to use.

With the new release we have also added some of the missing API, the new HasColumnType method will help you specify the type of the column you want to map to. 

animal.HasProperty(x => x.Name).HasColumnType("nvarchar(50)");

We have also provided support for specifying the type of internal identity mechanism for classes. The following code snippet maps the Order class with an internal identity with the HighLow key generator.

MappingConfiguration<Order> order = new MappingConfiguration<Order>();

The same API can be used for specifying a composite identity. However you should know that when using a composite identity you have to implement some special code in the class.

MappingConfiguration<Order> order = new MappingConfiguration<Order>();
order.HasIdentity(x => new { x.EmployeeId, x.CustomerId });

What is also new is the HasFieldName method which, as you might have already guessed, explicitly specifies the name of the field that is behind the property. The AsTransient method is also pretty self explanatory, it is used to make sure that a property is not mapped.

As you might recall, in Q2 defining associations with the Fluent Mapping API required for you to configure both navigational members and looked something like:

customerMapping.HasAssociation(c => c.Orders).WithOpposite(o => o.Customer);
orderMapping.HasAssociation(o => o.Customer).WithOpposite(c => c.Orders).HasConstraint((o, c) => c.Id == o.CustomerId);

We have worked on improving association handling and the same can now be configured with only the second line. Also now you can use the ToColumn method on an association, which will basically specify the column name of the foreign key column. The interesting thing though, is that you can even specify this on the property that is the collection. 

         .HasAssociation(x => x.Orders)
         .WithOpposite(o => o.Customer)

This code snippet will map the Customer property of the Order class to the CustomerId column in the Orders table. You can of course configure the association at  the opposite end. The following snippet will behave the exact same way: 

         .HasAssociation(x => x.Customer)
         .WithOpposite(o => o.Orders)

One of the new features we have added in the Fluent Mapping API is support for relationships that have only one end defined. Lets say for example that the Order class has a Customer property but the Customer class has no Orders property. This can be easily configured with code like: 

         .HasAssociation(x => x.Customer)

And while this turned out to be a rather long blog post, it only covers what’s changed since Q2. This is the first of a series of blog posts regarding the new features in the Fluent Mapping API, so make sure that you stay tuned for the following posts.

For the time being if you are impatient be sure to go ahead and try it out. If there is something you are wondering about just take a quick look at our online help as it has greatly matured from Q2 and is an awesome source of information about the Fluent Mapping API.

About the Author

Serge Ovanesyan

 is Technical Lead in Telerik Platform Team


Comments are disabled in preview mode.