[英]Why use IOrganizationService instead of CrmServiceClient in Dynamics 365 XRM tooling SDK?
Microsoft sample code for accessing Dynamics often looks like this: 用于访问Dynamics的Microsoft示例代码通常如下所示:
static void Main(string[] args)
{
try
{
string connectionString =
"Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";
using (CrmServiceClient conn = new CrmServiceClient(connectionString))
{
// Cast the proxy client to the IOrganizationService interface.
IOrganizationService orgService = (IOrganizationService)conn.OrganizationWebProxyClient ??
conn.OrganizationServiceProxy;
Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)orgService.Execute(new RetrieveVersionRequest())).Version);
}
}
catch (FaultException<OrganizationServiceFault> osFaultException)
{
Console.WriteLine("Fault Exception caught");
Console.WriteLine(osFaultException.Detail.Message);
}
catch (Exception e)
{
Console.WriteLine("Uncaught Exception");
Console.WriteLine(e);
}
}
}
But it is equally possible (and simpler) to use the Crm Service Client directly, like this: 但是同样可以(并且更简单)直接使用Crm服务客户端,如下所示:
class Program
{
static void Main(string[] args)
{
try
{
string connectionString =
"Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";
using (CrmServiceClient conn = new CrmServiceClient(connectionString))
{
Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)conn.Execute(new RetrieveVersionRequest())).Version);
}
}
catch (FaultException<OrganizationServiceFault> osFaultException)
{
Console.WriteLine("Fault Exception caught");
Console.WriteLine(osFaultException.Detail.Message);
}
catch (Exception e)
{
Console.WriteLine("Uncaught Exception");
Console.WriteLine(e);
}
}
}
My question: Why use that IOrganizationService property ever? 我的问题:为什么要使用那个IOrganizationService属性? It seems as if it has only a subset of the functionality of the CrmServiceClient.
看起来好像它只有CrmServiceClient的一部分功能。 And CrmServiceClient used directly seems both faster, simpler, more efficient, and more feature-rich.
直接使用的CrmServiceClient似乎更快,更简单,更高效,功能更丰富。
Any idea about why the sample code always has this additional layer of indirection? 有关为什么示例代码总是有这个额外的间接层的任何想法?
Thanks. 谢谢。
IOrganizationService
is an interface
that defines the most basic methods required to access all Dynamics functions. IOrganizationService
是一个interface
,用于定义访问所有Dynamics功能所需的最基本方法。 There are a number of general benefits to using interfaces. 使用接口有许多一般的 好处 。
IOrganizationService
has been around since CRM 2011, whilst CrmServiceClient
was introduced around CRM 2016. A simple reason for using IOrganizationService
is that has been around much longer and is present in existing code bases. IOrganizationService
自CRM 2011以来一直存在,而CrmServiceClient
是围绕CRM 2016引入的。使用IOrganizationService
一个简单原因是,它已存在很长时间并存在于现有代码库中。
CrmServiceClient
implements IOrganizationService
, and also provides a range of other methods, eg authenticating with CRM. CrmServiceClient
实现了IOrganizationService
,还提供了一系列其他方法,例如使用CRM进行身份验证。 Before CrmServiceClient
was introduced we used CrmConnection
to authenticate to CRM. 在介绍
CrmServiceClient
之前,我们使用CrmConnection
对CRM进行身份验证。 When we had to migrate from CrmConnection
to CrmServiceClient
, we only had to change to CrmServiceClient
, extract IOrganizationService
and the rest of the code base remains the same. 当我们必须从
CrmConnection
迁移到CrmServiceClient
,我们只需要更改为CrmServiceClient
,提取IOrganizationService
,其余的代码库保持不变。
Programming to the IOrganizationService
interface makes your code far more portable, and reusable. 对
IOrganizationService
接口进行编程使您的代码更具可移植性和可重用性。 For example; 例如; when you don't know your service object is going to be created.
当您不知道将要创建您的服务对象时。
IOrganizationService orgService = IOrganizationService)conn.OrganizationWebProxyClient ?? conn.OrganizationServiceProxy;
For testing purposes when you want to mock IOrganizationService
with a new MockOrganizationService
class. 出于测试目的,当您想要使用新的
MockOrganizationService
类模拟IOrganizationService
时。
When you want to move code between an external application and a plugin. 当您想在外部应用程序和插件之间移动代码时。 In a plugin the
CrmServiceClient
is not provided. 在插件中,不提供
CrmServiceClient
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.