Best practice to isolate data access layer in Ruby/Rails -
so, have ruby on rails application. blank now. , let me right beginning of experience java, might thinking not way ror devs do. :-)
what need create data access layer, access users, let userdao.rb using activerecord or directly accessing database or accessing or key-value storage or else can think of.
technically, don't have interfaces in ruby, can make userdao.rb "have" implementation (basically, talking composition), may need, userdaoactiverecord.rb or userdaomongo.rb or else that. userdao.rb call methods of implementation , that's it. should easy switch between implementations.
while sound possible solution, looking forward hear best practices problem in ruby world. thanks!
you have ruby class other activerecord (which pointed out object relational mapper, has no separate data access layer).
you might want at: http://sequel.rubyforge.org/
you create class, person, contains methods use instance of sequel talk database.
this, untested code, demonstrates why might not great idea:
class person attr_reader :first_name, :last_name datasource = sequel.sqlite('my_app.db')[:people] def initialize(record) @first_name = record.first_name @last_name = record.last_name end # find record database , use initialize new person object def self.find_by_id(id) self.new(table.where(:id => id)) end end
Comments
Post a Comment