Skip to content
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

Open
tokyo0709 opened this issue Sep 26, 2018 · 10 comments
Open

Class Library setup? #18

tokyo0709 opened this issue Sep 26, 2018 · 10 comments

Comments

@tokyo0709
Copy link

Wondering if there is a way to setup Automapper for a class library type project?

@MattHoneycutt
Copy link
Owner

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.

@tokyo0709
Copy link
Author

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.

@MattHoneycutt
Copy link
Owner

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...

@tokyo0709
Copy link
Author

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.

@MattHoneycutt
Copy link
Owner

Do all of your types that implement IMapFrom, IMapTo, or IHaveCustomMappings have public, parameterless constructors?

@tokyo0709
Copy link
Author

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.

@MattHoneycutt MattHoneycutt reopened this Sep 27, 2018
@MattHoneycutt
Copy link
Owner

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.

@tokyo0709
Copy link
Author

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.

@tokyo0709
Copy link
Author

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?

@tokyo0709
Copy link
Author

tokyo0709 commented Sep 28, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants