编程模式/建筑问题 [英] Programming pattern / architectural question

查看:84
本文介绍了编程模式/建筑问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个项目,我有一些其他实体的实体的BankAccount

I am currently working on a project where I have a BankAccount entity for some other entity.

每个银行账户作为参考,以银行实体,一个帐号和可选的IBAN。

Each bank account as a reference to a bank entity, an account number and optionally an IBAN.

现在因为一个IBAN可以被验证,我怎么能保证当IBAN设置一个账户是有效的。这将是一个干净的架构方法?我现在有没有参考任何其他层领域层,我喜欢这种清洁方法(我被Eric Evans的DDD的启发)。幸运的是可以在不访问任何外界系统进行验证IBAN所以在这种情况下,我能有类似

Now since an IBAN can be validated, how can I ensure that when the IBAN is set for an account is valid. What would be a clean architectural approach? I currently have a domain layer without any reference to any other layer and I like this clean approach (I was inspired by Eric Evans DDD). Fortunately the IBAN validation can be performed without accessing any outside system so in this case I could have something like

puclic class BankAccount
{
  public string Iban
  {
     set { // validation logic here }
  }
}

但现在我在想,如果IBAN验证需要,例如一个SQL服务器检查,或外部DLL我会用什么方法。我将如何实现这一点。我想创建一个被传递到服务的价值IBAN对象,这决定IBAN是否有效与否,它是集到的BankAccount实体后?
或者,我会创建一个允许instanstiate伊班人之前执行验证?

But now I was thinking what approach I would use if the IBAN validation requires an SQL server check for example, or an external dll. How would I implement that. Would I create an IBAN value object which is passed to a service, that decides whether the IBAN is valid or not and after that set it to the BankAccount entity? Or would I create a factory which is allowed to instanstiate IBANs and performs validation before?

感谢您的帮助!

推荐答案

我会用某种形式的反转的控制权。

I would use some form of Inversion Of Control.

要具体,我会叫IIBANValidator的接口。验证IBAN的各种手段应实现该接口。例如:

To be specific, I would have an interface called IIBANValidator. The various means of validating the IBAN should implement that interface. For example:

interface IBANValidator {
    Boolean Validate(string iban);
}

class SqlBanValidator : IBANValidator {

    public bool Validate(string iban) {
        // make the sql call to validate..
        throw new NotImplementedException();
    }

}

然后,我会在我的BankAccount类的方法,它接受一个实现IIBANValidator和IBAN号码,被结构类似(不被任何拉伸优化)的对象:

Then, I would have a method in my BankAccount class which accepted an object that implements IIBANValidator and the IBAN number and was structured like (not optimized by any stretch):

Boolean SetIBAN(IIBANValidator validator, String iban) {
  Boolean result = false;
  if (validator.Validate(iban)) {
    Iban = iban;
    result = true;
  }

  return result;
}

在这一点上你的BankAccount类就不必对你的校验器的依赖,你可以换出来的意愿,最终这是非常干净的。

At this point your BankAccount class would not have to have a dependency on your validators, you could swap them out at will, and ultimately it's very clean.

最后code看起来是这样的:

The final code would look something like:

BankAccount account = new BankAccount();
account.SetIBAN(new SqlBanValidator(), "my iban code");

显然,在运行时,你可以通过你想要的任何验证程序的实例。

Obviously at runtime you could pass any validator instance you wanted.

这篇关于编程模式/建筑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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