New to Hanami, and wanted to utilize the separation of responsibility that slices seem to provide. I have 2 slices that are touching “records” in the same database, but I know that slice A will not have to touch records in slice B, and vice versa.
From what I’ve read here, it seems like it’s not good design to separate the different repositories/relations into slices, but was wondering if there are more proper ways to split up these responsibilities.
Hey @mzhan! I’m not sure what you’re reading here, but I think it’s 100% fine to have separate repos in different slices. This makes great sense for separating concerns. Please go for it!
Having ROM relations split up into separate slices is something that might be challenging to achieve if you’re using a single ROM setup, but this is something we’ll look to address with our proper ROM integration that we’re planning for Hanami 2.2. For now, I think a middle ground of having a single set of relations, but repos split up into slices feels like a reasonable compromise.
Thanks @timriley, that makes sense to me. I’ve also been taking a look at this guide for splitting repositories A sneak-peak into dependency loading with Hanami and dry-container | Hanami Mastery, but when I create a repository in a slice, it seems to fail with key not found: "container". Is there a specific way I should add a dependency injection to give my slice access to the container?
something as simple as
module MySlice
module Repositories
class UsersRepo < ROM::Repository[:users]
def create(user_hash)
users.changeset(user_hash).map(:add_timestamps).commit
end
end
end
end
Upon further digging and document reading, I just had to add persistence.rom to my shared_app_component_keys config, and then include the “persistence.rom” in the repository.
I have repository base classes per slice that use ROM::Repository::Root with generic helper methods and also sets up the Entities module location.
Looks like
module MySlice
module Entities; end
class Repository < ROM::Repository::Root
include Deps[container: 'persistence.rom']
struct_namespace Entities
end
end
Then you can descend from that in the slice
module MySlice
class UsersRepo < Repository[:users]
def create(user_hash)
users.changeset(:create, user_hash).map(:add_timestamps).commit
end
end
end