Live Demos

Monday, April 27, 2009

What page am I on?

So it's been 4 years since my last post and obviously I was right about it not being addicting...at least for me it's not. Anyhow, this is my first post as a developer learning from peers or at least hoping to. I keep seeing all these posts about automapper, dependency injection, TDD, MVC, being agile, etc, etc.

Apparently I'm just not seeing the big picture or something because I can get behind a couple of the things mentioned above, but not everything.

I see the benefit of dependency injection and I believe I'm doing it with StructureMap. I have a web forms app that seems to be working with it, but a lot of people have made it look a lot more complicated than the way I'm using it. I'll give an example in a bit.

I see the benefit of TDD and again, I believe I'm doing it with NUnit & ReSharper. I have only really gotten into the unit tests and not so much into regression or integration testing. I even have a few tests that use Rhino.Mocks, but I'm not 100% sure they're setup right. They pass and fail when I want them to so I guess they're right. Again...example in a bit.

I definitely see the benefit of agile development and I REALLY like the idea of user stories instead of requirements. My team is about to start our first true agile project from start to finish and it's definitely going to be interesting. There are a few people on my team that aren't crazy about change and tend to do things because that's the way they're done.

Now automapper is one thing I'm not sure I'll benefit from yet. I think Jimmy Bogard is probably the best developer I know at the moment and I'm sure he created something great, but I don't see the benefit for me and my team right now. I'm pretty confident that I will eventually slap myself in the face for posting this just because I'm sure it is an awesome tool that I will one day see as VERY cool, but right now...I don't. Maybe it's because I work for a school district and our projects just aren't big enough to warrant this type of implementation. I don't know, but I'm confident that the community will shine some light.

MVC is something else that I will be behind eventually, but right now I'm too busy learning the agile approach to things. I can definitely see the benefit of MVC especially when it comes to TDD, but it's pretty much exactly like going from classic ASP to ASP.NET. Hopefully Jeffrey, Jimmy, and Ben's book, ASP.NET MVC in Action sheds a lot of light on the topic for my team and me. I bought the early edition and I really liked what I read primarily because they're doing things the right way and not concerned with whether or not you understand. Seems like that last statement could be taken as a bad thing, but it's really not. I'm tired of buying books that go over the same crap, but using common practices instead of recommended methods. For example, if you pick up ANY ASP.NET book, they ALL have the same exact stuff and not one that I own shows you the correct way to do any of it. Like the using statement for disposing of objects. Jimmy showed me that and I own at least 8 .NET books. Maybe I was just buying the wrong books, but they all seem the same to me.

Okay...let's get to the examples. Hopefully Jimmy reads this at some point and he can point me in the direction of how he does his code samples on his blog. I'm sure there is some tool he uses...I don't see him doing anything by hand.

So dependency injection...I watched most of Rob Conery's videos on MVC and I think he did a great job of getting the right people on his videos to help out. Although I'm pretty sure my least favorite video is the one with his boss on the AJAX stuff. I think it's video 14..some where around there.

So this is how I handled my dependency injection using web forms:

First thing I did was created a static Bootstrapper class in the App_Code and it looks like this:

public static class Bootstrapper
{
public static void BootStrapStructureMap()
{
ObjectFactory.Initialize(x => x.AddRegistry(new SampleRegistry()));
}
}

The SampleRegistry is a class that inherits from StructureMap's Registry and it looks like this:

public class SampleRegistry : Registry
{
public SampleRegistry()
{
ForRequestedType<ISampleRepository>()
.TheDefaultIsConcreteType<SqlSampleRepository>();
}
}


So once I created my Registry class and static Bootstrapper, I throw it in the Application_Start of the global.asax, which looks like this:

void Application_Start(object sender, EventArgs e)
{
Bootstrapper.BootStrapStructureMap();
}


I think the syntax for StructureMap's GetInstance method is ugly and I didn't want to depend on them not changing that syntax, so I created a helper method that looks like this:

public class DI
{
public static ISampleRepository CreateSampleRepository()
{
return ObjectFactory.GetInstance<ISampleRepository>();
}
}


Now I can create my IndependentPage that has a protected field for my repository like this:

public class IndependentPage : Page
{
protected ISampleRepository samplerepository = DI.CreateSampleRepository();
}


And now I can use it in any page that inherits IndependentPage like so...

public partial class _Default : IndependentPage
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void GetAllOfSomething(object sender, EventArgs e)
{
gvList.DataSource = samplerepository.GetAll();
gvList.DataBind();
}
}


I'll show my example of my use of Rhino.Mocks in another post...this one has gotten WAY longer than I expected. So please give me some feedback. I'm really curious to see where I'm at in the community...newb...okay I guess...decent...good...great...who cares

3 comments:

  1. Like someone else said, it's really hard to use DI inside of a WebForms world. To get full testability you need either some sort of MVP architecture or something, but that's a big undertaking and it's overwhelming enough to learn wtf a IoC container is all about, so you're right, this is a good start.

    I'm not sure how much you use generics in your code, but the uglyness of StrcutreMaps GetInstance method largely stems from that. You don't have to worry about them changing that interface, it's sort of SM's workhorse, but when using DI in this type of pattern I usually wrap it in a "static gateway" sorta like what you've done.

    Have you started using any constructor-bound dependencies on any of your non-WebForms code? That was sort of the "sweet spot" for me when I was getting started with DI/IoC.

    You're doing fine, that's a lot of tooling and terminology to learn all at once, so don't feel bad if it's overwhelming. It's a lot of stuff all at once.

    ReplyDelete
  2. Yes I'm very familiar with generics and love them. However, when I started this new job, I found that none of the developers knew about generics and had a hard time following them. So to make the code more readable for them I started wrapping the "funny" looking stuff.

    This is probably a topic for another post, but after digging around in the codecampserver project, I found that there is a line to draw on readability and code re-usability. If you're a true software shop like Headsprings, then I'm sure it's fine because everything is documented and pretty and you get people wanting to come to you. At a school district, the easier you make it to read, the more maintainable it is even if there isn't as much re-usable code. The typical school district is usually a training ground for talented people.

    Yes I have used some constructor-bound dependencies and you're right that is definitely part of the sweetness of DI/IoC.

    Thanks for the comments and I'm glad to hear that I'm not on the wrong track. You're right it is a lot of stuff, but it's exciting...I love learning new stuff...and there seems to have been a burst of new stuff in our field...either that or I was too busy managing and not out there learning.

    ReplyDelete
  3. Yawn....:) I do think that it is a lot to learn and I plan on learning it at a pace so slow that you guys will eventually lap me and then I will be caught up.

    ReplyDelete