[英]Custom services from IServiceProvider in D365
We have recently starting building applications using D365.我们最近开始使用 D365 构建应用程序。 This is a shift from java where we used spring for dependency injection.
这是从 java 的转变,我们使用 spring 进行依赖注入。 It seems that
IServiceProvider
does dependency injection, but I haven't found any way to make it work with our home grown services.似乎
IServiceProvider
进行了依赖注入,但我还没有找到任何方法让它与我们的本土服务一起工作。
We have code that looks like this:我们的代码如下所示:
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
// ... do stuff ...
}
and I want to add in the following line:我想添加以下行:
MyHomeGrownService myHomeGrownService = (MyHomeGrownService )serviceProvider.GetService(typeof(MyHomeGrownService));
However, I don't know how to register MyHomeGrownService
;但是,我不知道如何注册
MyHomeGrownService
; nor have I found much documentation on the topic.我也没有找到很多关于这个主题的文档。
Without proper dependency injection, developers have resorted to using new
in the code which is makes proper unit tests near impossible.如果没有正确的依赖注入,开发人员不得不在代码中使用
new
,这使得正确的单元测试几乎是不可能的。
How can I register custom services?如何注册定制服务?
For now, I created a delegating IServiceProvider
implementation which I use to wrap the given IServiceProvider
in every plug-in call.现在,我创建了一个委托
IServiceProvider
实现,用于在每个插件调用中包装给定的IServiceProvider
。 That custom implementation is aware of our services and will create those when ask.该自定义实现知道我们的服务,并会在询问时创建这些服务。 Everything else, it delegates.
其他一切,它代表。
I tried to make the delegating IServiceProvider
to be fast, but I have yet to try it in a live system, so I don't know how performant it is or isn't.我试图让委托
IServiceProvider
更快,但我还没有在实时系统中尝试过,所以我不知道它的性能如何。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.