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
Using fruit with a templated class? #6
Comments
You can bind an instantiation of the template, but not the template itself. If this doesn't solve the question, can you please explain what are you
|
Let's say this is my code: namespace base {
template<typename Model>
class DAO {
INJECT(DAO());
};
}
namespace first_component {
template<typename Model>
class AugmentedDAO : DAO<Model> {
INJECT(AugmentedDAO());
};
}
// then:
namespace model {
class Article { ... };
}
namespace project {
class Project {
...
fruit::Component<...> component()
{
return fruit::createComponent()
.bind<base::DAO, first_component::AugmentedDAO>();
}
}
// expecting to get a first_component::AugmentedDAO here
class Foo {
INJECT(Foo(base::DAO<Article>));
};
} I'd like to be able to inject a |
You left out quite a lot of types and the injector, so I'm not sure where in what component do you expect all the DAO<T> to AugmentedDAO<T> bindings to happen. Also you showed only 1 One possible feature that might help in this case (but that is not part of Fruit ATM) would be to allow you to specify a trait-like class describing your templated binding, e.g.: template <typename T> // This is basically a map from DAO<T> to AugmentedDAO<T> and then have a new method in Fruit's PartialComponent class so that you can use that like: fruit::Component<FooInterface> getFooComponent() { However this bindResolver() would be needed in all components where you need some binding DAO<T> -> AugmentedDAO<T>, so they'd need to include the definition of DAOResolver which in turns needs the definition of AugmentedDAO to be included (and all the things needed for its implementation). |
what I have in my "real code" is a connector module, that's agnostic about everything else around, and provides the base DAO. Then I have a versioned "extension" ("first_component" in my example), providing VersionedDAO, that's in another module. When that module is loaded, its fruit component should replace DAO with VersionedDAO. Then I have the "application modules", which are not tied together at all, and only have a dependency on the base connector module. They do not know about VersionedDAO, nor should they have to. Only Fruit should know about the swapping of DAO->VersionedDAO (Well, and the versioning extension), and the application modules should get versioning without knowing about it. |
When do you expect all the instantiations VersionedDAO, ...,
If the above 3 are satisfied, Fruit could potentially do it for you, but it I'd consider using some kind of non-templated pure virtual interface to class DaoHelper { class V1DaoHelper : public DaoHelper { template void doStuff() { class Article {...}; class SomeClass { public: // Then you can bind DaoHelper to V1DaoHelper in a get*component function. INJECT(SomeClass(Dao articleDao)) : articleDao(articleDao) {} ... Then you can have separate compilation (and independent modules) for |
Sorry I took so long to answer on this. Your post made me realize quite a few things weren't in place in our codebase, and we refactored it all pretty heavily to have something that works much better. Before, we were using "implicit binding" quite extensively, we've now changed everything to use components. We ended up using SFINAE on a templated component function, to select one overload (that would Thank you! |
Great, happy to hear that! On 15 December 2015 at 09:14, ven notifications@github.com wrote:
|
Hi,
in the codebase I'm working with, I have a templated class I'm trying to get injected. Think something like this:
I'd like to be able to
.bind()
this in one of my components, but it seems I can't find a way to do it. Is there someway to tell Fruit it's going to be injected with a template argument? Can I absolutely not do this? If so, any workaround available?Thanks!
The text was updated successfully, but these errors were encountered: