The new DataMapper attribute in Glass Mapper

The new DataMapper attribute in Glass Mapper

Late last year I got my pull request to Glass Mapper merged, adding a [DataMapper(Type)] attribute for use in attribute configuration of models.

Before this it wasn't possible to explicitly state which data mapper should be used for mapping a property. You had to do add the data mapper in the GlassMapperScCustom.CreateResolver() method.

I ended up creating this attribute as I was working on an internal library/module that was using Glass Mapper, which was meant to be used by other projects and as such didn't have access to the Glass Mapper configuration. All of the model mapping had to be done using attribute configuration (at least that way it wouldn't require any setup in consuming projects).

The [DataMapper(Type)] is part of Glass Mapper since v4.3.1.194

You use it like this:

[DataMapper(typeof (MyCustomDataMapper))]
public MyCustomType MyField { get; set;}