HeroicFramework
HeroicFramework copied to clipboard
Class Library setup?
Wondering if there is a way to setup Automapper for a class library type project?
You can certainly use AutoMapper in a class library project, and the latest version of Heroic.AutoMapper should work just fine. You'd just want to call HeroicAutoMapperConfigurator.LoadMapsFromAssemblyContainingTypeAndReferencedAssemblies
somewhere in your application's main method, before you want to use AutoMapper.
Gotcha. If the calling application was an Azure function would this cause a problem? I can't seem to get that call to work when I throw it in the beginning of an Azure function.
Hmm, I haven't tried that. Does the call throw an error? Or does it just fail to load the maps? If it's failing to load the maps, it's possible that some artifact of it running in Azure Functions is interfering with it's ability to reflect through the maps...
Yeah. The call throws an error saying that there is no parameterless constructor for it if I remember right. I'll have to get more details when I look at it again but it didn't seem to get the mappings at all.
Do all of your types that implement IMapFrom, IMapTo, or IHaveCustomMappings have public, parameterless constructors?
Ah ok yes this could be part of the problem. The class I was mapping implemented IHaveCustomMappings and properties that had getters only and only one parameterized constructor. For this reason I was attempting to setup a custom mapping that would use that constructor but I am sure I may have been doing this incorrectly.
Yup, that would definitely do it. HeroicAutoMapper requires that your mapping classes have public constructors.
What you could do in this case is define a separate mapping config class that implements IHaveCustomMapping. Something like this:
Model:
public class SomeModel {
public string Something {get; private set; }
public int SomethingElse {get; private set; }
public SomeModel(string something, int somethingElse) {
Something = something;
SomethingElse = somethingElse;
}
}
Mapping config:
public class SomeModelMappingConfig : IHaveCustomMappings
{
public void CreateMappings(IMapperConfiguration configuration)
{
configuration.CreateMap<Something, SomeModel>()
// Mapping config here
}
}
I've had to do this a few times in the past. I typically put the config class and the model in the same file so that the relationship is easily (re-)discoverable.
I hope that helps! I'm going to leave the issue open for now, feel free to close if this works for you.
I just tested this and it worked perfectly. Thank you for the help! I love this library and am stoked to get it working with Azure functions as well.
Ok so I just ran into another problem where I have multiple Azure functions but I don't know which one is going to get hit first. In the constructor of all of them I am making that call to load the maps from the assemblies but it can only be called once it seems like or it will error out. Is there anything built out that would allow me to check if that has already been hit once?
Scratch that I figured out a workaround by building a static initializer. (Sorry to drag this out)
public static class AutomapperInitializer
{
private static bool _initialized = false;
public static void Configure()
{
if (!_initialized)
{
HeroicAutoMapperConfigurator.LoadMapsFromCallerAndReferencedAssemblies();
_initialized = true;
}
}
}
And then calling,
AutomapperInitializer.Configure();
In each azure function constructor.