Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool

September 2008 - Posts

  • Some comments are a total waste of time

    I am all for comments in code (gasp I know) but some are just a total waste of time.

    Case in point.  I came across this today.

    /// <summary>
    /// Commit Transaction.
    /// </summary>
    public void CommitTransaction()
    {
    ........
    }

    WTF, I guess putting the Commit Transaction comment makes it more clear that the method "CommitTransaction' commits a transaction.

    As I said above, I am for comments, but I have a few simple rules I like to follow.

    1. Don't repeat yourself
      Like the example above don't waste my time telling me via a comment what the method/property name does when the name of the method/comment clearly states that information.
    2. Comment intent, not logic
      I know how to read if statements, and for loops.  Please do not tell me 'doing a look' or 'doing a check' because that is clear from the code.  However, please tell me why you are doing the loop if it is not obvious.  If there is some funky logic in the loop, that I need to know.
    3. Keep it short, and 'timeless'
      I do not want to read a novel.  I want to read 1 maybe 2 sentences and know enough to move on.
    4. If it is a 'Hack' it needs to be stated as such
      We all have added 'Hacks' to our code.  These are things that would not make sense to the casual observer but there is a reason why they are being done.  THESE MUST BE COMMENTED because if they are not someone else in the future is going to come along and repeat all your mistakes.

    Ok, rant over.

    Till next time,

    BTW, I removed that comment from my code base.

  • Clean build server with MSTest == FAIL

    It is no secret that I am not a fan of MSTest (read here for more rants on MSTests).  Today's rant is about how MSTest does not allow you to have a clean build server.

    Yea I know what you are thinking, here comes another long winded rant on MSTest.  Well this should not be too long winded, and hey, it is my blog and I can rant if I want to :).

    This week I was trying to setup our build server and I need it to run MSTest tests.  I know that MSTest does not run like NUnit, but thought for sure there was a way I could get my tests to run without having to install the full blown version of Visual Studio.  After a bit of searching and asking others I came to final conclusion that in fact you cannot get MSTest to run without Visual Studio. 

    This is an issue to me because when I setup my build server I like to keep it clean.  I do NOT want to install any 3rd party software.  The way I see it is like this.  If we are not going to install the software package on the end users computer, why should I need to install them on our build server, and the last time I checked we do not install Visual Studio on our end users computer.

    This is another issue to me because MSTest is the ONLY test framework (for .Net) that I know of that does not run with a single DLL placed into the bin (or any other output directory).  I just have to ask the genius' over in Redmond what the hell were they smoking when they decided to build MSTest.  It is pretty clear they had no prior knowledge of how to use the other tools such as NUnit, MBUnit or xUnit (I know, xUnit was not out yet).  I know this because of all the various testing frameworks MSTest is the one that does everything different.  You could argue they were on the cutting edge and were innovating, but I call BS on that.

    Anyway, I feel better now telling the world another reason why I am not a fan of MSTest.  Sadly I am stuck using it for now and at least in the short term as we have hundred of tests already written in MSTest.  But rest assured that I am on a crusade to get our team to switch from MSTest to NUnit (or any other logical framework).

    Till next time,

    [----- Remember to check out DimeCasts.Net -----]

  • StructureMap Automocker & FillDependencies Error(230)

    I have been giving the AutoMocker that is part of StructureMap 2.5 a go lately.  Today I was trying to setup my test and I get running into the following error:

    failed: StructureMap.StructureMapException: StructureMap Exception Code: 230
    Cannot call "FillDependencies" on type IGatheringPipeline. Check that the type is concrete and has no primitive arguments

    Everything looked right and I knew i could create my objects directly via the ObjectFactory method, but I just could not figure out what was wrong.  My 'bad' code is below.

    [TestMethod]
    public void IsSynchronizationNeeded_EnsureHitsRepository()
    {
        var pipelineMocker = new RhinoAutoMocker<IGatheringPipeline>();
    
        pipelineMocker.Get<iRepository>().Expect( x => x.DataNeedsSynchrnoization() ).Return( true ).Repeat.Once();
        
        var isSynchronizationNeeded = pipelineMocker.ClassUnderTest.IsSynchronizationNeeded();
    
        Assert.AreEqual( true, isSynchronizationNeeded );
    
    }
    

    After staring at the code and taking another look at its intent it dawned on me what the issue was.  I was creating the automocker with an interface and I wanted to call methods on that interface and actually have the logic execute.  In order to get this to work I simply needed to change my class in the auto mocker to the concrete.

    [TestMethod]
    public void IsSynchronizationNeeded_EnsureHitsRepository()
    {
        // Using the Concrete here
        var pipelineMocker = new RhinoAutoMocker<GatheringPipeline>();
    
        pipelineMocker.Get<iRepository>().Expect( x => x.DataNeedsSynchrnoization() ).Return( true ).Repeat.Once();
        
        var isSynchronizationNeeded = pipelineMocker.ClassUnderTest.IsSynchronizationNeeded();
    
        Assert.AreEqual( true, isSynchronizationNeeded );
    
    }
    

    After I changed my test to use the code above everything worked out well. 

    Hope this helps,

    Till next time,

     

    [----- Remember to check out DimeCasts.Net -----]

  • NAnt/MSbuild build error MSB4019 -- Missing Targets

    I was trying to setup our new build server today and received this error:

    error MSB4019: the imported project <Path to .net framework here>\Microsoft.CompactFramework.CSharp.targets was not found.

    At first I was a bit confused as to what was going one, how could you not find my targets file?  And of course this was working just fine on my laptop (always works on you machine), but not the build server.

    Before I dive into the solution for this, I would like to pause to review what was on the build box.

    Environment Overview:

    Build Server: Team City
    Build Script: NAnt
    Build Action: MSBuild
    Software Installed: .Net 2.0 SDK, .Net 3.5 SDK, .Net 3.5 Compact Framework SDK, need tools for build.  NO Visual Studio IDE's

    Onto the Solution:

    Ok, now that we know what the server setup was like, lets get onto the solution.

    For what ever reason my build server did not contain any .targets files for the compact framework.    These files are normally found in your c:\Windows\Microsoft.Net\Framework\v3.5 folder, but mine were missing.  The easiest way to fix this issue is to grab these files off of the server/laptop that has them and copy/paste them onto the server that does not.  This worked just fine for me.

    Note: My issue was with missing targets for the compact framework, but have heard reports of the same thing happening with other .targets files. 

    Hope this helps someone.

    Till next time,

  • Custom Formatters for YUI DataTable

    In a prior posting (here) I walked through how to add the YUI DataTable to your site.  Today I thought I would take that a step further and explain how to create custom formatters so your grid can look all fancy and stuff. 

    A custom formatter is a way to define a layout for the DataTable.  There are built in formatters, but you can also define your own.

    Here are the list of built in formatters (found in YUI docs):

    • "button" points to YAHOO.widget.DataTable.formatButton
    • "checkbox" points to YAHOO.widget.DataTable.formatCheckbox
    • "currency" points to YAHOO.widget.DataTable.formatCurrency
    • "date" points to YAHOO.widget.DataTable.formatDate
    • "dropdown" points to YAHOO.widget.DataTable.formatDropdown
    • "email" points to YAHOO.widget.DataTable.formatEmail
    • "link" points to YAHOO.widget.DataTable.formatLink
    • "number" points to YAHOO.widget.DataTable.formatNumber
    • "radio" points to YAHOO.widget.DataTable.formatRadio
    • "text" points to YAHOO.widget.DataTable.formatText
    • "textarea" points to YAHOO.widget.DataTable.formatTextarea
    • "textbox" points to YAHOO.widget.DataTable.formatTextbox

    Creating your own formatter is cake, with a few lines of code you are off to the races.

    IN order to create your own custom formatter you need to do 2 things.

    1. Create the actual formatter:
      // wanna create a custom formatter for the cell
      this.myCustomFormatter = function(elCell, oRecord, oColumn, oData) 
                  
      {
      
      	var name = oRecord.getData("Name");
              var episodeNumber = oRecord.getData("EpisodeNumber");
              elCell.innerHTML = "" + name + "";
                  
      };
      


      You will notice that the code above is actually creating a delegate callback that the DataTable will invoke.  It is inside this callback that all the 'magic' happens.
    2. Assign the formatter to the DataTable:
      // Add the custom formatter to the shortcuts 
      YAHOO.widget.DataTable.Formatter.myCustom = this.myCustomFormatter; 
      


      This code simply will assign your formatter to the Formatter collection that is part of the DataTable
    3. Tell the particular column on the DataTable which formatter to use:
      var myColumnDefs = [                    
      	{key:"EpisodeNumber", label:"#", width:30},
              {key:"Name", formatter:"myCustom"}, 
              {key:"EpisodeDate", label:"Release Date", width:100},
              {key:"Count", width:50}
      ];
      


      Pay close attention to this code, take notice where I use 'formatter:"myCustom".  The use of myCustom MUST match the name you assigned the formatter on the DataTable formatters collection.

    There you go, a quick overview on how to create and use a custom formatter for you your YUI DataTable.

    Till next time,

    [----- Remember to check out DimeCasts.Net -----]

  • Quote of the day

    I was listening to Episode #8 on DeepfriedBytes today and heard the greatest quote.

    In this episode Woody and Keith are speaking with Brad and Jim who both work over at Microsoft.  The quote goes a little like this:

    'We are a Microsoft shop here so we try to use Microsoft products'

    Sorry, I just found it great they they actually referred to them selves as a 'Microsoft shop'

    Till next time,

  • Upgrading a Smart Device Project to Compact Framework 3.5

    ***** Disclaimer: I am a total NOOBIE to working on Smart Device Projects *****

    Today I needed to upgrade on of our smart device projects from Compact Framework 2.0 to 3.5.  I figured that I could do this via the properties dialog, but could not (see image below).  But as you can see, the Target Framework option is no enabled.

     

    Being the guy I am (having now regard to totally screwing something up) I decided to take a look at the project file (.csproj) to see what I could learn from that.  Withen about 10 seconds of looking at the file I came across the following node.
    <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>

    I decided to change this to:
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>

    When I went back into my solution it asked to refresh based on the fact the files where changes.  After the refresh all was good.

    I know there HAS to be a better/cleaner/eaiser/safer way to do this, but I could not find it.  So, if there is please let me know.

    Hope this helps someone in the future,

    Till next time,

    [----- Remember to check out DimeCasts.Net -----]

  • Testing your IoC Bindings

    One thing I like to do when I am using a IoC (Inversion of Control) container is to create simple test that ensure that my objects have been wired up correctly and can be created via the IoC container. 

    I know this may seem like overkill, but I feel that by having my IoC bindings covered by tests I am covering all my bases.

    So, what does testing my bindings do for me

    • Allows me to know if my object is configured correctly
      By having these tests I will know immediately if I have somehow broken my IoC bindings glue (yes, glue is the technical term in this case). 

      It is not hard to phantom that when making changes to your IoC bindings logic (either via config files or via inline code) you can mistakenly break something.  Having these tests provides me immediate feedback if this were to happen.
    • Allows me to know if my objects dependencies are configured correctly
      Setting up the wiring for your main object is only half the battle.  When using an IoC container many time you are going to have that container do some sort of Constructor Injection of your dependencies. 

      By having tests that simply create my object with IoC I will find out immediately if one of my dependency objects is not wired up.  This scenario is pretty common and having this test can save you some headaches.

    Here is the test code I use for my IoC bindings tests:

    [Test]
    public void CanCreateViaDI()
    {
        var iocObject = ObjectFactory.GetInstance< YourObjectHere >();
        Assert.That( iocObject, Is.Not.Null );

    }

    Hope this helps someone.

    Till next time,

    [----- Remember to check out DimeCasts.Net -----]

  • Chicago Alt.Net Meeting on 9/10 -- IoC Containers

    Just a friendly reminder about the next Chicago Alt.Net meeting.

    Registration is open at the usual url: http://altnetchicago.eventbrite.com/

    Inversion of Control for the masses

    6:00 pm
    Pizza and networking time

    6:30 pm
    John Nuechterlein (a.k.a. jdn) puts Inversion of Control (IoC) containers under the spotlight. If you have been noticing the increasing buzz around this technology but never had a chance to see what it is and how it can be used, this is your chance to see:

    • An overview of what IoC is, using StructureMap as the example
    • Why one might want to use IoC
    • How one can implement IoC
    • When one might not use IoC

    7:45 pm
    You may want to stick around after the presentation portion of the meeting and take part in our monthly open discussion. The topic is never arranged in advance but it's common that it reflects the content of the presentation.

    Till next time,

  • Upgrading Dimecasts.Net from MVC Preview 4 to Preview 5

    Like many other nerds (sorry people, lets face we are nerds :) ), as soon as I heard that MVC Preview 5 had been released I wanted to run out and upgrade Dimecasts.net. 

    Let me first start off by saying this.  Anyone that decides to build a production application of any size on a Technology Preview must be willing to accept breaking changes between releases.  Please do not take this post as me 'bitching' or 'complaining'.  This post is just to help others that may be about to do the same thing.

    Here are the conversion issues I ran into:

    Issue #1:
    Since the new assemblies are now versioned again I needed to update the web.config to reflect 3.5.0.0

    Solution:
    Spend 3 seconds changing the web.config

    Issue #2:
    The decision was made by the MVC team to remove RenderUserControl and replace it with a more generic RenderPartial.  Now they both do the same thing in principal, but act in a slightly different way.  Because RenderPartial does not return any html (it writes it directly to the output stream) you need to change the syntax to call it.

    Solution:
    Replace all instances of RenderUserControl with RenderPartial as well as change the syntax.

    Old Syntax
    <% = Html.RenderUserControl(......) %>

    New Syntax
    <% Html.RenderPartial(......); %> // No = sign and added ;

    Issue #3:
    The HTML Helper ActionLink was changed to no long support the use of generics/lambda expressions.  This was done because you can not assume route information based on generics/lambdas and this could cause issues in the future

    Solution:
    Make the needed and painful change all over the code.

    Issue #4:
    Most of the HTML Helper's for text entry (text box, text area, etc) was changed to no longer accept html attributes such as width, height, cols, etc.  This was done with good intent and was the right move.

    Solution:
    Now in order to supply html attributes you need to use the overload that allows you to provide an anonymous object with the information.

    ie -- new { width = 25, height = 20 }

     

    All in all the upgrade was not all that painful.  I think it took me about 1 hour, nothing major.  Actually took me about as long to regression test the site as it did to make the actual changes.

    I hope this helps someone else that is making the change.

    Till next time,

    [----- Remember to check out DimeCasts.Net -----]

More Posts

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News