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

July 2008 - Posts

  • StructureMap Change to ObjectFactory.ResetDefaults()

    It appears that with the new release of StructureMap 2.4.9 (aka 2.5) the usage of ObjectFactory.ResetDefaults() has been replaced by ObjectFactory.Reset().

    Now why is this important?  I used .ResetDefaults() in my unit tests when I wanted to reset the plugin graph (is that the correct term?) because I had injected a stub into the graph. 

    I thought I would just mention this out there in case anyone else ran into this issue when they upgraded to StructureMap 2.5.

    Till next time,

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

  • Remote Pair Coding Idea

    Yesterday Kyle Baley mentioned (here) that he had given some thought about doing remote pairing with various people as a learning exercise.  I know he has mentioned this to me in the past and I always thought it was a great idea.  However, I was never in a position to join in a session cause I always had to be in the office.

    However, now that I am at home most days (3 weeks a month) I have the time and the want. 

    So here is what I am thinking.  I would be open to doing some pairing (or could be more than 2 of us) sessions from time to time.  We could code anything from my stuff (aka dimecasts) to work stuff (verbal NDA needed) or your stuff.

    If anyone is interested drop me a line.

    Till next time,

  • Is it evil to use reflection when testing?

    I have a scenario where I have a private method that needs to be tested and the only way I can get to that method is by calling another public method and exercising all of its logic. 

    This private method has a few different pathways and I would like to create tests for each of them.  I could do this by creating tests for the public method and just ensure that each of the tests exercise the various pathways of the private, but that just seems like too much work and effort.

    So, my question to you is this.  Is it evil to use reflection to test a private method?

    BTW, I know I could make the method internal and use the [InternalsVisibleTo] trick, but I do not want to make this method internal?

    Thoughts, opinions, feedback,

    Till next time,

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

  • My Initial Impressions with the new HandleErrorAttribute with MVC Preview 4

    As part of my process to upgrade DimeCasts.net to preview 4 I wanted to take advantage of the new HandleErrorAttribute.  Out of the box this looked like something that I would want to use and could use very easily.  However, I quickly realized that this attribute does not cut it for me.

    Why does it not work well for me, I will tell you.

    1. It is a sealed class which means that I cannot inherit and extend it.  Why would it be sealed?  Note to MVC team.  Avoid sealing classes for 'basic' features.
    2. By default it wants you to put your error page in the Views/Shared and name the page error.aspx.  Now I know there is a way to override this, but it was more painful that I had hoped.
      [MyHandleErrorAttribute(View = "~/Views/[folder]/[page].aspx")]
    3. I was not able it to route to a controller action, only to my view.  I tried to get it to route to my controller action (ok, maybe I was doing something wrong... it is possible) but did not have any luck.  Even when I could get it to display my view correctly it did not invoke the controller action for me.

    All this being said, I am NOT going to switch to using this new attribute as my hand rolled one works better in my opinion and is still getting the job done (right tool for the job right :) ).

    I must admit, I am a bit sad that this nice, common feature did not work out for me.

    If anyone else has had better luck with this please let me know.

    Till next time,

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

  • Upgrading DimeCasts.net to MVC Preview 4

    Today I decided to sit down and upgrade the Dimecasts.net code to run on MVC preview 4.  To be honest I did not know exactly what to expect, and I have to say, it was kinda painful but not too bad.

    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.

    You can get the list of changes and the readme from here.
    You can get other information about the MVC framework from codeplex or asp.net.

    Ok, on to the issues I ran into.

    Problem: Assemblies not being correctly referenced.  The first hurdle I ran into was that VS did not want to accept my new MVC assemblies.  For some reason even though I had added them as a reference they were not being dropped into the bin directory.
    Solution: All I needed to do was remove all the files from the bin in the solution and the Reference_Dependencies folders

     

    Problem: The MVC team renamed RenderView to View
    Solution: Global find and replace

     

    Problem: Had to update the web.config because the version on System.Web.Abstractions and System.Web.Routing had changes
    Solution: Find the few spots in the file and change the version from 3.5.0.0 to 0.0.0.0.

    I was told by Chris Sutton that this was done with intent because SP1 of .Net 3.5 was about be released and the team did not want there to be any version issues.

     

    Problem: All my viewdata references stopped working.  I have used the strongly typed ViewData feature a ton with MVC and all my references to it broke.
    Solution: Prior to preview 4 I could do something like this in my codebehind:
    ViewData.SomeProperty

    Now you need to do this:
    ViewData.Model.SomeProperty.

    The change for this makes since, but it was a pretty painful change.

     

    That about does it for my pain points in upgrading to preview 4.  I now need to spend some time giving the site a good run though to ensure I did not miss something.  Then it is off to gork all the new features and figure out how to use them :)

    Till next time,

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

  • Using your app.config (web.config) to store your StructureMap settings

    With the most recent drop of StrcutreMap (2.4.9 -- aka 2.5) you now have the ability to put your wiring information inside the app.config file.  Prior to this you needed to either use the StructureMap.config file or wire your dependencies up in code. So today I thought I would simply review how to use this new feature and show how simple and easy it is.

    ***** Disclaimer *****
    Yes, I know that some people think that using attributes for IoC wiring is evil, but I like it.  Also, because most of the stuff I work on is 'API' like, I tend to not have a single entry point so using the config files is the path with the least amount of friction.
    ***** End Disclaimer *****

    If you have ever used the StructureMap.config file you know that a sample layout would look something like this:

    <StructureMap>
    <Assembly Name="Assembly_NAME_HERE" />
    </StructureMap>

    Well, the good news is that adding your config to an app.config (or web.config) is almost 100% the same.  Below you will find a simple app.config that has my StructureMap information inside it

    <configuration>
    <configSections>
    <section name="StructureMap" type="StructureMap.Configuration.StructureMapConfigurationSection, StructureMap"/>
    </configSections>
    <StructureMap>
    <Assembly Name="Assembly_NAME_HERE" />
    <Assembly Name="Assembly_NAME_HERE" />
    </StructureMap>

    </configuration>

    Just so you know, in order to use the app.config as your source, you do need to add on line of code to your application.

    StructureMapConfiguration.PullConfigurationFromAppConfig = true;

    So there you go, moving from the StructureMap.config to an app.config is easy and should be fairly painless.

    Hope this helps.

    Till next time,

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

  • RhinoMocks new Arrange, Act, Assert does not seem to work under MSTest....odd

    -- UPDATE See text in red below --

    Ok, I am in NO way trying to say that this is either Rhino's issue or MSTest.  I am simply blogging this with the intent that someone can shed some insight as to what may be going on.  Although, since I know that the AAA logic of Rhino works with the EXACT same code under NUnit i have to believe it has something to do with the MSTest works.

    So here is my propblem:

    I have a test where i want to mock out and set an expectation on a repository.  When I tried the code via the AAA syntax my expectation would only return a null.  When I changed to use Record/Playback everything worked well.  Odd.

    Here is the syntax for AAA (non-working)

    var mockRepository = new MockRepository();
    var mockStatusCheckRepository = mockRepository.DynamicMock();
    
    mockStatusCheckRepository.Expect( x => x.GetCustomerAuditInformation( "", "" ) ).IgnoreArguments().Return( new CustomerAuditInformation { NeedsInitialDataCreated = true } ).Repeat.Once();
    var information = mockStatusCheckRepository.GetCustomerAuditInformation( "", "" );
    // Returns null
    

    I was able to get this working by NOT using the instance of MockRepoistory, instead using the Static Methods.  Belows Code works

    var mockStatusCheckRepository = MockRepository.GenerateMock();
    
    mockStatusCheckRepository.Expect( x => x.GetCustomerAuditInformation( "", "" ) ).IgnoreArguments().Return( new CustomerAuditInformation { NeedsInitialDataCreated = true } ).Repeat.Once();
    
    var information = mockStatusCheckRepository.GetCustomerAuditInformation( "", "" )
    

    So, this may just be a case of me not fully understanding the new AAA syntax and how to use it, but I know that using the instance stuff works under NUnit... odd


    Here is the syntax for the Record/Playback (working) - Can use the AAA syntax above as well

    var mockRepository = new MockRepository();
    var mockStatusCheckRepository = mockRepository.DynamicMock();
    
    using ( mockRepository.Record() )
    {
        mockStatusCheckRepository.Expect( x => x.GetCustomerAuditInformation( "", "" ) ).IgnoreArguments().Return( new CustomerAuditInformation { NeedsInitialDataCreated = true } ).Repeat.Once();
    
    }
    
    using ( mockRepository.Playback() )
    {
        var information = mockStatusCheckRepository.GetCustomerAuditInformation( "", "" );
    }
    

    Cany anyone shed some light on this?  Is there a way around this?

    Till next time,

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

  • MSTest, why I hate you.... You cause me too much friction

    Yesterday I spent 4+ hours trying to tackle a bug that should not have been a bug in the first place, and the culprit was MSTest.  Please keep in mind that I have never really used MSTest in any real form.  I am a NUnit guy through and through.

    Here is some background on what I was trying to do (keep in mind, I have done this numerous times in the past).  I was setting up an application that was going to use the StructureMap Ioc/DI (Version 2.4.9 -- aka 2.5) container.  Because I do not mind using the attributres and StructureMap.config files this was the way I was going to do all the wiring for my container. 

    During the testing (via MSTest) of this code, I immediatly ran into an issue with the .config file not being found at runtime.  Now this was odd to me as I could see that the .config file was being copied to the output directly like it should be.  I spent the next 20-30 minutes ensuring that all my code was correct and that the config file was indeed in the right place.

    It was not till I was stepping though the current directory was not my /bin folder, but rather \TestResults\whitt010_CM4NRG1 2008-07-22 16_18_25\out (if you have ever used MSTest you will understand the folder naming convetion).  I found this to be very, very odd.  I did a little digging and realized that for each test run MSTest will create a new folder to run all the tests in (why, I have now figgen clue).  But because my .config file was not a 'normal' file in the eye of MSTest it was not being copied.  Well, my good buddy Steven Andrews was nice enought to help me figure out how to copy the .config file to the correct output folder (post on that forthcoming).  Well, I thought this would solve my issue, but it did not.

    Now that I have the .config in the correct test folder (again, not in the bin like ALL the other testing frameworks..why does MS always have to be different) I thought I would be golden.  However, I was still not able to get this working.  After another hour or two of beating my head against the wall, and of bothering both Chad Myers and Jeremy Miller (father of StructureMap).  I finally realized what was going on.  When MSTest runs, it sets the current working directory to c:\<InstallBase>\Microsoft Visual Studio 9.0\Common7\IDE because that is where MSTest runs.  Because of this StructureMap was having issues finding the .config file (before you jump to SM as being the issue, think again.  I have done this before both with NUnit and MBUnit and both work fine).

    Alas after 4+ hours into my journey I threw in the towel.  I decided that using the StructureMap.config file method with MSTest was just too much friction.  So again with the help of Jeremy and Chad (do not know who was actually typing in the IM window as they were both there) I decided to solve the problem another way.

    Here is what I did to get everything working:

    1. Download and use StructureMap 2.4.9 (aka 2.5)
    2. No-longer used the StructureMap.config file.... bye bye my friend
    3. Setup StructureMap to read my config info from my app.config
      1. This involves moving my config info to the app.config
      2. Telling StructureMap how to read from the app.config (blog post on this soon)
    4. Swore over and over again at the pain that MSTest caused me.

    In intend to create a few more detaild posts on this over the next few days and will links to them here.

    Hope this helps someone.

    Till next time,

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

  • [ANN] New guest Author on Dimecasts.net - Justin Etheredge

    Today we have another guest author on Dimecasts.net.  In this series Justin will be teaching all of us how to use the Ninject IoC container.

    Justin's first episode can be found here, and keep a look out for more future episodes.

    Till next time,

  • [ANN] New guest author on DimeCasts.net

    This posting is a few days late, sorry Kirstin.  But last week DimeCasts got another great guest author to submit content.

    Kirstin Juhl (soon to be TFS Queen) submitted the first of a long line of TFS screencasts.  Her first episode can be found here, and keep a watch out for future episodes by her.

    Till next time,

  • Referencing 2.0 Web Services (asmx) in Visual Studio 2008

    Recently I needed to add a reference to a classic (.Net 2.0) web service inside a new project that was being created via Visual Studio 2008. 

    Now, I have not done a lot of work with WebServices in the past 6-9 months, but thought it would be cake.  Please keep in mind, the service I wanted to connect to was NOT a WCF Service, it was a standard ASMX service.

    Below is what I thought would work, but did not

    1. Attempt to Add A Service ReferenceAddAServiceReference
    2. Choose Services in Solution (my Project is in the same solution file).
      ServicesInSolution 
    3. View the generated service implementation.
      IncorrectGeneratedService
    4. WCF Style code implementation
      WCFStyleImplementation 

    What you will notice from step 4 is that the way you go about implementing this service is in the WCF style.  This is not what I was looking for.  I was wanting to reference this like all the other code in our projects.

    Below is what DID work

    1. Attempt to Add A Service ReferenceAddAServiceReference
    2. Need to click the Advanced button
      ClickAdvancedButton 
    3. Need to add the service as a Web Reference
      ClickAddWebReference 
    4. Finally need to search for and find your web service
       AddWebReference
    5. Lastly, if you have done everything correctly, you should see the following.
      CorrectlyGeneratedService

    Now I know that the preferred service type is now WCF, but come-on not everyone is using WCF just yet.  Adding a traditional web service reference is way too much friction.  Why is not possible to add a reference from the solution explorer?  Really can anyone answer me that.

    Till next time,

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

  • Should not be 'The Business', but should be 'Our Business'

    Last night I was listening to a recent HanselMinutes pod casts where he was talking with Tom & Mary Poppendiek.  For those of you who do not know who Tom & Mary are, they are the 2 behind a lot of the lean software movement (books here & here).

    There was a statement made in the episode by Tom that really stuck to me and actually caused me to have a 'ah-ha' moment.  The statement that Tom made was that we as developers tend to refer to business owners as 'The Business' in place of referring to them as 'Our Business'.

    The difference may seem subtle, but in actually it is quite substantial.  By referring to business as 'The Business' we are making the statement that we have no say or no influence in the decisions being made or the direction to be taken.  It also implies that we as developers (or IT personal in general) are in some way inferior to the people making the decisions. 

    When we refer to the business as 'Our Business' we are doing multiple things.  First we are saying that we have ownership and knowledge in the business and have the ability to not just code, but to make decisions.  We are also saying that we have more ownership in the product by including ourselves in the 'Business'

    The differences between the terms 'The Business' and 'Our Business' may seem small, but in reality they are huge.  Think about, reflect on it.  Let me know your thoughts.

    Till next time,

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

  • Foundations of Programming EBook - Great Read & ITS FREE

    If you have not hear, Karl Seguin has authored a great, short eBook which he is giving away free to the world.  This book is based of his blog series of the same title.

    Do yourself a favor and go download this book, it is pretty short and is a great read.  There are great nuggets of information for developers at every level. 

    You can browse to Karl's post here.

    Till next time,

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

  • MVC Html helper to render an image with a Routable Link

    Today I ran into a need where I wanted to create the html needed to render an image as a link and I wanted that clicking of that image to route to a controller action.  The off the shelf Html helper that comes with the MVC frame does not support this by default (there is a work around) so I decided to create my own.

    Here is the desired HTML output I am looking for:
    <a href="[RouteHere]" target="_blank"><img src="[ImagePath]" alt="[AltText]" style="border-style: none"/></a>

    Here is the work around:
    <a href="<%=Html.BuildUrlFromExpression<MyController>( x => x.MyRoute()  ) %>" target="_blank"><img src="[ImagePath]" alt="[AltText]" style="border-style: none"/></a>

    The workaround above simply uses the html helper for building url's from an expression and pops that into the HTML.  This is a workable solution, but I would rather have the Html helper generate the Html needed.

    Here is the code with the Html Helper:
    <%= Html.ImageLink<MyController>( x => x.MyRoute(), "[ImagePath]", "[Target]", "[AltText]" )%>

    I like the above a little better as it does not mix html code with a call to the html helper.  I feel this syntax is a little cleaner.

    Here is the Html Helper code:
    public static string ImageLink<T>( this HtmlHelper helper, Expression<Action<T>> linkUrlAction,
        string imageUrlPath, string linkTarget, string altText ) where T : Controller
    {           
        string linkUrl = helper.BuildUrlFromExpression( linkUrlAction );

        string outputUrl = string.Format( @"<a href='{0}' target='{1}'><img src='{2}' alt='{3}' style='border-style: none' /></a>", linkUrl, linkTarget, imageUrlPath, altText );
        return outputUrl;
    }

    Please note that this helper method is built for my needs, if you need/want to have the html rendered a little differently then you will have to make the necessary modifications.

    Hope this helps.

    Till next time,

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

  • [Ann] Reminder about Chicago Alt.Net meeting on July 9th

    Just wanted to send out another reminder about the upcoming Chicago Alt.Net meeting on July 9th.  Last week Serigo made the same announcement here.

    Hope to see everyone out there.

    Till next time,

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

More Posts Next page »

Our Sponsors

Red-Gate!