ruby-on-rails - Ruby 中的存储库或网关模式

标签 ruby-on-rails ruby design-patterns architecture

如何在 Ruby 中实现存储库或网关模式?

我来自 C# 世界,我通常抽象出我的数据访问,但是使用 ActiveRecord 作为 Ruby 中的默认数据访问机制,如何实现这一点并不明显。

我通常在 C# 中做的是使用抽象接口(interface),然后为 ECFustomerRepositoryNHibernateCustomerRepositoryInMemoryCustomerRepository 以及依赖具体实现在这种情况下我注入(inject)了匹配的具体实现。

那么现在,Ruby 方式是什么?!

据我所知,在动态语言中你不需要像 DI(依赖注入(inject))这样的东西。 而且 Ruby 具有强大的语言特性,可以实现诸如 mixins 之类的东西。

但是您会定义 mixin 以在类或模块级别静态使用吗?

如果我想针对内存中的存储库进行开发并且在生产中我会切换到我的 ActiveRecord-Repository,我该如何编写我的业务逻辑?

If 在这里可能走错了路,因为我习惯于用静态类型的语言思考。某人将如何以 Ruby 方式处理此任务?基本上我想让我的持久层抽象并且它的实现可以互换。

编辑:我指的是 robert c. martins (unclebob) keynote about architecture

感谢您的帮助...

最佳答案

我明白你在说什么。我也有 .NET 背景。抽象出你的业务逻辑和持久性逻辑是一个好主意。我还没有找到适合你的 gem。但是您可以轻松地自己滚动一些简单的东西。最后,存储库模式基本上是一个委托(delegate)给持久层的类。

这是我的做法:

require 'active_support/core_ext/module/attribute_accessors'

class GenericRepository

  def initialize(options = {})
    @scope = options[:scope]
    @association_name = options[:association_name]
  end

  def self.set_model(model, options = {})
    cattr_accessor :model
    self.model = model
  end

  def update(record, attributes)
    check_record_matches(record)
    record.update_attributes!(attributes)
  end

  def save(record)
    check_record_matches(record)
    record.save
  end

  def destroy(record)
    check_record_matches(record)
    record.destroy
  end

  def find_by_id(id)
    scoped_model.find(id)
  end

  def all
    scoped_model.all
  end

  def create(attributes)
    scoped_model.create!(attributes)
  end

private

  def check_record_matches(record)
    raise(ArgumentError, "record model doesn't match the model of the repository") if not record.class == self.model
  end

  def scoped_model
    if @scope
      @scope.send(@association_name)
    else
      self.model
    end
  end

end

然后您可以拥有一个 Post 存储库。

class PostRepository < GenericRepository

  set_model Post

  # override all because we also want to fetch the comments in 1 go.
  def all
    scoped_model.all(:include => :comments)
  end

  def count()
    scoped_model.count
  end

end

只需在您的 Controller 中的 before_filter 或初始化或任何地方实例化它。在这种情况下,我将其范围限定为 current_user,以便它仅获取这些记录并仅为当前用户自动创建帖子。

def initialize
  @post_repository = PostRepository.new(:scope => @current_user, :association_name => 'posts')
end

def index
  @posts = @post_repository.all
  respond_with @posts, :status => :ok
end

我遇到了 https://github.com/bkeepers/morphine这是一个微型的 DI 框架。它可能对你有用 :) 但是 DI 并不是 ruby​​ 中使用频繁的模式。此外,我实例化了我的存储库,以便将它们的范围限定为当前用户或其他用户。

我正在寻找正确的方法来完成您的要求,如果我真的找到了,我会写一些关于它的文章。但就目前而言,在持久性和我的 Controller 之间划清界线已经足够了。如果操作得当,以后切换到不同的系统不会有太大的麻烦。或者添加缓存等。

关于ruby-on-rails - Ruby 中的存储库或网关模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9365813/

相关文章:

ruby-on-rails - 如何在Rails中将 "3.days"转换成字符串 "3 days"?

ios - 带有参数 iOS 的单例

c# - 从数据库中检索属性的简单类型的创建模式?

ruby-on-rails - 使用简单表单将数据属性添加到自定义包装器

html - Bootstrap : How to center image in panel heading?

ruby - 如何在 Ruby 中处理 'reverse sum'?

objective-c - 处理类似方法的设计模式

mysql - 将 MySQL 切换到 Postgres : "column must appear in the GROUP BY clause or be used in an aggregate function"

ruby-on-rails - 带有 Heroku 的 Rails 不发送 Mandrill 电子邮件

ruby-on-rails - 使用 Rails 将日期字符串转换并保存到数据库时使用的格式