-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: multiple Interface trait implementations #29
Comments
That's an interesting case. I'll think about implementing it. The current architecture doesn't support this because you can only implement Component (effectively) once per type since the interface type is an associated type. |
I've done some experimentation and this is feasible, but adds some extra verbosity (have to be more explicit about which interface when talking about a component). Have you tried making a trait that requires both traits, and making a component out of that? Example: trait FizzBuzz: Fizz + Buzz {}
impl<T: Fizz + Buzz> FizzBuzz for T {}
#[derive(Component)]
#[shaku(interface = FizzBuzz)]
pub struct FooService;
impl Fizz for FooService {}
impl Buzz for FooService {} |
Hey @AzureMarker thanks for your answer! Your suggestion though, does not allow you to write components that depend only on one of the two interfaces. Something like: #[derive(Component)]
#[shaku(interface = BarInterface)]
pub struct BarService{
#[shaku(inject)]
fizz_service: Arc<dyn Fizz>,
} Several architectures like Hexagonal architecture or CQRS rely on declaring different interfaces that segregate the scope of operations. It's perfectly fine to implement them within a single concrete Moreover, this approach improves the testability of your services/repositories, because you can mock just an interface, with respect to the whole service. I surveid other dependecy-injection crates and some of them allow for this behavior ( |
I agree that this is useful, just concerned about ergonomics and introducing more complexity. Here's a possible workaround using the current shaku version that does what you want, though it's a little verbose: It's an interesting pattern though. If the re-implementation of |
I've been playing with
shaku
for weeks now and I do think this is a huge enabler for Hexagonal Architecture in Rust.The only limitation I came across is the inability to declare multiple
Interface
implementations for aComponent
struct. While in Rust would be perfectly possibile forstruct FooService
to implement the two traitsFizz
andBuzz
, I'm not able to annotateFooService
with something like:What do you think about it?
Best regards and keep up the good work! 💪
The text was updated successfully, but these errors were encountered: