活动记录与存储库 - 优点和缺点? [英] Active Records vs. Repository - pros and cons?

查看:312
本文介绍了活动记录与存储库 - 优点和缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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(); }
}

它是如何从活动记录=>分层设计迁移?

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

  • 在名称二传手实体级验证=>仍然是(通过DataAnnotation ableit)
  • 在业务逻辑/规则验证(唯一名称)=>从实体转移到一个新的独立的ContactValidator
  • 在保存逻辑=>移动到一个独立的存储设备模式类(还带的UnitOfWork)
  • 在加载逻辑=>移动到一个独立的存储
  • 与存储库交互是通过一个新的ContactService(这将强制使用ContactValidator,contactRepository那样,的UnitOfWork等 - 而不是让呼叫者松散contactRepository那样)。

我在寻找同行的首肯/这个分层设计的建议 - 我通常不会设计活动记录类型之外!任何意见AP preciated。

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是不是真的使用和存储库/验证的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.

但是,如果我在编写软件的船运公司,其中有很多复杂的业务规则和流程,然后使用存储库模式,以及与其他领域驱动设计模式将提供在更容易维护code,从长远来看。

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.

这篇关于活动记录与存储库 - 优点和缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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