How do you register subclasses of the DapperExtension ClassMapper to be used?

I just started playing with DapperExtensions and it looks very promising. However, I am confused about how to handle registering subclasses of ClassMapper.

I have both a custom PluralizedAutoClassMapper and a regular ClassMapper, and I'm trying to use both.

Here is my pluralized cartographer ...

public class CustomPluralizedMapper<T> : PluralizedAutoClassMapper<T> where T : class { private readonly Type[] SinglularTablePocoTypes = new []{ typeof(LibraryInfo) }; public override void Table(string tableName) { base.Table(tableName); if(SinglularTablePocoTypes.Any(type => string.Equals(type.Name, tableName, StringComparison.CurrentCultureIgnoreCase))) TableName = tableName; } } 

... and here the mapper is specifically for the LibraryInfo class

 public class LibraryInfoMapper : ClassMapper<LibraryInfo> { public LibraryInfoMapper() { Map(libraryInfo => libraryInfo.Name).Column("LibraryName"); Map(libraryInfo => libraryInfo.Description).Column("LibraryDescription"); AutoMap(); } } 

PluralizedAutoClassMapper I get the job by calling the following ...

 DapperExtensions.DapperExtensions.DefaultMapper = typeof(CustomPluralizedMapper<>); 

But I'm not sure how to use another at the same time. What am I missing?

+5
source share
1 answer

Ok, I figured it out. The problem was that I am using IoC, and my POCOs are in a different assembly from the mappings.

In short, my model has no idea and does not care about where and how it is stored. It defines only an interface that describes how it needs to interact with this repository. My DAL defines a class that implements this interface, and uses SQLite with storage support. Since mappings only make sense with respect to SQLite, this is where I defined mappings.

The problem is that DapperExtensions is reflected in the assembly, which defines the POCOs that are looking for their ClassMappers, but since mine were in a different assembly, they were not found.

However, you can tell DapperExtensions to search for external assemblies through the following line of code ...

 DapperExtensions.DapperExtensions.SetMappingAssemblies(new[]{ assemblyA, assemblyB, ...assemblyN }); 

So, since my mappings are defined in the same place as my static Mapper class (which has a static “Initialize” call), all I have to do is tell DapperExtensions to look there like that ...

 public static class Mappings { public static void Initialize() { DapperExtensions.DapperExtensions.DefaultMapper = typeof(CustomPluralizedMapper<>); DapperExtensions.DapperExtensions.SetMappingAssemblies(new[] { typeof(Mappings).Assembly }); } public class CustomPluralizedMapper<T> : PluralizedAutoClassMapper<T> where T : class { ... } public class LibraryInfoMapper : ClassMapper<LibraryInfo> { ... } } 

And now everything works!

Even better, since I can specify the table name in LibraryInfoMapper, there is actually no need for my CustomPluralizedMapper, and so I can just use the standard PluralizedAutoClassMapper, like this ...

 public static class Mappings { public static void Initialize() { DapperExtensions.DapperExtensions.DefaultMapper = typeof(PluralizedAutoClassMapper<>); DapperExtensions.DapperExtensions.SetMappingAssemblies(new[] { typeof(Mappings).Assembly }); } public class LibraryInfoMapper : ClassMapper<LibraryInfo> { public LibraryInfoMapper() { Table("LibraryInfo"); AutoMap(); } } } 

Done and done! There was ZERO documentation about finding this, so I hope this helps others!

+8
source

Source: https://habr.com/ru/post/1236197/


All Articles