Nick,
Thanks for your reply.
Although the example is targeted I don't think the general problem is.
"How can I inject a dependency which requires data from the unit/
module it is injected into?"
Seems quite a generic thing to me - but that said I can't come up with
any other cases off the top of my head.
Your suggested modification works in this case - but for situations
using an RAII design accessors may not be usable - assume for the
general case that the information is required in the *construction* of
the dependency and cannot be passed later.
Perhaps I am over-complicating this - I guess that as dependency
injection implies that the dependent is no longer responsible for the
creation of the depended-on item, that the client code therefore
*must* be responsible for it. Hence must have access to the data
required for construction of that item.
Think I'm being a little obsessive about encapsulation here?? :-D
Thanks
--rob
On Aug 15, 7:16 pm, "Nick Parker" <
ni...@developernotes.com> wrote:
> Rob,
>
> It seems to be a very targeted problem, where you have a device
> communicating with hardware. Below I tried to markup what you may have been
> describing, with the adjustment that I threw an interface around your serial
> implementation by just defining pure virtual functions within the class. It
> seems as if, and I think you are pointing this out in your question, why do
> I need to expose the baud rate back up to the FooDevice? Is that correct?
> If so, I would consider either augmenting what really needs to be exposed to
> the ISerial, or writing a separate generic adapter that FooDevice could talk
> to and it in turn could delegate your baud rate information back into the
> Serial class.
>
> public class ISerial
> {
> public virtual void SetBaudRate(int rate);
> public virtual int GetBaudRate();
>
> };
>
> public class Serial : ISerial { // Implementation here...};
>
> public class FooDevice
> {
> public FooDevice(ISerial *serial){ // store pointer }
>
> }
>
> Nick
Parkerwww.developernotes.com
>