B
bhavya internship
Guest
Hello all,
I wanted to implement dependency injection in my project as part of the requirement.
Below is my understanding before proceeding with the implementation:
We have Windows Management Instrumentation (WMI) implementation as a separate interface and using that we are reading WMI data (i.e., machine name, bios serial no etc.). Here wmi_interface is the separate module and making it loosely coupled. So that I can inject this class into the top layer to use the wmi_interface functionality(i.e., manager_impl).
wmi_interface implementation:
wmidata.h
class wmi_interface
{
public:
virtual read_wmi_data() = 0;
virtual ~wmi_interface();
};
manager_impl implementation:
In the top layer i.e., in manager_impl class, we are injecting the wmi_interface as a smart pointer into the constructor.
manager.h file
class manager_interface
{
public:
virtual ~manager_interface();
virtual create_subscription() noexcept = 0;
};
// Below is the global function outside of the class
std::unique_ptr<subscription_manager_interface> get_manager_interface(
std::unique_ptr<wmi_interface> wmi);
}
manager.cpp file
class manager_impl : public manager_interface
{
public:
explicit manager_impl(std::unique_ptr<wmi_interface> wmi) :
m_wmi{ std::move(wmi) })
{
}
private:
std::unique_ptr<wmi_interface> m_wmi;
};
std::unique_ptr<manager_interface> get_manager_interface(std::unique_ptr<wmi_interface> &wmi)
{
return std::unique_ptr<manager_interface>(std::make_unique<manager_impl>(std::move(wmi)));
}
Using dependency injection, I wanted to make this application module as plug and play so that at the later point of time i can create some other interfaces like produt_data (to get the product information) and device_data (to get the device information). So that i can inject this data also to manager_impl class constructor.
Is my proposed implementation is valid?
Do we need interface with virtual function to implement dependency injection?
Could someone please help me injecting interface as smart pointer into the constructor using Dependency Injection is feasible?
And also in future if i am creating new interfaces, Can I inject those interfaces also in the constructor of the top layer?
Thanks in advance.
Continue reading...
I wanted to implement dependency injection in my project as part of the requirement.
Below is my understanding before proceeding with the implementation:
We have Windows Management Instrumentation (WMI) implementation as a separate interface and using that we are reading WMI data (i.e., machine name, bios serial no etc.). Here wmi_interface is the separate module and making it loosely coupled. So that I can inject this class into the top layer to use the wmi_interface functionality(i.e., manager_impl).
wmi_interface implementation:
wmidata.h
class wmi_interface
{
public:
virtual read_wmi_data() = 0;
virtual ~wmi_interface();
};
manager_impl implementation:
In the top layer i.e., in manager_impl class, we are injecting the wmi_interface as a smart pointer into the constructor.
manager.h file
class manager_interface
{
public:
virtual ~manager_interface();
virtual create_subscription() noexcept = 0;
};
// Below is the global function outside of the class
std::unique_ptr<subscription_manager_interface> get_manager_interface(
std::unique_ptr<wmi_interface> wmi);
}
manager.cpp file
class manager_impl : public manager_interface
{
public:
explicit manager_impl(std::unique_ptr<wmi_interface> wmi) :
m_wmi{ std::move(wmi) })
{
}
private:
std::unique_ptr<wmi_interface> m_wmi;
};
std::unique_ptr<manager_interface> get_manager_interface(std::unique_ptr<wmi_interface> &wmi)
{
return std::unique_ptr<manager_interface>(std::make_unique<manager_impl>(std::move(wmi)));
}
Using dependency injection, I wanted to make this application module as plug and play so that at the later point of time i can create some other interfaces like produt_data (to get the product information) and device_data (to get the device information). So that i can inject this data also to manager_impl class constructor.
Is my proposed implementation is valid?
Do we need interface with virtual function to implement dependency injection?
Could someone please help me injecting interface as smart pointer into the constructor using Dependency Injection is feasible?
And also in future if i am creating new interfaces, Can I inject those interfaces also in the constructor of the top layer?
Thanks in advance.
Continue reading...