MicroLite-ORM / MicroLite

MicroLite ORM framework

Home Page:microliteorm.wordpress.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convention Based Mappings

opened this issue · comments

Thank you for this beautiful framework. This looks really good and deserves much more attention.

I am aware that Microlite allows attribute-based mappings like an ORM. But, how can we use convention-based mapping for database identifiers which are:

  • plural table names (POCO class would be created singular),
  • database identifiers (tables, views, columns, functions) use underscore_case (POCO class and properties would be in PascalCase)

The motivation behind the convention-based mapping is to have clean DTO/POCO classes.

If Microlite supports this idea, could you please create a minimal documentation to show how we can achieve that? Any advice on this would be highly appreciated.

Thank you once again.

If I understand, you have the following?

CREATE TABLE [dbo].[Customers]
(
    [Customer_Id] [int] IDENTITY(1,1) NOT NULL
)
public class Customer
{
    public int CustomerId { get; set; }
}

Yes, mostly like that. I am using PostgreSQL. I have this:

CREATE TABLE core.customers
(
    customer_id                     SERIAL PRIMARY KEY,
    customer_code                   national character varying(12),
    customer_name                   national character varying(100)
);
public class Customer
{
    public int CustomerId {get; set;}
    public string CustomerCode {get; set;}
    public string CustomerName {get; set;}
}

The existing IsIdentifier convention will be OK, what you'll need to do is specify a custom ResolveColumnName function when you register the convention based mapping:

Configure.Extensions()
    .WithConventionBasedMapping(
        new ConventionMappingSettings
        {
            ResolveColumnName = (PropertyInfo propertyInfo) => 
            {
                return Regex.Replace(propertyInfo.Name, @"(?!^)(?=[A-Z])", "_").ToLower();
            }
        });

This is the result

CustomerId -> customer_id
CustomerCode -> customer_code
CustomerName -> customer_name

For further information around customising the mapping conventions, see the wiki

Many thanks.

I think that this would be very useful to be included in the wiki. Especially for PostgreSQL users because PostgreSQL Server is case-sensitive which forces to use underscore convention.

Feel free to update the wiki or even write a blog post for the MicroLite blog if you like?

Definitely Trevor. Will do that once I have a fair understanding of MicroLite.

I've added an issue to simplify this for you going forward #422 which we will add in MicroLite 7