StructureMap Configuration and ASP.NET MVC Controllers
Recently I began playing with a long standing DI / IOC too called StructureMap. Naturally I ran into the what some might notice as a familiar error when using ASP.NET MVC controllers. “System.MissingMethodException: No parameterless constructor defined for this object.” Since I was injecting a Repository into my controllers, as the error points out, they no longer have a parameterless constructor which is required by the DefaultControllerFactory.
It was easy enough to determine and find the information required override the CreateController method to get StructureMap to create an instance of the controller. I then started by using the XML Configuration of StructureMap to tell it what to inject into my controllers. Like many others I tend to find Xml Configuration tedious and error prone. So I dove into the Fluent, DSL like configuration for StructureMap. I ran into a couple of issues with the fluent configuration (they were all from my lack of understanding StructureMap) so I wanted to pull together the different resources I used to get up and running.
I created a bootstrapper of sorts. It’s just a static class and method that is called from Application_Start(). Pretty simple once I was able to get everything together.
public static void BootstrapStructureMap()
{
ObjectFactory.Initialize(x =>
{
x.Scan(scanner =>
{
scanner.TheCallingAssembly();
scanner.With
scanner.WithDefaultConventions();
});
x.ForRequestedType
}
);
}
Originally I was registering each of my controllers. Something to the tune of
x.ForRequestedType
y.OfConcreteType
y.OfConcreteType
Tedious, eh?
Thanks to the following article from Jeremy Miller I was able to use the conventional standard for automatically registering all controller types that I might add in the future. I will paste the code here but it’s originally from the article.
Using the conventional standard of controller names “FooController”, “BarController”, etc one can determine a controller name.
public class ControllerConvention : TypeRules, ITypeScanner {
public void Process(Type type, PluginGraph graph) {
if (CanBeCast(typeof (IController), type)) {
string name = type.Name.Replace("Controller", "").ToLower();
graph.AddType(typeof(IController), type, name);
}
}
}
and in the bootstrapper you are instructing StructureMap to scan the entire assembly for controllers matching the convention.
x.Scan(scanner => {
scanner.TheCallingAssembly();
scanner.With
scanner.WithDefaultConventions();
});
And presto, all new Controllers should be auto-registered.
Big thanks to Jeremy and his work on StructureMap.
No commentsGetting CruiseControl.NET, SVN, and MSBuild up and running (*quickly)
Continuous Integration is something I have had my eye on for some time but have never really taken the time to dig into it. I became inspired when I read a recent thread on the alt.net yahoo group.
I am in somewhat of the same situation and figured what the heck, I’ll give it a go at home and I’ll be no worse for the wear. Hopefully this will help others — either getting someone excited about how *cough* simple it is, or overcoming a problem when attempting the setup.
I chose to go with a combination of CruiseControl.NET, SVN, and MSBuild. I use subversion as my SCM so this made sense, and MSBuild seemed as a good an option as any.
Installation
Download and install the Build Server and the CCTray
Install the subversion binaries -
If you already have the .NET Framework SDK Installed you should already have MSBuild, %windir%\Microsoft.NET\Framwork\v3.5\MSBuild.exe
Configuration
It seems relatively straight-forward, but I ran into multiple problems — both with configuration from guides that I found and with the samples. I finally settled on the following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <cruisecontrol xmlns:cb="urn:ccnet.config.builder"> <project name="MyProject"> <workingDirectory>c:\Builds\MyProject</workingDirectory> <artifactDirectory>c:\Builds\MyProject</artifactDirectory> <sourcecontrol type="svn"> <executable>C:\Program Files\Subversion\bin\svn.exe</executable> <trunkUrl>http://svn.repos/svn/MyProject/trunk</trunkUrl> <workingDirectory>c:\Builds\MyProject</workingDirectory> <username>foo</username> <password>dood</password> </sourcecontrol> <tasks> <msbuild> <executable>C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe</executable> <workingDirectory>c:\Builds\MyProject</workingDirectory> <projectFile>MyProject.sln</projectFile> <buildArgs>/v:quiet /noconlog /p:Configuration=Debug</buildArgs> <timeout>600</timeout> <logger>c:\Program Files\CruiseControl.NET\server\Rodemeyer.MsBuildToCCNet.dll</logger> </msbuild> </tasks> <publishers> <xmllogger> <logDir>C:\Builds\MyProject\Logs</logDir> </xmllogger> </publishers> <modificationDelaySeconds>10</modificationDelaySeconds> </project> </cruisecontrol> |
Two of the items that caught me here and were throwing me an error were the <workingDirectory /> and <artifactDirectory />> elements.
I also decided to use the following logger, since I was having issues with the default.
Needless to say this got me up and running. I wish it would have been easier, I ran into far too many small roadblocks. It may have been because I needed to upgrade TortoiseSVN and install Subversion, but to me this should be easier. Although now that I have done this it *should* be easier next time.
I know this isn’t probably the ideal setup. I’ll try and post some of the changes that I make, both with building different targets or MSBuild tasks that make this a little cleaner for deployment, included Automated Unit Tests.
Any feedback is appreciated, and I’ll try and answer any questions.
Good Luck!
1 commentTriathlon : Chapter 2
I am toying with the idea of competing in another Triathlon. Probably the Lifetime Fitness Triathlon again. It’s a good fitness goal and is something to keep me motivated throughout the dreary Minnesota winter and spring months.
I got started training this week. Again I have no official training regimen. I’ll probably take a few tips from one of the 20 week programs I have read through. This week started off with a 5 mile run. I feel it in the legs today so tonights bike might be interesting. No pain no gain!
I vow open water swim time this season.
P.S. Anyone want to donate a triathlon bike? I’ll be a walking advertisement
Continous Improvement
I took a page(s) from Rob Conery. I’ve been doing a lot of reading lately and following a particular development community for a good 4 - 6 months. I’ve even been to a local user group
One of the movements in this community includes continuous improvement. I’ve been doing plenty of reading and learning in theory, but it’s time to put the rubber to the road. The way it happens to work at this time will be an implementation of a pet project of mine that I’ve have been thinking about for some time.
By no means do I claim to provide “the way”. The exercise I will be working is specifically a learning experience much like Rob’s
I claim no authority on the subjects of DDD, TDD, NHibernate, or MVC. I plan to implement a relatively simple e-commerce engine using the aforementioned techniques. It will ultimately be a learning experience for me. It may be right, it may be wrong. I’d certainly like to see constructive feedback. I will blog along with my experiences and get my source code published online somewhere.
No commentsFantasy Draft Review
Had the annual Fantasy Football Draft last night. Overall I’m not really high on my team at this point. I finished 4th last year in a 10 team league. It’s a keeper league and my only keeper was Tony Romo. Perhaps you can see where this is going. I pick 7th. As it turns all top 10 RB were gone and the top 3 WR were also gone. That doesn’t leave much for pick 7.
Here’s how it turned out:
Keeper : QB Tony Romo
7 RB Larry Johnson
15 RB Thomas Jones (I think he’ll have a better year than last)
24 WR Plaxico Burress
47 RB Edgerrin James
54 WR Greg Jennings (Tough to reproduce what he had last year, but I think he’ll be fine)
67 TE Chris Cooley
74 WR Hines Ward
87 RB Ahman Green
94 WR Reggie Williams
107 QB Aaron Rodgers (solely because Romo’s bye is wk 10 and GB plays a terrible pass defense)
114 D Balitmore Ravens
127 TE Donald Lee (somewhat of a homer pick)
134 RB Kolby Smith (had to handcuff LJ)
147 K Jeff Reed
154 WR Ronald Curry
ALT.NET
I attended my first user group meeting tonight. It was the Minneapolis ALT.NET User Group. It was a good experience. I went in not expecting much as it was the inaugural group meeting in the Twin Cities. I was accompanied by my fellow geek, Jason. The ALT.NET group is something I’ve recently started following after a few posts that I ran across in my searches for professional development and to find different and potentially better ways of doing things.
Since finding the group, reading some blogs, and listening to the podcasts, I’ve become more of an advocate of finding the right tools and methods to gain efficiency and provide appropriate methods to provide appropriate client solutions. I like and respect the passion in the community, hope it continues on, and hope that I can appropriately contribute my successes and stories back.
At my current job we are currently “rewriting” our main application, converting it from a VFP windows fat client to a .NET web based solution. We decided to go with NHibernate vs other ORM’s or self-baked DAL’s. Since we are starting from scratch (at least from a system/application perspective), I am very interested in promoting some of the ideas from the ALT.NET community. Having been with the company for only a year and seeing some of the struggles with the current implementation, I feel that in our particular problem domain I think DDD is something that would be extremely benificial. The same goes for Continuous Integration, and unit tests…. (any unit tests). I’d even prefer a new method of source control. That leads me to one of my motivations for the ALT.NET community…how do you sell some of this new stuff to your development manager and your company president? Obviously I can initiate discussions and float my ideas, but how do I grab traction and have a concrete example of how this is better than it is today? What are other success stories out there?
I’m open for comments and I hope to pick some brains about some of this.
1 commentOlympic Fever
It’s been a while, but I’ve been busy so back off!
After a whirlwind July being and being out of town every darn weekend, I finally got a chance to settle in at home for the weekend, do a little work around the house, and catch some good Olympic games coverage.
I finally was able to find decent 5/4″ x 6″ x 12 PT deck boards. And I’ll be darned but the Endeavor can damn near swallow em. I only needed 7. A large number have been a trickier transport home, but it was all good. They are drying out as we speak and will get put on this week. Then it’s time to stain them and seal the new fence. I need a new garage door, service door, and garage door opener too… I could go on, but it’ll get depressing.
I’ve been obsessed with the Olympics. Let’s just say I got my fair share of coverage this weekend. I watched plenty of swimming, USA vs China in basketball and water polo. I’ve seen plenty of beach and indoor volleyball and as I mentioned both are absolutely outrageous. I can’t imagine being on the receiving end of some of those kills. A couple of highlights stick out in my head — Dalhausser/Rodgers losing to Latvia, the US Men’s Indoor Volleyball team pulling out a tough match against Venezuela, May-Treanor/Walsh continuing their sport’s dominance, Michael Phelps’ quest to become the most successful Olympic athlete of all time, and of course the rousing upset of the Frenchies in the Men’s 4×100 Freestyle. Awesome race! I can’t wait to see what else the games bring us. You can bet that I’ll be watching both the men’s and women’s triathlon as well. I don’t care if it’s streamed at 3AM.
Aaron Rodgers gets his first start tonight. Even though it’s only their first preseason game, it’ll be interesting to see how he comes out and how fans react to his success or failures on the field. I feel it might get ugly if he doesn’t perform.
No commentsYou can check that one off the list.
It’s been a while since my last post… no excuses, no apolgizes, that’s just the way it goes sometimes.
Tina and I recently celebrated our 1 year anniversary.
What will probably be the last Country Jam… at least for the foreseeable future is coming up. We’ll have to make the best of.
We’ve got 3 consecutive weekends of camping coming up. It’ll be tough. The 2.5 days of sun and such really wear a guy down. Once we get through it, I’ll definitely need some down time to recover.
Tina and I are now triathletes. We completed our first triathlon yesterday. It was a tough race (not that I thought it would be easy). When I first saw the swim course I was a bit overwhelmed. I can’t imagine swimming the olympic distance. We were able to watch a fair portion of the Pro race as our waves didn’t kick off until they were basically done. Some amazing athletes in that group. And I gotta say watching their transitions was a thing of beauty. Tina went off first. Once I got into the shoot I was feeling OK, no real nerves. I took a quick walk into the water and waded into a swim. I was both passed and did some passing. The open water was tougher than I had expected. I had to use the breast stroke several times. My rhythm was a bit off and I always felt I was dodging other swimmers. Half way to the first buoy, I thought to myself.. how in hell am I gonna make it. Turns out it wasn’t so bad. I swam much faster pace than I been training. I finished in just over 15 minutes. I was thinking more like 20 - 22 (which would have been very slow comparitively). The transition to the bike went well and I was off and pedaling. I knew I was pacing slow for the bike, but didn’t know how slow. I finally hit the halfway point and was 6 minutes behind. I finished 12 minutes off my pace. Parts of it were a tough ride and I felt like I could have moved a little faster in some areas. The final transition to the run took a few hundred meters to get my legs back under me and my running muscles in tune. I ran an OK 3 miles. I feel like could have run a little faster and cut off a few more minutes of my time. But I am happy I finished and met my goal of under 2 hours. Yesterday afternoon I was exhausted, but feel pretty good now.
All in all it was a great experience and I am glad I participated. Will I do another? Maybe, maybe not. For whatever reason the Olympic course somewhat fascinates me… but like an earlier post mentioned, I better get swimming!
Special thanks to Jason , Carl and Michelle, and Beth and Ryan for navigating the busy, full streets of the Lake Nokomis area to come down and cheer us on. And thanks to all of the provided words of encouragement from family and friends.
No commentsI’m no Tom Malchow
I finally got in the pool today for my first triathlon swimming session. I knew it wouldn’t be easy but it was quite different than I had imagined.
To preface, I feel like I am in pretty darn good shape. I have been doing a fair amount of running, biking, and biking then running to get the feel for transitioning. In fact last week, I biked 15mi and ran 3mi right afterward. You may be thinking 2/3 legs, not bad. That’s what I thought.
Well I got into the pool today and started swimming, I really have no guide or pace since this is my first training/workout swim in a very long time [read 6th grade]. I had to limit myself to 100m iterations and then a short rest. It was quite a workout, but I think the breathing really got me. It’s a different workout when you have to hold your breath and try to establish a breathing rythym that is determined fully by your ability to get your head out of the water long enough to take a breath and not suck in any water.
I’m glad I didn’t wait longer because I’ve got some work to do to get back into a swimming mode… I suppose I could always resort to the Elementary Backstroke.
2 comments