Chris Stewart's ASP.NET Blog

My experiences with ASP.NET

But I like my dependencies!

Recently I've been talking about dependency injection and ASP.NET MVC.  Each are interesting technologies and are "hot topics" in the ASP.NET world right now.  I had decided that my newest project would utilize these, if for nothing else but to learn them in more detail.

I had spent a few days working on the service level code for this project.  After building a few dozen interfaces and concrete classes, I felt like I simply had too much unnecessary code.  In my situation, which is equivalent to 95% of my past projects, the benefits of dependency injection did not outweigh the tediousness of implementing it.  I'm not going to reuse any of the code in my data, business, entity, or services layers.  So why spend so much effort and add so much complexity to decouple them?  I don't need extra implementations of my interfaces that would effectively be injected into my application.  So why implement a framework that is so heavily in favor of that?

The only benefit in my situation was in the unit testing arena.  In this case, I really had no plans to unit test this application.  I simply don't need it here.  Now, I'm sure that's awful and everything should be unit tested, but seriously, it's not always necessary.  I know test driven development is the popular thing this year but it's not always advantageous to follow that practice or even unit test to begin with.  This application is one of those exceptions.  I could explain why but someone will object, so I'll move on. :)

After a few days of sitting on the code I had written and debating my course of action, I decided to pull out the dependency injection and ASP.NET MVC and use plan old Web Forms with a typical n-tier architecture.  Everything is tightly-coupled and impossible to unit test from the presentation layer, and I couldn't be happier. :)  As a disclaimer, I was only using ASP.NET MVC because of how well dependency injection fits into that programming model.  See my previous blog entry for more.

I like dependency injection and I really like the outlook for ASP.NET MVC.  What I've learned in the last few weeks is that they have their scenarios where they're more appropriate, and that there are instances where they don't fit.  The overhead in creating the code to implement dependency injection is not trivial.  Take a hard look at your problem, application, and development environment before deciding which path to take.  I have a wonderful application I'd like to tackle that would fit ASP.NET MVC and dependency injection perfectly.  It's just not the one I'm working on right now.

kick it on DotNetKicks.com

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 15, 2008 9:15 AM

But I like my dependencies! - Chris Stewart's ASP.NET Blog said:

Pingback from  But I like my dependencies! - Chris Stewart's ASP.NET Blog

# February 15, 2008 9:20 AM

Shazam999 said:

What's the computer equivalent of Milton's Paradise Lost?  Because that's where you're going :)

You'll probably forever be cursed to use a computer with one of those Pentium chips with the FP error :)

# February 15, 2008 10:50 AM

Glenn said:

Middle Tier guys will always dream of a Middle Tier driven Front-end, and Front-end developers will always dream of an automated Tier code generator.  Usually, both come up a little short for production quality applications.  ...It doesn't mean I'll stop learning the concepts and new implementations.  Some day, they'll finally come together.

# February 15, 2008 1:52 PM

Shawn Oster said:

Whoa, feels like I just went through a time machine and I had to double-check the date of this post :)  TDD has been "hot" or mainstream for at least 5 years, probably more.  Is it really only now just seeping into the Microsoft culture?  Spooky.

Of course I agree that you don't always need it and there are plenty of times I don't do a damn bit of testing, it just depends on your projects.

As far as MVC and dependency injection I wouldn't marry those two so tightly in your head (or your reader's heads), they're not really related at all, they just happen to work together.  All my new projects are MVC-based and a lot of them don't have any injection OR tests (shhh), it's just that if the app is even vaguely DB-oriented I can get a more fully functional site up and running with MVC in less lines of code than ever I could with WebForms.

# February 16, 2008 8:09 PM

SitePoint Blogs » .NET on the ‘NET Feb 15-22: Silverlight 2 is Sweet said:

Pingback from  SitePoint Blogs » .NET on the ‘NET Feb 15-22: Silverlight 2 is Sweet

# February 22, 2008 8:48 PM

Brian Lowry said:

Chris,

As always, I enjoy reading your posts because you approach them from a different angle than most of the .Net blogs I read. As Shawn said, I wouldn't necessarily marry MVC and DI; at the same time, MVC is extremely pliable with regards to DI, so the DI pundits definitely will be pushing MVC over WebForms.

I'm currently working on a rather large WebForms app as MVC is not mature enough to really deliver it to a client as an application. I've managed to create DI using Webforms, and while there is some tight coupling to the Windsor Container, everything else is decoupled.

While I now have to create an extra interface for each service level, I like the trade off of 30 mins of extra work for future-proofing any fickle client decisions. For example:

The client has a "contact us" form that at this moment send an e-mail to sales@somecompany.com. In three months, they will tell me that they would like to additionally store that information in the database. What steps need to be taken to modify your existing code to allow for this functionality, and more importantly, can it be done cleanly so that methods are actually describing what's occurring?

With DI, I can create a new class that inherits from IContactService and write it to save the information to the DB. Then all I need to do, is go into the web.config and register it as an additional parameter to the ContactUs page. I'm done.

Plus, I can unit test if need be. ;)

# February 23, 2008 11:56 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)