The data mapper in Griffin Framework now includes a fluent api

The data mapper in Griffin.Framework have been updated to support fluent mapping and some other new features.

Like in previous versions you do not need to do anything special in your mappings if the table matches the class. Just create an empty mapping class:

public class MyMapper : CrudEntityMapper<MyEntity>
{
	public MyMapper() : base("TableName")
	{
	}
}

It’s when you need to handle differences that you need to customize the mapping (and only for the specific property). Like other data mappers you can set which column name the property has or if the
property is the primary key (composite keys are supported).

However, with the fluent api you can also convert between column and property values. For instance if the property is an int and the column is a string.

public class MyMapper : CrudEntityMapper<MyEntity>
{
    public MyMapper()
        : base("TableName")
    {
        Property(x => x.Id)
            .ToColumnValue(propertyValue => propertyValue.ToString())
            .ToPropertyValue(colValue => int.Parse(colValue.ToString()));
    }
}

Another new feature is that you can ignore a property for CRUD operations or for SELECT statements (or both):

public class MyMapper : CrudEntityMapper<MyEntity>
{
    public MyMapper()
        : base("TableName")
    {
        // property is considered read only.
        Property(x => x.Name)
            .NotForCrud();
    }
}

Finally I’ve updated the SqlCommandBuilder so that it returns the SCOPE_IDENTITY() for inserts (and the mapper assigns the ID to the PK).

var user = new User("Jonas");
_uow.Insert(user);
Console.WriteLine(user.Id); //assigned.

The convention is that the a property named Id will automatically be assigned. If your primary key is named something else you can specify that in the mapping.

  • Greg Carter

    I used your DAL tool on a few tables in the Northwind db. When I saw that the generated entity classes were basic classes with get set properties I thought there might be a chance I could use these as the ‘model’ classes for my WPF application using the MVVM approach. I’m using the Catel framework for MVVM. It has a base class that you CAN use for all your models, you don’t have to use it but it saves a lot of time if you do. It implements the INotifyOnPropertyChanged and if you use the Catel.Fody plugin it weaves all the properties into dependency-alike properties. So I tried setting the generated classes to inherit from Catel’s ModelBase class. The problem I ran into is that the mapper then picks up all the base classes properties and tries to map those. So I overrode the Mapper’s Configure to remove the base classes properties from the mapping. Things seem to be working on the db side (just running some of the generated test code). Before I go too much further, do you have any thoughts on if this is a good or bad idea? Thanks.