-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Class Library setup? #18
Comments
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 |
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:
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. |
Wondering if there is a way to setup Automapper for a class library type project?
The text was updated successfully, but these errors were encountered: