Skip to content

An experimental Unit of Work for concurrency & transaction control for Sequel

License

Notifications You must be signed in to change notification settings

choski7/sequel-uow

 
 

Repository files navigation

Sequel + Unit Of Work

This is an experiment (not yet thread safe!!!) that attempts to apply some PoEA best practices on top of Sequel's DataSet layer. The goal is being able to manage both transactions and concurrency with an ORM-agnostic aproach. Here you'll find some interesting patterns:

  • Repository
  • Mapper
  • Unit Of Work (UoW) with concurrency
  • A Registry for finding active UoWs

Installation

First install the gem via Bundler: gem 'sequel-uow', :git => 'git:https://github.com/nandosola/sequel-uow.git'

Tests

All tests can be run with the rake spec task. Individual tests can also be run via the rspec command, i.e.: rspec --color spec/001*

Sample usage

require 'sequel-uow'

# ...

String.inflections do |inflect|
  inflect.irregular 'company', 'companies'
  inflect.irregular 'local_office', 'local_offices'
  inflect.irregular 'address', 'addresses'
end

class Company < BaseEntity
  children :local_offices

  attribute :name, String
  attribute :url, String
  attribute :email, String
  attribute :vat_number, String
end

class LocalOffice < BaseEntity
  children  :addresses
  parent :company

  attribute :description, String
end

class Address < BaseEntity
  parent :local_office

  attribute :description, String
  attribute :address, String
  attribute :city, String
  attribute :state, String
  attribute :country, String
  attribute :zip, String
  attribute :phone, String
  attribute :fax, String
  attribute :email, String
  attribute :office, TrueClass, :default => true
  attribute :warehouse, TrueClass, :default => false
end

# ...
transaction = UnitOfWork::Transaction.new(Mapper::Sequel)
company_h = {
        name: 'FooBar, Inc',
        local_offices: [
            {
                description: 'branch1',
                addresses: [{description: 'addr1'}]
            }
        ]
    }

a_company = Company.new(company_h)
transaction.register_new(a_company)

UnitOfWork::Transaction handles aggregate states and their persistence:

  • register_new
  • register_clean
  • register_dirty
  • register_deleted
  • commit
  • rollback
  • complete

TO-DO

  • Validate state transitions
  • Example/test with complex (multi-object) transaction
  • Thread safety
  • Serialize state-keeping structures to files
  • Optimistic offline concurrency handler
  • RDocs & UML docs
  • Clearer tests
  • ...

See also

License

Licensed under the 3-clause BSD License. See LICENSE file for more details.

About

An experimental Unit of Work for concurrency & transaction control for Sequel

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Ruby 100.0%