Skip to search.
altdotnet · Alt Dot.Net Discussions

Group Information

  • Members: 1347
  • Category: Software
  • Founded: Dec 27, 2007
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Messages

  Messages Help
Advanced
AMC: Changes to the way we think   Message List  
Reply Message #2299 of 24622 |

Village Wisdom:

-          We use constructor dependency injection (and sometimes properties) because it’s important to expose your dependencies…

-          … so that callers who aren’t using IoC (ahem  tests) can easily work with the test in isolation by using mocks and not having to involve an IoC container…

-          … and so it behooves us to keep the number of dependencies in a class down to a minimum to keep tests from getting too complicated…

-          … which also forces us to battle between too-many-dependencies and dependencies-that-do-too-much.

 

Basically, it seems like we make a lot of choices based on writing tests and the fact that our tests don’t use IoC (to keep things less complicated, we tell ourselves).

 

Now, I happen to buy into this, and it has worked well.  But… what if… we started allowing an IoC container to creep into our test playground, also (of course with something handy like AMC to populate the container with the more mundane stuff).

 

This opens up a whole new style of dependency management, IMHO.  What if we designed classes to _require_ some form of IoC container.  What if passing every dependency into the c’tor or properties isn’t the best way (every time)?

 

Bellware (and others) have talked about the various forms of dependencies (opaque, etc) and the one where a given class goes out and gets all its dependencies from the container itself (as opposed to having them injected) is ‘close’ and ‘not horrible’, but not perfect.

 

What if we require you to pass in a single ‘dependency resolver’ interface (which is basically the IoC container) into the c’tor from which the dependencies are gathered?

 

“What does this gain us?” you may ask.  Well, it prevents your tests from having to know about all the dependencies in a given class. It makes tests less brittle as you can add and remove dependencies without having to change as many tests – or changing the tests dramatically. It requires less setup and less expectation management just to get to some interesting part to test.  The AMC, container, and the class-under-test could work out themselves what needs to be mocked in order to satisfy the dependencies of the class and your test can interact only with the mocks you need to interact with in order to pass

the test.

 

I haven’t had a chance to put any of this to code (yet), but this occurred to me at the TDD CodingDojo today (more about that later) and I thought it might work.  I’ll try to whip up some examples of what I’m talking about and I may find that it’s not worth doing, but I’m willing to bet that this may address some of the friction of what to mock, how many, what expectations, etc.

 

Thoughts?

-Chad



Sun Feb 10, 2008 2:01 am

chad.myers94
Offline Offline
Send Email Send Email

Message #2299 of 24622 |
Expand Messages Author Sort by Date

Village Wisdom: - We use constructor dependency injection (and sometimes properties) because it's important to expose your dependencies... -...
Chad Myers
chad.myers94 Offline Send Email
Feb 10, 2008
2:01 am

I've done this in the past by creating 2 constructors: - one explicitly takes the dependencies - one no-arg constructor, which calls into the IoC container to...
Jimmy Bogard
jimmy.bogard Offline Send Email
Feb 10, 2008
2:14 am

I basically do the same thing as Jimmy. It came back to bite me once: I was using a class in a unit test with the no-arg constuctor, and the IoC container...
Tom Opgenorth
tom.opgenorth Offline Send Email
Feb 10, 2008
2:48 am

... I'm a little tired right now... but I read your post a few times and I'm not quite sure what the problem you're trying to solve is. Is it the number of...
Aaron Jensen
aaronthejensen Offline Send Email
Feb 10, 2008
3:40 am

- Number of args in c'tor - Number of things necessary to do in a SetUp() method or in the test itself just to satisfy dependencies which may...
Chad Myers
chad.myers94 Offline Send Email
Feb 10, 2008
3:55 am

... I see this as purely aesthetic, I don't think there's any tangible need to "fix" this. ... This is a problem sometimes, but like you said, it's often a...
Aaron Jensen
aaronthejensen Offline Send Email
Feb 10, 2008
4:09 am

... I've thought this was a problem as well, but I tend to agree with Aaron that it's largely aesthetic. However, I have sort of toyed with the idea of whether...
Shawn Hinsey
shawnhinsey Offline Send Email
Feb 10, 2008
4:16 am

Playing the devil's advocate, is this the symptom of: - An anemic domain model or - A coordinator with too many concerns This might happen when a domain...
Jimmy Bogard
jimmy.bogard Offline Send Email
Feb 10, 2008
4:22 am

This issue seems to frequently boil down to the decision about whether or not I'm injecting something explicitly through the constructor, or relying on a...
Shawn Hinsey
shawnhinsey Offline Send Email
Feb 10, 2008
4:30 am

Why not just use a set of static factory methods somewhere in your test library?...
Miika Makinen
khmerang Offline Send Email
Feb 10, 2008
5:14 am

Hi all - forgive my interjection. This is my first post to this list, though I've been lurking for a while. It would appear that you're systemically increasing...
Jef Newsom
jef_newsom Offline Send Email
Feb 10, 2008
4:25 am

Can you explain the benefit of passing the container to the service over using the AMC ? It looks to me like a lot more work. ... Can you explain the benefit...
Ayende Rahien
Ayende@... Send Email
Feb 10, 2008
8:01 am

It's unclear to me how passing in the container is substantially different from using it statically....
Shawn Hinsey
shawnhinsey Offline Send Email
Feb 10, 2008
8:09 am

Either way it is more work than just DI ... Either way it is more work than just DI On 2/10/08, Shawn Hinsey < smhinsey@... > wrote: ... <*> Your email...
Ayende Rahien
Ayende@... Send Email
Feb 10, 2008
8:19 am

I'd tend to agree. Maybe some code samples would help....
Shawn Hinsey
shawnhinsey Offline Send Email
Feb 10, 2008
8:20 am

I'm not sure I follow your question. 'passing the container to the service' and 'using AMC' are not mutually exclusive. AMC would straddle the container...
Chad Myers
chad.myers94 Offline Send Email
Feb 10, 2008
1:36 pm

Why on earth would I want the class to be aware of the container? Let us make take this example: public SmsSender(ILogger logger, IAlertGenerator generator,...
Ayende Rahien
Ayende@... Send Email
Feb 10, 2008
6:02 pm

(Ayende Rahien said...) ... Aware-of and directly-referencing are two different things. ... (NOTE: I wouldn't pass IKernel in there and thus strongly bind...
Chad Myers
chad.myers94 Offline Send Email
Feb 10, 2008
8:29 pm

What more do you want it to do? You are familiar with the things like [Transaction], I assume....
Ayende Rahien
Ayende@... Send Email
Feb 10, 2008
8:37 pm

... shouldn't be ... I agree with some/most of this but have you thought about just using a Service Locator style approach?...
Colin Jack
colin.jack Offline Send Email
Feb 11, 2008
8:04 pm

Service Locator taints the application with knowledge of the container, it makes dependencies less obvious, and in general means that I have to read code...
Ayende Rahien
Ayende@... Send Email
Feb 11, 2008
8:11 pm

It "taints" it with knowledge of the service locator, you may or may not be using IoC for your service locator. Personally I remember reading Fowlers article...
Colin Jack
colin.jack Offline Send Email
Feb 11, 2008
8:16 pm

How much time do you spend in the ctor? For myself, I almost always let R# deal with the ctor and never touch (or look) at it....
Ayende Rahien
Ayende@... Send Email
Feb 11, 2008
8:18 pm

I'm not saying that it takes me an age to create the constructor though. I am just saying that if you find a class has a lot of dependencies in its interface...
Colin Jack
colin.jack Offline Send Email
Feb 11, 2008
8:23 pm

It sounds like you're advocating partial adherence to the dependency inversion principle. If you're putting service locators inside a class, you're creating...
Jimmy Bogard
jimmy.bogard Offline Send Email
Feb 11, 2008
8:22 pm

... Fowler covers this better than I could so there is no point me regurgitating (http://www.martinfowler.com/articles/injection.html#ServiceLocatorVsD ...
Colin Jack
colin.jack Offline Send Email
Feb 11, 2008
8:28 pm

I think I'm misunderstanding your usage of a service locator. This is what's in my head, an opaque dependency w/ a service locator: public AccountService() { ...
Jimmy Bogard
jimmy.bogard Offline Send Email
Feb 11, 2008
8:38 pm

... is ... INotificationSender. But ... I.e., ... Nope thats pretty much it. In this case you are coupled to the Service Locator which is a simple class you...
Colin Jack
colin.jack Offline Send Email
Feb 11, 2008
8:48 pm

Maybe I missed it, but haven't anyone throw out the property setter approach for optional dependencies. This still allows a container to be used and removes...
Joey Beninghove
joeybeninghove Offline Send Email
Feb 11, 2008
8:57 pm
First  | < Prev  |  Last 
Advanced

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help