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

kick it on DotNetKicks.com

blog comments powered by Disqus
Related Posts Plugin for WordPress, Blogger...