Windsor / null dependency injection

88 views
Skip to first unread message

Scott_M

unread,
Jul 25, 2012, 5:12:53 PM7/25/12
to castle-pro...@googlegroups.com
Ran into a recent case where one of my new apps is using a component where a constructor based dependency of that component is not necessary (IBar) and would be painful to inject (lot of projects/related projects to import/build).  Here is a simplified example:

public MyService : IService
{
  private IFoo _foo;
  private IBar _bar;

  public MyService(IFoo foo, IBar bar)
  {
    _foo = foo;
    _bar = bar;
  }
}

So lets say that the IBar dependency is not needed for the current application but IFoo is.  Is there a good way to inject NULL for that dependency using windsor config / XML / Nullable<T>?    How is this done?


thanks




Krzysztof Koźmic

unread,
Jul 25, 2012, 5:14:17 PM7/25/12
to castle-pro...@googlegroups.com

Create another constructor with just foo

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/i7nkYXi8mp0J.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

Scott_M

unread,
Jul 25, 2012, 5:19:51 PM7/25/12
to castle-pro...@googlegroups.com
How does Windsor deal with multiple constructors?  How can it determine which one to call?   In my case Application A may not need IBar but Application B may need it.


thanks




On Wednesday, July 25, 2012 4:14:17 PM UTC-5, Krzysztof Koźmic wrote:

Create another constructor with just foo

On Jul 26, 2012 7:12 AM, "Scott_M" <scott.kendall.mcfadden@gmail.com> wrote:
Ran into a recent case where one of my new apps is using a component where a constructor based dependency of that component is not necessary (IBar) and would be painful to inject (lot of projects/related projects to import/build).  Here is a simplified example:

public MyService : IService
{
  private IFoo _foo;
  private IBar _bar;

  public MyService(IFoo foo, IBar bar)
  {
    _foo = foo;
    _bar = bar;
  }
}

So lets say that the IBar dependency is not needed for the current application but IFoo is.  Is there a good way to inject NULL for that dependency using windsor config / XML / Nullable<T>?    How is this done?


thanks




--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/i7nkYXi8mp0J.
To post to this group, send email to castle-project-users@googlegroups.com.
To unsubscribe from this group, send email to castle-project-users+unsub...@googlegroups.com.

Krzysztof Kozmic

unread,
Jul 25, 2012, 5:32:28 PM7/25/12
to castle-pro...@googlegroups.com

Picks greediest it can satisfy. If you don't want IBar in Application A don't register it there
 

To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/rmlDXRvKOWsJ.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.

Jack Hughes

unread,
Jul 26, 2012, 3:11:24 AM7/26/12
to castle-pro...@googlegroups.com

Create  a null implementation of IBar that NOPs all methods. That way MyService doesn't have a dangling null reference that will likely cause problems in the code. In your new project register the null implementation with windsor, everywhere else register the concrete implementation.

--

You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/i7nkYXi8mp0J.

codin...@googlemail.com

unread,
Jul 26, 2012, 3:13:59 AM7/26/12
to castle-pro...@googlegroups.com, castle-pro...@googlegroups.com
Obviously the best way to do this is to actually refactor it out of your code...

Sent from my iPhone

Scott_M

unread,
Jul 26, 2012, 11:19:53 AM7/26/12
to castle-pro...@googlegroups.com, codin...@googlemail.com
Great ideas.  We have made fake implementations before that do nothing for all methods.  Curious if this could be done generically with FactoryPattern/RhinoMocks.


thanks



On Thursday, July 26, 2012 2:13:59 AM UTC-5, (unknown) wrote:
Obviously the best way to do this is to actually refactor it out of your code...

Sent from my iPhone

On 26 Jul 2012, at 08:11, Jack Hughes <JHu...@openxtra.co.uk> wrote:

Create  a null implementation of IBar that NOPs all methods. That way MyService doesn't have a dangling null reference that will likely cause problems in the code. In your new project register the null implementation with windsor, everywhere else register the concrete implementation.

 

From: castle-project-users@googlegroups.com [mailto:castle-project-users@googlegroups.com] On Behalf Of Scott_M
Sent: 25 July 2012 22:13
To: castle-project-users@googlegroups.com
Subject: Windsor / null dependency injection

 

Ran into a recent case where one of my new apps is using a component where a constructor based dependency of that component is not necessary (IBar) and would be painful to inject (lot of projects/related projects to import/build).  Here is a simplified example:

 

public MyService : IService

{

  private IFoo _foo;

  private IBar _bar;

 

  public MyService(IFoo foo, IBar bar)

  {

    _foo = foo;

    _bar = bar;

  }

}

 

So lets say that the IBar dependency is not needed for the current application but IFoo is.  Is there a good way to inject NULL for that dependency using windsor config / XML / Nullable<T>?    How is this done?

 

 

thanks

 

 

 

 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/i7nkYXi8mp0J.

To post to this group, send email to castle-project-users@googlegroups.com.
To unsubscribe from this group, send email to castle-project-users+unsub...@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-project-users@googlegroups.com.
To unsubscribe from this group, send email to castle-project-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages