契约优先 SOA:设计业务领域:WCF [英] Contract-First SOA: Designing Business Domain: WCF

查看:43
本文介绍了契约优先 SOA:设计业务领域:WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WCF 构建一个全新的系统.我将使用契约优先方法来构建基于面向服务的概念的服务.我有一个返回用户银行帐户详细信息的服务操作.该帐户可以是FixedAccount"或SavingsAccount"类型.我设计的服务如下.

I am building a completely new system using WCF. I am going to use Contract-First Approach for a service which is to be built based on Service Oriented concepts. I have a service operation that returns a bank account details of a user. The account can be of type "FixedAccount" or "SavingsAccount". I have designed the service as follows.

[ServiceContract]
interface IMyService
{
[OperationContract]
AccountSummary AccountsForUser(User user);
}


[DataContract]
class AccountSummary
{
 [DataMember]
 public string AccountNumber {get;set;}

 [DataMember]
 public string AccountType {get;set;}
}

这么多就好了.

现在,我需要为此服务开发业务域.我能想到两种选择(任何新方法总是受欢迎的)

Now, I need to develop the business domain for this service. I can think of two options (any new approach is always welcome)

1) 方法 1:提出一个 BankAccount 基类.从它派生的专门类是FixedAccount"和SavingsAccount".BankAccount 将有一个方法 Transfer(string toAccount).这成为我们熟悉的 &有效的 OOAD.这涉及用于在 AccountSummary DTO 和 FixedAccount/SavingsAccount 域类之间映射的映射器.

1) Approach 1: Come up with a BankAccount base class. The specialized classes derived from it are "FixedAccount" and "SavingsAccount". The BankAccount will have an method as Transfer(string toAccount). This becomes our familiar & effective OOAD. This involves mapper for mapping between AccountSummary DTO and FixedAccount/ SavingsAccount domain classes.

2) 方法 2:不使用映射器翻译层.

2) Approach 2: Without using mapper translation layer.

问题

1) 假设我正在使用方法 1.是否有任何文章/教程解释了如何根据 DTO(条件映射)中的 AccountType 值将 AccountSummary DTO 映射到 FixedAccount/SavingsAccount 域类?

1) Suppose I am using approach 1. Is there any article/tutorial that explains how to map AccountSummary DTO to FixedAccount/ SavingsAccount domain classes based on the AccountType value in DTO (conditional mapping) ?

2) 如何完成方法 2 中的任务?

2) How do I achieve the task in approach 2 ?

阅读:-

  1. http://www.soapatterns.org/service_facade.php

SOA 架构数据访问

在 WCF 中设计服务和操作

WCF 数据协定和参考实体数据?

逻辑何时属于业务对象/实体,何时属于服务?

推荐答案

首先 - 您需要了解是否真的需要成熟的 SOA.

First of all - you need to understand if you really need full-blown SOA.

SOA 基本上意味着每个操作都通过将我们的系统与其他系统分离的服务.在极少数情况下(以防应用程序变得非常庞大)- 系统的一部分来自另一部分.

SOA basically means that every operation is going through service that decouples our system from other system/s. In rare cases (in case application grows extra huge) - one part of system from another.

您的应用程序会与任何其他应用程序交谈"吗?

Will your application "talk" with any other application?

如果不是,而且您只是在构建单体网站,请放开您的思想并减少 SOA 废话.否则你最终会得到无用的抽象层.

If not and you are just building monolith website, free your mind and cut that SOA bullcrap. Otherwise you will end up with useless abstraction layer.

这是您可以应用第二种方法的唯一方法,因为如果不将域模型映射到其他东西,您就无法将其完全解耦.

That is the only way you can apply 2nd approach because you can't decouple domain model completely without mapping it to something else.

如果确实需要 SOA - 我们必须封装,将我们的域模型隐藏在外部世界之外.这意味着 - 必须有某种从我们的模型到 DTO 的映射.

In case there really is a need for SOA - we must encapsulate, hide from outer world our domain model. That means - there must be some kind of mapping from our model to DTOs.

是否有任何文章/教程解释如何将 AccountSummary DTO 映射到 FixedAccount/SavingsAccount

Is there any article/tutorial that explains how to map AccountSummary DTO to FixedAccount/ SavingsAccount

映射本身并不复杂.这是映射对象的一种简单方法:

Mapping itself isn't complex idea. Here's one simple way to map objects:

class AccountSummary{
  public string InterestingThing {get; set;}
  public string AnotherThing {get; set;}
}
class AccountSummaryMapper{
   public static Map(BankAccount a){
    return new AccountSummary{
        InterestingThing=a.SomethingSomething,
        AnotherThing=a.Something.Else.ToString()
      };
   }
}
var accountSummary=
  AccountSummaryMapper.Map(myBankAccount);

这似乎无效.Automapper 等对象到对象映射器可以提供帮助.通过教程,它应该足以让你开始.想法并不难 - 您创建 Maps,在应用程序启动时将它们告诉 Mapper,然后使用 Mapper 通过给定的配置映射您的对象.

This might seem ineffective. Object-to-object mappers like Automapper can help. Go through tutorial, it should be good enough to get you going. Idea ain't hard - you create Maps, tell Mapper about them on application start and then use Mapper to Map your objects by given configuration.

另外 - 考虑映射方向.良好的面向对象代码通常意味着您要么提出问题,要么告诉对象去做的事情.对象不应该知道其他对象职责的内部工作原理.

Also - think about mapping direction. Good object oriented code usually means that you either ask questions or tell object to do stuff. Objects shouldn't know about inner workings of other object responsibilities.

以此类推 - 父母完成孩子的家庭作业是不好的,因为孩子不会学到任何东西,父母会背负不必要的工作.相反 - 父母应该强迫孩子自己工作.

In analogy - it is bad for parents to complete their child homework because child won't learn anything and parent will be burdened with unnecessary work. Instead - parent should force child to work on his own.

将 AccountSummary 直接映射到 BankAccount 并重新设置其状态就像做功课一样.应该不需要这样的映射.相反 - 将 BankAccount 告诉 BankAccount.DoHomework(pencil, copybook, someStrongWords).

Mapping AccountSummary directly to BankAccount and re-setting its state is like doing homework. There shouldn't be need for such a mapping. Instead - tell BankAccount to BankAccount.DoHomework(pencil, copybook, someStrongWords).

开发业务领域

您不开发业务领域.您开发的领域模型只是业务领域的反映,用于解决特定问题.

You do not develop business domain. You develop domain model which is just a reflection of business domain, made to solve particular problems.

这篇关于契约优先 SOA:设计业务领域:WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆