July 1027

log4net Gotcha with .Net 4.0 Applications

I’ve been using log4net in an application for a little while, it took some time to get it set up (maybe I’ll do a post about that in the future?) – but once it was set up I could call it from any form in my project and log whatever I wanted. For this particular project I decided I was going to create a new project and bring in my existing code, upgrade it to .Net 4.0 and fix any build errors. What confused me was that log4net wasn’t recognised in my project.

I checked my references and sure enough log4net showed up in there, my app.config file hadn’t changed but as soon as I tried a Using statement there were problems, trying to compile resulted in the following error: “The type or namespace ‘log4net’ could not be found (are you missing a using directive or an assembly reference?)”. Well quite clearly I wasn’t missing the using statement and I could see log4net in the references.

Well there are 2 versions of .Net 4.0, the Client profile and the full profile; the client profile only has some of the framework, it’s trying to reduce the footprint of the framework – however it doesn’t have system.web.dll and it seems that log4net requires that in order to work. So the solution was to change the Target Framework from “.NET Framework 4 Client Profile” to “.NET Framework 4”, allow Visual Studio to update the project (it will want to close it, which is fine), when it reopens the project log4net will not throw the error and I can get on with converting my project.

Spread/Promote this post

If you enjoyed this article, consider bookmarking or helping me promote it! Thanks.

  • Del.icio.us
  • Digg It!
  • Technorati
  • BlinkList
  • DZone It!
  • Furl
  • NewsVine
  • Reddit
  • StumbleUpon

Don't miss another post

Subscribe to SJM Dev's RSS Feed to stay updated with our latest articles!

Permalink | Comments (0)

July 1024

Entity Framework 4 - my first steps

I’m working on a pet project that I’ve wanted to migrate to using an ORM for a while. Tonight I took the plunge and decided I’d see how easy Entity Framework 4 (EF) makes things.

The Code-First Development with Entity Framework 4 post from Scott Gu made it look pretty easy and I figured I could probably swap out all my existing data access layer quite quickly, after all it’s promising to do most of the dirty data plumbing for me. Sure enough it’s really quick and easy to create a bunch of POCO classes but I’ve already hit 2 gotchas that I thought I’d share:

  1. Nullable datatypes for primary keys are not allowed. Typically I’ll create public int? ID{get; set;}, if the ID isn’t known then this is a new entity, if it is known then I've retrieved the object from the database and so this will be an update. However nullable datatypes are not accepted, so I’ll have to modify the way I’ve been doing these things.
  2. Object inheritance. I have a Contact class that defines some basic properties contacts have, then 2 derived classes (Customers and Suppliers). As all contacts should have an address I want to add an Address property to Contact but I’m getting an error.

    The navigation property 'Address.Contact' references an entity type 'Data.Models.Contact' that is not contained within an entity set. Reference a specific object set type 'Data.Models.Supplier,Data.Models.Customer' instead.

    So instead of having 1 place to deal with the Contacts Addresses relationship (and in this case it is only very simple, contacts are allowed 1 address only), I now need 2 places?

The code

This is what I've got for the Contact class

using System;

namespace Data.Models
{
    public abstract class Contact : BaseEntity
    {
        public virtual Address Address { get; set; }
        public DateTime DateAdded { get; set; }
        public String Email { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String Title { get; set; }
    }
}

and this is for the Address class:

using System;

namespace Data.Models
{
    public class Address : BaseEntity
    {
        public virtual Contact Contact { get; set; }
        public String County { get; set; }
        public DateTime DateAdded { get; set; }
        public String Phone { get; set; }
        public String Postcode { get; set; }
        public String Street { get; set; }
        public String Town { get; set; }
    }
}

finally this is the Supplier class:

using System;
using System.Collections.Generic;

namespace Data.Models
{
    public class Supplier : Contact
    {
        public virtual ICollection<Component> ComponentsSupplied { get; set; }
        public virtual ICollection<Product> ProductsSupplied { get; set; }
        public String SupplierName { get; set; }
    }
}

So nothing overly complex going on here – Suppliers implement Contact and provide a SupplierName property. I’m not sure I get why EF won’t let me define my classes this way.

That said this will make me significantly more productive once I get over this initial learning curve. I’ve been wanting to implement NHibernate in this project for a little while but messing about with xml files compared to the quick approach with EF means that I’ll probably use EF for all simple things for the moment – well until I get something I can experiment with using NH Open-mouthed smile

Spread/Promote this post

If you enjoyed this article, consider bookmarking or helping me promote it! Thanks.

  • Del.icio.us
  • Digg It!
  • Technorati
  • BlinkList
  • DZone It!
  • Furl
  • NewsVine
  • Reddit
  • StumbleUpon

Don't miss another post

Subscribe to SJM Dev's RSS Feed to stay updated with our latest articles!

Permalink | Comments (0)

July 1020

CodeRush Rename with F2 key

Rory Becker mentioned in a recent post CodeRush 101 (by Mehul Harry) that he uses F2 to invoke the Rename Refactoring. That was a forehead slapping moment for me… of course it makes perfect sense F2 should activate the Rename refactoring. I had a look in the Options and it does appear to be there, however it wasn’t active. Fortunately I found the answer on the DevExpress forums, I’ll quote it here, I’m sure no one will mind:

By default the Alternative Bindings are Disabled, select the Alternative Bindings "folder" and Enable it, then all the shortcuts that are in the Alternative Bindings should come to life ;)

Spread/Promote this post

If you enjoyed this article, consider bookmarking or helping me promote it! Thanks.

  • Del.icio.us
  • Digg It!
  • Technorati
  • BlinkList
  • DZone It!
  • Furl
  • NewsVine
  • Reddit
  • StumbleUpon

Don't miss another post

Subscribe to SJM Dev's RSS Feed to stay updated with our latest articles!

Permalink | Comments (0)

July 1015

TDD Kata – some more progress

Practicing the same thing over and over makes you better at doing it; this is true for anything, sports, martial arts, painting or even writing code.

That’s the aim behind the tdd kata that I’m doing every day (well every day at work Embarrassed) and I’m seeing slow, but sure, progress. I posted the other day about my tdd kata progress and said that I was getting only so far and that I couldn’t complete step 3. Well today I managed to finish that step and with a few minutes to spare. Now I’ve unblocked myself I need to keep practicing to ensure it sticks. It reminds me of when I started doing chin-ups; at first I could barely manage to haul myself up, then after a while I could do 9 reps and I was stuck at 9 reps for 2-3 weeks – I just couldn’t get into double figures. Then one day I managed 13, from 9 to 13 in a day. Once I’d broken the (mental) barrier it seemed that I’d unlocked things and managed to make progress once again. The important thing here is that I kept at it, sure I spent a while doing the same thing and not seeing any progress but it all helped.

You may be able to tell from this post that I’m actually feeling pretty pleased with myself and I really have nothing more to say – I just wanted to tell someone about my progress!

Spread/Promote this post

If you enjoyed this article, consider bookmarking or helping me promote it! Thanks.

  • Del.icio.us
  • Digg It!
  • Technorati
  • BlinkList
  • DZone It!
  • Furl
  • NewsVine
  • Reddit
  • StumbleUpon

Don't miss another post

Subscribe to SJM Dev's RSS Feed to stay updated with our latest articles!

Permalink | Comments (3)

July 1013

Code Rush Unit Test Runner not yet good enough

A big part of the TDD kata I’m doing is running the tests (kinda obvious huh?) and as I’m practicing this mouselessly it is essential that I can invoke my test runner with a keyboard shortcut. My current runner is TestDriven.Net, I’ve been using it for a while and I’m quite happy with it – 3.0 dropped in June so it is being actively developed.

Because I like trying new things, and the tdd kata is a way for me to learn new things, I thought I’d see how the CodeRush Unit Test Runner compared. So after I’d finished with the kata and I had a few tests in my suite I ran them all with TestDriven.Net and the Unit Test Runner.

With TestDriven.Net the tests below total execution time was 1.604 seconds and took 2.24 seconds, not bad, not very fast but not bad. The Unit Test Runner reported a duration of 1.85948165430718 (pretty precise). I’m not sure (and it’s not clear) whether that’s the total execution time, in which case it is quicker than TestDriven.Net or if it’s the total time it was running for, in which case it’s slower than TestDriven.Net. But there is a difference, so giving it the benefit of the doubt I thought I’d try to use the Unit Test Runner instead of TestDriven.Net and see how I got on.

TestDriven.Net can be invoked by pressing the menu key + U, pretty quick though a little awkward to reach the menu key. Alternatively you can bind to any keystroke you want through the Tools > Options > Keyboard settings. Great, now I wanted to find the shortcut I could use to invoke the Unit Test Runner… uhm, nothing is mentioned in the menus and looking in the Keyboard settings I couldn’t figure out what I needed to bind, nothing jumped out at me as an option. So I Googled it for a bit and then went to the DevExpress support system to see what they said. Unfortunately the only post I found, that seemed relevant was Unit Test Runner Window Shortcut which was posted in June and confirmed that there is no shortcut available at the moment – though there are plans to create a shortcut in a future release.

To be honest this is pretty much a deal breaker for me. I’m really happy with just using the keyboard and lifting my hands off just to invoke a test, something I’m supposed to do every minute or so, is really slowing me down. I have to grab the mouse, work out where the cursor is, move the cursor over an icon then click the icon and make a selection (do I want to run the tests or debug the tests) – I know that sounds like a pathetic complaint, but really doing this as frequently as I need to in order to practice TDD is quite painful. Please DevExpress fix this in the very next release!

Update

Didn't think I'd need to update this post this quickly; it's been less than 3 hours and DevExpress have responded to my plea! In 10.1.6 (i.e. the next release) they've added the "ShowUnitTestRunner" action, so you're able to bind any shortcut for opening the Test Runner, which is good news, though I would need to request a daily build or wait for the official release to take advantage.

However Rory Becker has come up trumps with 2 options that you can use today! The first of which is to simply - doh! why didn't I think of that, and the other is a plug-in that shows the test runner

I bound Ctrl+R, Ctrl+T (run tests) to RunSolutionTests and it works great. I will also bind something for RunTestsAtCursor, which are the two I use the most frequently.

My test suite

using System;
using MbUnit.Framework;
using tddKata.Core;

namespace tddKata.Tests
{
    [TestFixture]
    public class StringCalculatorTests
    {
        [Test]
        public void Add_EmptyString_ReturnsZero()
        {
            int actual = StringCalculator.Add("");

            Assert.AreEqual(0, actual);
        }

        [Test]
        [Row("1", 1)]
        [Row("2", 2)]
        public void Add_SingleNumber_ReturnsNumber(string numbers, int expected)
        {
            int actual = StringCalculator.Add(numbers);

            Assert.AreEqual(expected, actual);
        }

        [Test]
        [Row("1,2", 3)]
        [Row("2,3", 5)]
        public void Add_TwoNumbers_ReturnsSumOfNumbers(string numbers, int expected)
        {
            int actual = StringCalculator.Add(numbers);

            Assert.AreEqual(expected, actual);
        }

        [Test]
        [Row("1,2,3", 6)]
        [Row("2,3,4", 9)]
        public void Add_MultipleNumbers_ReturnsSumOfAllNumbers(string numbers, int expected)
        {
            int actual = StringCalculator.Add(numbers);

            Assert.AreEqual(expected, actual);
        }

        [Test]
        [Row("1\n2,3", 6)]
        [Row("1\n2\n3", 6)]
        [Row("2\n3,4", 9)]
        public void Add_MultipleNumbersNewlineDelimiter_ReturnsSumOfAllNumbers(string numbers, int expected)
        {
            int actual = StringCalculator.Add(numbers);

            Assert.AreEqual(expected, actual);
        }
    }
}

Spread/Promote this post

If you enjoyed this article, consider bookmarking or helping me promote it! Thanks.

  • Del.icio.us
  • Digg It!
  • Technorati
  • BlinkList
  • DZone It!
  • Furl
  • NewsVine
  • Reddit
  • StumbleUpon

Don't miss another post

Subscribe to SJM Dev's RSS Feed to stay updated with our latest articles!

Permalink | Comments (3)