Active Records vs. Repository - 优缺点? [英] Active Records vs. Repository - pros and cons?

查看:14
本文介绍了Active Records vs. Repository - 优缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ActiveRecord 你可以定义一个这样的类:

Using ActiveRecord you might define a class like this:

class Contact
{
  private String _name;
  public String Name
  {
    get { return _name; }
    set 
    { 
      if (value == String.IsNullOrWhiteSpace())
        throw new ArgumentException(...);
      else
        _name = value;
    }
  }

  public Boolean Validate() { ... /* check Name is unique in DB */  }

  public Boolean Save() { ... }

  public static List<Contact> Load() { ... }
}

虽然这很好也很简单,但我发现我的类变得非常臃肿,因为大量的逻辑混合在一起!

Whilst this is nice and simple, I've found my classes become very bloated with a big mix of logic going on!

使用分层/域设计,您可以定义相同的类,例如:

Using a layered/domain design you might define the same class like:

class Contact
{
    [Required(AllowEmptyStrings=false)]
    public String Name { get; set; }
}

class ContactService : IService
{
    public List<Contact> LoadContacts() { return (new ContactRepository()).GetAll(); }
    public Contact LoadContact(int id) { return (new ContactRepository()).GetById(id); }
    public Boolean SaveContact(Contact contact)
    {
        if (new ContactValidator().Validate(contact))
            new ContactRepository().Save(contact);
    }
}

class ContactRepository : IRepository
{
    public List<Contact> GetAll() { ... }
    public Contact GetById(int Id) { ... }
    public Boolean Save(Contact contact) { ... }
}

class ContactValidator : IValidator
{
    public Boolean Validate(Contact contact) { ... /* check Name is unique in DB */ }
}

class UnitOfWork : IUnitOfWork
{
    IRepository _contacts = null;
    public UnitOfWork(IRepository contacts) { _contacts = contacts; }
    public Commit() { _contacts.Save(); }
}

它是如何从 Active Record => 分层设计迁移过来的?

How was it migrated from Active Record => layered design?

  • Name setter 中的实体级验证 => 仍然存在(通过 DataAnnotation 实现)
  • 业务逻辑/规则验证(唯一名称)=> 从实体移至新的单独 ContactValidator
  • 保存逻辑 => 移至单独的 Repository 模式类(也带有 UnitOfWork)
  • 加载逻辑 => 移至单独的存储库
  • 与 Repository 的交互是通过新的 ContactService 进行的(它将强制使用 ContactValidator、ContactRepository、UnitOfWork 等 - 反对让调用者使用 ContactRepository!).

我正在为这种分层设计寻求同行的认可/建议 - 我通常不会在 Active Record 类型之外进行设计!任何评论表示赞赏.

I'm looking for peer approval/suggestions for this layered design - I don't usually design outside of Active Record type! Any comment appreciated.

注意 - 这个例子故意简单(UnitOfWork 并没有真正被使用,并且 Repository/Validator 的newing 会以不同的方式处理).

NB - This example is deliberately simple (the UnitOfWork isn't really used and the newing of Repository/Validator would be handled differently).

推荐答案

这实际上取决于您的域逻辑的复杂程度.例如,如果我正在写一个简单的博客,那么活动记录就可以了,主要是应用程序正在保存和加载数据.其简单而活跃的记录模式是完成这项工作的正确工具.

It really depends on how complex your domain logic is. For example if I was writing a simple blog then active record will be fine, mostly the application is saving and loading data. Its simple and active record pattern is the right tool for the job.

但是,如果我正在为有许多复杂业务规则和流程的航运公司编写软件,那么使用存储库模式以及其他 域驱动设计模式将提供更易于维护的代码.

However if I was writing software for a shipping company in which there are many complex business rules and processes then using the repository pattern, along with other Domain Driven Design patterns will provide at much more maintainable code in the long run.

使用领域驱动设计,您将使用规范模式来实现您的验证.

Using domain driven design you would use the specification pattern to achieve your validation.

这篇关于Active Records vs. Repository - 优缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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