Last active
May 13, 2020 10:34
-
-
Save msusur/e34be94cbceac20ea364 to your computer and use it in GitHub Desktop.
Dynamics CRM Dependency Injection Workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class SuperPlugin : IPlugin{ | |
private static Lazy<IDependencyResolver> DependencyResolverTrunk = new Lazy<IDependencyResolver>(ResolverFactory); | |
public void Execute(IServiceProvider serviceProvider){ | |
// initialize a static container instance if not available | |
var containerWrapper = new ContainerWrapper{ | |
Container = serviceProvider.GetService(typeof(IPluginExecutionContext)), | |
Resolver = DependencyResolverTrunk.Value | |
}; | |
OnExecution(containerWrapper); | |
} | |
public abstract void OnExecution(IDependencyResolver resolver); | |
private static IDependencyResolver ResolverFactory(){ | |
var resolver = new Resolver(); | |
Setup(resolver); | |
return resolver; | |
} | |
private static void Setup(IDependencyResolver resolver){ | |
resolver.Register<IService, Service>(); | |
} | |
} | |
public class MyPlugin : SuperPlugin, IPlugin{ //you need to specify the IPlugin here as well because of a bug on Plugin Registration Tool. | |
public abstract void OnExecution(IDependencyResolver resolver){}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment