Comments (12)
Hi Amit,
The new version of Hypodermic takes you by the hand. Basically, when you wrote
builder.registerType< Driver >().as< IDriver >();
builder.registerType< Car >(CREATE(new Car(INJECT(IDriver))))
.as< ICar >()
.singleInstance();
now you can write
builder.registerType< Driver >().as< IDriver >();
builder.registerType< Car >().as< ICar >().singleInstance();
because you no longer need to describe the look of the constructor of Car.
More specifically, you only have to register types when you need to configure them. That is, their lifetime, or when they have to be known as an interface, or they have a very specific constructor not accepting instance types that Hypodermic can inject.
As a last example, if IDriver didn’t exist, well, if Car was accepting a Driver, you could only describe Car
builder.registerType< Car >().as< ICar >().singleInstance();
and if Car didn’t need to be known as ICar or be a single instance, you could just resolve Car without registering anything (doing so is harmless of course).
If you have any further questions, feel free to get back to me.
Thanks for the cheerings, it is always appreciated.
from hypodermic.
Thanks Yohan,
I tried following your guidelines, and i'm getting this error:
ContainerBuilder.h:27:81: required by substitution of 'template<class T> typename
Hypodermic::RegistrationDescriptorBuilder::ForTypeConstruction<T>::Type&
Hypodermic::ContainerBuilder::registerType() [with T = FG::S2CMessageDispatcher]'
static assertion failed: Could not autowire T: you should consider registering T either by
providing an instance or an instance factory
on this line:
builder.registerType< S2CMessageDispatcher >().as< IS2CMessageDispatcher >().singleInstance();
This is the class:
class S2CMessageDispatcher : public IS2CMessageDispatcher
as you can see - it's implementing IS2CMessageDispatcher
.
Can you help me understand what's the issue?
Thanks,
Amit
from hypodermic.
Can you show me the ctor of your class? (Is it public?)
from hypodermic.
It looks something like this:
S2CMessageDispatcher(std::shared_ptr<IA>a_handler,
std::shared_ptr<IB> b_handler,
std::shared_ptr<IC> c_handler,
std::shared_ptr<ID> d_manager,
.....
with ~40 dependencies like this, all interfaces.
from hypodermic.
I assume the order in which i register the types doesn't matter, right?
from hypodermic.
It doesn't.
from hypodermic.
Well, this is it. Have a look at Config.h
Hypodermic is configured to inject up to 20 dependencies by default. You might be able to raise it to whatever the number you want.
from hypodermic.
That solved it, thanks.
One more thing..
i had a registration like this:
builder.registerType< CommandlineParser >
(
[=](IComponentContext&)
{
return new CommandlineParser(argc, argv);
}
).as< ICommandlineParser >().singleInstance();
How should i implement it now?
from hypodermic.
Is it equivelent to this:
builder.registerInstanceFactory
(
[=](Container&)
{
return std::make_shared< CommandlineParser >(argc, argv);
}
).singleInstance();
If i try this - i'm getting this compilation error:
/home/amitkanfer/dev/unified_fireglass/vb/3rd_party/Hypodermic/
ProvidedInstanceFactoryRegistrationDescriptor.h:65:50: error: this context->m_instanceFactory =
m_instanceFactory;
thanks alot :)
from hypodermic.
Yes, so I should investigate... What kind of unit test can I build to make this fails like your code?
from hypodermic.
Thank you very much for pointing out this issue. You are all set. Thanks again
from hypodermic.
Thanks alot!
from hypodermic.
Related Issues (20)
- Visual Studio 2017 does not build lib HOT 3
- Support for C++17 HOT 2
- how to register a constructor that takes an argument
- GUN C++ 4.7.3 Compiler error HOT 1
- Resolve other than shared_ptr (e.g. unique_ptr) HOT 5
- Current syntax for register factory with arguments HOT 5
- Pass parameters to register type HOT 1
- Resolve a chain of instantiate of explicit dependencies name?
- Compile errors: multiple definition of boost::*
- static assertion failed: TArg should be a complete type HOT 5
- [FeatureRequest] Fluent lifetime management HOT 1
- Autowiring of factory methods HOT 2
- Question: register two interfaces for same concrete class HOT 2
- Fail to build in window boost1.7 gcc9
- Bug: interface is resolved twice from nested container if they are added as both .as<interface>() and .asSelf() HOT 2
- Bug: singleInstance not working as expected when resolving from nested containers HOT 2
- How to inject a factory for named instances? HOT 3
- Is it possible to specify the dependency explicitly while resolving HOT 4
- Issue when resolving same types in different shared libraries HOT 1
- ContainerBuilder doesn't have a virtual destructor HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hypodermic.