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
Allow running AutoMapper configuration in plugins #1754
Comments
Hi Andrei, Thanks for looking at this. I have put together a pull request showing my proposal for your interest. The only downside I can see is the need to reference AutoMapper in Nop.Core, but as long as you are not concerned about that (and I don't see any reason to be) then it should solve the plugin configuration problem. Looking forward to your input! |
Hi Andrew, I've just played with it to reproduce the issue. I've added one more AutoMapperStartupTaskTest and AutoMapperConfigurationTest to Nop.Web (not to Nop.Admin). And it worked fine. Both configurations were successfully registered (AutoMapperConfiguration from Nop.Admin and the new one - AutoMapperConfigurationTest) |
Thanks Andrei. Just to clarify, are you saying it worked fine on develop (as in, you couldn't reproduce the problem) or that it worked fine with the pull request? |
Contribution #1756 |
@andrewlock I test it on my local computer (before I've seen your pull request) |
Apologies Andrei, I'm not sure if you've understand the problem correctly, but do let me know if I'm just misunderstanding you! If you call Instead, we need to ensure all configurations are applied before calling The other problem as it stands is that every project and plugin that uses AutoMapper would need to reference the Nop.Admin project, in order to reference the I think my pull request deals with both of these issues. 😄 |
Andrew, I see now. Thanks a lot! I'll have a look |
…mappers (plugin developers can use it). Each mapper should implement IMapperConfiguration interface.
Andrew, done. Again thanks a lot for suggestion! P.S. I've refactored the implementation a bit. And made it more consistent with our other approaches (e.g. dependency registar) |
Excellent, you've definitely nailed a better implementation, thanks! 😄 |
Summary
We need a way to configure
AutoMapper
in plugins. I propose to pushAutoMapperConfiguration
into Nop.Core and add anAddConfiguration()
method.Explanation
I have just been working through upgrading our nopcommerce plugins from 3.70 to 3.80 and have run into one significant issue.
Previously, we were using AutoMapper extensively to map our Entities to ViewModels etc. We ran the configuration code in an
IStartupTask
that would do all the appropriateCreateMap
calls which worked smoothly.In the latest upgrade to AutoMapper 5.0, as I'm sure you realise, the static Mapper interface has been replaced with an instance method, and has a requirement for all configuration to be performed as part of a single initialisation method.
Currently you have a
IStartupTask
that performs all the configuration in the Nop.Admin project by callingAutoMapperConfiguration.Init()
. Somehow we need a decoupled way of providing additional configuration to theAutoMapperConfiguration.Init
method.There's a number of ways this could work, but one possibility would be to push the static
AutoMapperConfiguration
class down the project stack and into Nop.Core. It could have a public collection of configuration methods thatIStartupTasks
can add to, or better still anAddConfiguration(Action<IMapperConfigurationAction> configureAction)
method that pushes into a private list.You could then call
AutoMapperConfiguration.Init()
inNopEngine.Initialize()
once allIStartupTasks
have run, which will create a newMapperConfiguration
using the provided configActions, and callCreateMapper()
.It still feels a little messy, but I think it's possibly the cleanest solution?
I'm happy to put together a pull request if you agree.
The text was updated successfully, but these errors were encountered: