Monday, November 21, 2011

TDD Adventure: Repository Newb

Forcing myself to use Test Driven Development is teaching me more than expected. Last year I learned about Entity Framework and the ease it brings to developers who want to focus on functionality. I created a project with it, and looking back on the code now it looks very much 'rookie'. I have matured quite a bit with the idea of context.

Recently with a new idea for another project I want to tackle TDD this time. Being experienced with Entity Framework I still felt like I was making hard coded database calls in the controllers. It wasn't really hard coded database procedures, but it was direct expressions against the entity object set. I feel really newb saying this, but I heard talk and would read about this 'repository' class. But didn't fully understand it and what it was for.

Just going through this tutorial twice, once as exampled and the second using my project. I could almost hear the light bulb click on. Repositories are the place for all the Linq code which could be SQL or even calls to stored procedures. And using a fakeObjectSet as the context really sets me up for the way I approach projects and allowing me to also start my newest project with TDD.

Tuesday, November 15, 2011

WebGrid Duh


Why reinvent the wheel for a Porsche when Microsoft already has a Ford in full production.
As I am doing another start up idea, I have been able to implement a lot of what I have learned previously with MVC. There is talk around a few 3rd party libraries out their to perform specific tasks designed to help speed up development time.
MvcContrib is one of these. Looking for a better and more strongly typed way of displaying table data I ran into MvcContrib being talked about quite a bit. I also noticed the comments where 2-3 years dated. The class I wanted to implement was the .Grid().
While searching for specifically MVC and Grid() the System.Web.Helpers Namespace also showed up. Now while using the MvcContrib class may have more functionality than the limited bare bones you get with a Microsoft Library, Microsoft is the 800 pound Gorilla. For learning a new class and shooting for functionality vs efficiency and problems I wish I had, the WebGridclass is treating me right.

TDD is great to talk about.


My latest upcoming project I am vowing to use Test Driven Design/Development. Reading books, going through tutorials, even watching videos of rock star TDD professionals is very inspiring. The problem I am having is re-gearing my approach to code. My standard thought process involves thinking of the root level object and how to build on top of it. Where TDD should be getting straight into the real problem and top down development.
I am trying to bring TDD into existing projects but keep walking away because I convince myself writing the solution would be quicker. Deep down, I know the more I practice I get with TDD, the better and more proficient I will become and eventually be coding faster than before with tested code. As I force myself though the books, the urge to utilize TDD in current projects grows stronger. Soon, I hope, I will be confident enough in my TDD skills to bust into it. Just as I taught myself MVC, I need to do the same for TDD.

MVC / MVC?


Using a shared hosting server has a huge advantage of cost. Especially if you don’t expect large traffic. It does provide some unique challenges when dealing with configuration though.
By default, web.config files inherit from each other. Awesome news if your building sub folders to contain micro-sites of your original. You immediately get access to all your connections, provides, and even custom classes you previously imported. But when creating sub folders within your root directory housing a different domain, things get a little messy.
After getting a base MVC site set up locally I deployed it to my shared hosting solution. The first thing I noticed was the url routing was off. Instead of the typical “domain.com/{controller}/{action}” I was seeing “domain.com/{subfolder}/{controller}/{action}” While I could manually remove the subfolder from the url and still get a successful request. This was not ideal and I don’t think the end user is interested in my subfolder naming convention.
I knew I was going to have to use some url rewriter to accomplish this. It didn’t take long for me to find out my hosting provider has dealt with this before and has pre-installed a URL Rewrite Module on all their windows shared hosting accounts with IIS 7. Lucky me. Easy fix, all I had to do was add some specific xml to my subfolder web.config system.webServer section.
<rewrite>
     <rules>
          <rule name="Remove Virtual Directory">
               <match url=".*" />
               <action type="Rewrite" url="{R:0}" />
          </rule>
     </rules>
</rewrite>
The second piece was not inheriting the connections and providers from the parent web.config. There are two ways of doing this, the easiest is putting
<clear />
after your section deceleration and before your names. This worked for most of it, what the previous doesn’t cover is any attributes you may have declared in the section deceleration, such as
<membership defaultProvider="customProvider">
The clear needs to go after this line, but doesn’t clear the attributes. So how do we remove this from our config? First, hopefully you have access to the root directory. Now edit the root web.config and place a section before your <system.web>, it should look like this.
<location path="." inheritInChildApplications="false">
        <system.web>
            ....
        </system.web>
    </location>
The only thing you really need to be careful here is this will wipe out your entire <system.web> for any sub folder you create. So make sure your children applications have valid web.config’s. Enjoy!

Switching from full Entity Framework to POCO


A big challenge I had with moving from a full Entity Framework solution to adopting a POCO set was managing state. Since my project was working well and I went to POCO I starting picking up issues with data not saving.
A huge issue was logging into the webapp. Obviously the most important function. The application tracks the logged in user via a session token saved to the database. After many frustrating tests and a half solution I thought was working, I finally found the ObjectStateEntry wasn’t keeping track of the object. The changes I was making to the object properties didn’t notify the OSE because I was using POCO and didn’t have it set up.
Searching google for some time I ran into this article http://blogs.msdn.com/b/adonet/archive/2009/06/10/poco-in-the-entity-framework-part-3-change-tracking-with-poco.aspx . Great article and I came up with an ugly, but working resolution of changing the state manually before saving. Is there a better way? Most likely, but this works and was speedy fix to get users logged into the system.

Knockout.js

Saw a demo of knockout.js located here http://knockoutjs.com/ and knew I had to use it in the next project. It turned out to be just as simple as described and can’t wait to use it again.

JoliCloud FTW?

I just installed http://www.jolicloud.com/ on a net book that now dual boots with Windows 7. I will say the 9 second boot and instant sleep resume is intoxicating. As long as this machine doesn’t get into power hungry applications (which the net book struggles with anyway) I think I will give it a run.

Excellent designs worth reviewing.

Are you looking for some inspiring patterns to use. And wanting to get a matching set created by the same artist. http://webtreats.mysitemyway.com/

Social Icons For Free

Just ran into an amazing discovery of social icons for free use. These are done by some professional artists. http://www.chethstudios.net/2009/09/free-social-media-network-icons.html