views:

243

answers:

4

Hello everybody,

How often do you use IoC for controllers/DAL in real projects?

IoC allows to abstract application from concrete implementation with additional layer of interfaces that should be implemented. But how often concrete implementation changes? Should we really have to do job twice adding method to interface then the implementation if implementation hardly will ever be changed? I took part in about 10 asp.net projects and DAL (ORM-like and not) was never rewritten completely.

Watching lots of videos I clearly understand that IoC "is cool" and the really nice way to program, but does it really needed?

Added a bit later: Yes, IoC allows prepare better testing environment, but we also have nice way to test DAL without IoC. We wrap DAL calls to database into uncommited transactions without risk to make data unstable.

+2  A: 

IoC isn't a pattern only for writing modular programs; it also allows for easier testing, by being able to swap in mock objects that implement the same interface as the components they stand in for.

Plus, it actually makes code much easier to maintain down the road.

Pierreten
Andrew Florko
Then you're actually doing integration testing, since you're touching databases.
Pierreten
Why not? May be testing with mocking, then without mocking it too much?
Andrew Florko
Andrew Florko
@Andrew Florko, That is something that the IDE will do for you and you will not have to worry about that. You just change the interface and you have tools that change the implementations for you.
Mattias Jakobsson
@Andrew If you're changing interfaces then you're probably doing feature development not maintenance. Also, get ReSharper and learn the keyboard short cuts. It will pay for itself in a week in productivity gains. They have 30 day demos and you can email and ask for an additional 60 days. Check it out.
Ryan
+1  A: 

It's not IOC that allows you to abstract application from concrete implementation with additional layer of interfaces, this is how you should design your application in order to be more modular and reusable. Another important benefit is that once you've designed your application this way it will be much easier to test the different parts in isolation without depending on concrete database access for example.

Darin Dimitrov
> to be more modular and reusable.I suspect DAL library is reusable itself, because> isolation without depending on concrete database access for exampleSeems (for me) to be unpractical and hardly ever used feature.> benefit is that once you've designed your application this way it will be much easier to test the different parts Seems to be minor, because (if we talk about DAL) i can test DAL with uncomitted transaction calls.
Andrew Florko
@Andrew Florko, Then how do you test you BLL without using your DAL if you have a dependency on your DAL? Your question seem to be more about contract based programming then the use of IoC containers. Those are two different things.
Mattias Jakobsson
Yes, my BLL depends on DAL implementation. And I have never faced the problem with this dependence (because DAL implementation never changed completely). Unit tests cover DAL "singular functions" with uncomitted transaction calls as well as BLL "complex functions".
Andrew Florko
@Andrew Florko, Then you can't unit test your BLL, because it will test your DAL as well. Writing tests will then be a lot harder then if you could simply muck your DAL (and all other dependencies).
Mattias Jakobsson
Yes, I can't. But is it really a problem? Have you ever faced the situation, where such approach leads toward unpredictable results (not theoretically but in practice)? And each time I add method to contract interface, I have to create real-DAL implementation, and mock-DAL implementation as well. May be, world goes crazy on unit testing? Creating a DAL mock is not really pleasent job. It can contains hundreds of functions with in "memory database" with lists/dictionaries. Why not to test the real DAL functions on real data in place?
Andrew Florko
@Andrew Florko, Mocking your DAL doesn't mean that you set up a in-memory db. It means that you replace it completely and only return static fake data for the methods you know you will use. With a mocking framework this only means a few lines of code. When I test my DAL I usually test against a real db as well, mocking is about replacing your dependencies with static dummy implementations. A DAL usually doesn't have any dependencies.
Mattias Jakobsson
Ok, I agree we can abstract everything from everything but common question "is it really helpfull (or hellfull)?" :) Remember those ugly stone-age days when we didnot mock Dal and httpContext. May be we are all too proud that Godness-of-Abstractness smiles on us and such feeling overwhelms speed of development?
Andrew Florko
@Andrew Florko, See my comment on your question. A IoC can do so much for you. But it will take a while to find all those advantages, so at first it might be extra work. Today all my applications use a IoC container heavily and I have to say that I write a lot less code then I did before using a IoC and my code is a lot more readable and maintainable. I will never go back. A IoC can do so much more then just locate a implementation for a interface.
Mattias Jakobsson
Andrew Florko
@Andrew Florko, The thing that can make you write less code is that you can set up your conventions with a IoC container. I'm not good at explaining how, sorry about that, but you will find that out when you know your IoC better. In your interface you only have the signature, so if you write the signature in the interface VS will write the signature in your implementation, so no extra code there. With a mocking framework you will not have to write any test implementations for your interfaces. It's pretty much impossible to write unit tests without mocking, so I guess you haven't done that.
Mattias Jakobsson
Andrew Florko
@Andrew Florko, Your tests seem to be integration tests. That is the kind of tests that test that everything is coupled together right and that the different parts of your application can work together. Unit tests are the tests that only test a single method and none of its dependencies. Both kind of tests are important. A while back I started a series of posts (http://codejunkies.se/post/2010/03/04/My-project-setup-Part-1-Initialization.aspx) where I explain how I build applications (revolving a lot around my IoC container). unfortunately I have only had time to write one post so far.
Mattias Jakobsson
+1  A: 

There's much more about IoC except ability to change implementation:

  • testing
  • explicit dependencies - not hidden inside private DataContext
  • automatic instantiation - you declare in constructor that you need something, and you get it - with all deep nested dependencies resolved
  • separation of assemblies - take a look at S#arp Architecture to see how IoC allows to avoid referencing NHibernate and other specific assemblies, which otherwise you'll have to reference
  • management of lifetime - ability to specify per request / singleton / transitive lifetime of objects and change it in one place, instead of in dozens of controllers
  • ability to do dynamic stuff, like, getting correct data context in model binders, because with IoC you now have metadata about your dependencies; and this shows that maybe IoC does to your object dependencies what reflection does to C# programming - a lot of new possibilities, that you never even thought about

And so on, I'm sure I missed a lot of positive stuff. While the only "bad" thing that I can think about (and that you mentioned) is duplication of interface, which is non-issue with modern IDEs support for refactoring.

Well, if your data interfaces change every day, and you have hundreds of them - you may want to avoid IoC.

But, do you avoid good design practices just because it's harder to follow them? Do you copy and paste code instead of extracting a method/class, just because it takes more time and more code to do so? Do you place business logic in views just because it's harder to create view models and sync them with domain models? If yes, then you can avoid IoC, no problem.

queen3
A: 
Ryan
I should add that one of those projects I referenced has over 100 db tables!
Ryan