简体   繁体   English

在面向服务的体系结构中为服务调用定义方法签名的最佳实践是什么?

[英]What is the best practice to define method signature in Service oriented architecture for service call?

What is the best practice to define service call prototype/signature while developing application in service oriented architecture. 在面向服务的体系结构中开发应用程序时定义服务调用原型/签名的最佳实践是什么。

For Example I want to create Service call to send email. 例如,我想创建服务调用以发送电子邮件。

Let say I have following object in my domain layer 假设我的域层中有以下对象

 [datacontract]
public class Email
{
      public string To { get; set; }
      public string From { get; set; }
      public string Message { get; set; }
      public string Subject { get; set; }

      //I am not going to use this properties in send email method
      public string OtherProp1 {get; set;}
      public string OtherProp2 {get; set;}
      public string OtherProp3 {get; set;}

  }

Should I create Service method signature like 我应该创建服务方法签名吗?

   public bool SendEmail(string from,string to, string subject,string message){//My Logic}}

Or 要么

   public bool SendEmail(Email myEmail){//My Logic}

I am leaning toward first option.(Having said that I know if object is way to complex than it make sense to pass whole object itself.) 我倾向于第一个选项。(已经说过,我知道对象是否复杂,而不是传递整个对象本身。)

Unfortunately, second option is less clear in this case and this is because of your Email class . 不幸的是,在这种情况下,第二种选择不太明确,这是因为您的Email class

Suppose I am to call your method. 假设我打电话给你的方法。 You make me pass an instance of the Email class. 你让我传递了一个Email类的实例。 I go to the class contract and find 7 properties. 我去了课程合同,找到7个属性。

And, how am I supposed to know which parameters are mandatory and which are optional? 而且,我怎么知道哪些参数是强制性的,哪些是可选的? From the docs? 从文档? Not a cleanest design if I have to consult the docs to make proper use of the API. 如果我必须查阅文档以正确使用API​​,那么这不是最干净的设计。

I would rather refactor your Email class to call it EmailRequest with all these optional parameters removed and then I would create yet another class EmailResponse if you ever need to use it as a return value of a service. 我宁愿重构您的Email类,将其EmailRequest ,删除所有这些可选参数,然后如果您需要将其用作服务的返回值,我将创建另一个类EmailResponse

I too vote for approach #2. 我也投票选择方法#2。

Since you mentioned 'service oriented architecture', you should create a DataContract to gain full control of what your clients see. 既然您提到了“面向服务的体系结构”,那么您应该创建一个DataContract来完全控制客户看到的内容。

Here, serialization is opt-in, so the 'unused properties' will not be sent over the wire. 这里,序列化是选择加入,因此'未使用的属性'不会通过线路发送。

Plus, you get other benefits like controlling the order of serialized members, specifying if fields are required or not, custom names, versioning and so on. 此外,您还可以获得其他好处,例如控制序列化成员的顺序,指定是否需要字段,自定义名称,版本控制等。 It just makes things obvious for the clients. 它只会让客户明白一切。

Also, the DataContractSerializer is supposedly 10% faster than the XmlSerializer . 此外, DataContractSerializer据称比XmlSerializer快10%。 More details on this blog, though I am not sure if approach #1 (primitive types) would use XmlSerializer or DataContractSerializer . 关于这个博客的更多细节,虽然我不确定方法#1(原始类型)是否会使用XmlSerializerDataContractSerializer

[DataContract(Name="MyEmail", Namespace="http://contoso.org/soa/datacontracts")]
public class Email
{
  [DataMember(Name="ToField", IsRequired=true, Order=0]
  public string To { get; set; }

  [DataMember(Name="FromField", IsRequired=false, Order=1]
  public string From { get; set; }

  [DataMember(Name="MessageField", IsRequired=true, Order=2]
  public string Message { get; set; }

  [DataMember(Name="SubjectField", IsRequired=false, Order=3]
  public string Subject { get; set; }

  public string OtherProp1 {get; set;}
  public string OtherProp2 {get; set;}
  public string OtherProp3 {get; set;}    
}

It's always better to go OOP way. 以OOP方式总是更好。 If email class is excessive, try to analyze and define another solution. 如果电子邮件类过多,请尝试分析并定义另一个解决方案。 Like this 像这样

   public class Email
    {
          //everything needed for service is extracted in another class
          public EmailAddressDetails {get;set}
          //I am not going to use this properties in send email method
          public string OtherProp1 {get; set;}
          public string OtherProp2 {get; set;}
          public string OtherProp3 {get; set;}

      }

and use service like this 并使用这样的服务

   public bool SendEmail(EmailAddressDetails email){//My Logic}}

How about writing both? 写两个怎么样? And simply having the first, call the second? 只是拥有第一个,拨打第二个?

I would make your Email object a [DataContract] and go with option two. 我会将您的Email对象设为[DataContract]并使用选项二。 It's my opinion that you don't want that many parameters for one service method. 我认为你不希望一个服务方法有那么多参数。

One of the important aspects of SOA is the contract so I would really vote against cluttering it with unneeded data and details which will cause it to deteriorate fast(er). SOA的一个重要方面是合同,所以我真的会投票反对用不需要的数据和细节来混淆它,这将导致它快速恶化(呃)。

The option offered by channs is pretty good as it concentrates on defining the contract explicitly and in details, but I'd also separate the classes used for contract purposes from the classes used internally to decouple and hide internal implementation details (I explain this in detail in the Edge Component pattern) 由channs提供的选项非常好,因为它专注于明确地和详细地定义契约,但我还将用于合同目的的类与内部使用的类分开以隐藏和隐藏内部实现细节(我详细解释了这一点)在边缘组件模式中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 远程调用Windows Service方法的体系结构 - Architecture for remoting to call a Windows Service method 实例化Web服务时的最佳做法是什么 - What is best practice when instantiating a web service 如何从实体框架和面向服务的架构入手? - How to start with entity framework and service oriented architecture? 在ASP.NET中的面向服务的体系结构中进行缓存 - Caching in Service Oriented Architecture in ASP.NET 层体系结构的最佳实践是什么? - What is the best practice for layer architecture? 这是什么类型的服务架构? - what type of service architecture is this? 存储库服务层搜索方法最佳实践。 - Repository Service layer Search Method best practice. .asmx服务:从Web方法内部最多调用300次方法的最佳方法是什么? - .asmx service : what is best way to call a method upto 300 times from inside web method? MVC验证 - 使用服务层保持干燥 - 最佳做法是什么? - MVC Validation - Keep it DRY with a service layer - What is best practice? 在 IdentityServer 中授权和验证工作人员服务的最佳实践是什么? - what is best practice for authorize and authenticate a worker service in IdentityServer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM