Thursday, May 23, 2013

Using NANT with Visual Studio 2012

I have used Apache's Ant for quite a while. Now in there is NANT in the C# and .NET world. However Visual Studio only uses MSBUILD. Here's how you make it so that Visual Studio will use NANT instead:

First, open up the .csproj file and modify the DefaultTargets to "Nant" as follows:
<Project ToolsVersion="4.0" DefaultTargets="Nant" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Next, add the following at the bottom of the file:

<Target Name="Nant">
  <Exec Command="CALL nant.exe -D:buildtype=$(Configuration)" />
</Target>

Now, in your NANT's Default.build file, add this as the build target:


  <target name="build" description="Compiles the source code">

    <echo message="Build Type: ${buildtype}"/>
    <property name="debug" value="false"/>
    <if test="${buildtype == 'Debug'}">
      <property name="debug" value="true"/>
    </if>
    <echo message="Debug = ${debug}"/>

    <!-- Make sure the build directory is present -->
    <mkdir dir="${build.dir}" unless="${directory::exists('build')}" />

    <!-- Compile sources -->
    <csc target="library" output="${build.dir}\${project::get-name()}.dll" debug="${debug}">
      <sources>
        <include name="**/*.cs" />
      </sources>
      <references>
        <include name="${log4net.dir}\log4net.dll"/>
      </references>
    </csc>

    <!-- Copy log4net over. -->
    <copy file="${log4net.dir}\log4net.dll" tofile="${build.dir}\log4net.dll"/>

  </target>

This will use whatever build configuration used in Visual Studio. It will also set debug mode if you build it in Debug (to generate those .pdb files).


Wednesday, May 15, 2013

Log4net not found

I have a console application. I added log4net as a reference. I included it in my program with the using keyword. I get an error. 

The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)

Yes, I really did add it as a reference. Yes, I changed my Target Framework to ".NET Framework 4.5". What now?

Here's the solution. Include System.Web as a reference to your project. It then compiles successfully! Argh!

What is even more aggravating is that it still seems to compile after you remove System.Web! Double argh!!

This is very misleading. And why would a logger need any kind of dependencies is beyond me. 

This kind if thing would never happen in Java. If there was a dependency in one of the libraries it would just throw a runtime exception. Sigh. Oh well. 

Monday, May 13, 2013

Mutable-Immutable Data Transfer Objects in C#

The getters and setters in C# can make implementing POCOs easy. But sometimes when you are writing an API you need an object that can be modified internally but is read-only externally. In my mind, it is ideally better to have both objects strongly related to each other because if you remove one field or property in one, then it is removed for the other.

So how is this done?

You can't do this:
    public class ImmutableThing
    {
        int Id { get; }
        string Name { get; }
    }

    public class MutableThing : ImmutableThing 
    {
        int Id { get; set; }
        string Name { get; set; }
    }

Because the compiler will complain. "Automatically implemented properties must define both get and set accessors." Having the Immutable object extend the Mutable object yields the same result. 

To solve this, an Interface must be defined:

    public interface IThing
    {
        int Id { get; }
        string Name { get; }
    }

Now have the Immutable object extend from the interface:

    public class ImmutableThing : IThing
    {
        private int id = 0;
        private string name = null;
        public ImmutableThing(int id, string name)
        {
            this.id = id;
            this.name = name;
        }
        public int Id { get { return id; } }
        public string Name { get { return name; } }
    }

And the Mutable interface:
    public class MutableThing : IThing
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

You can have the fields in an abstract class so that you can have methods available for both immutable and mutable objects:
     public abstract class AbstractThing
    {
        protected int id = 0;
        protected string name = null;
    }

    public class ImmutableThing : AbstractThing, IThing

    {
        public ImmutableThing(int id, string name)
        {
            base.id = id;
            base.name = name;
        }
        public int Id { get { return id; } }
        public string Name { get { return name; } }
    }

    public class MutableThing : AbstractThing, IThing

    {
        public int Id 
        { 
            get { return id; }
            set { base.id = value; }
        }
        public string Name 
        { 
            get { return name; }
            set { base.name = value; }
        }
    }

I wish Visual Studio 2012 can tell me right away if it won't compile. I currently have to rebuild it each time.

Sunday, May 12, 2013

사다리 타기 (Sadari Tagi)

Today I learned about  Sadari Tagi (사다리 타기) a Korean way of drawing straws. It is a way to randomly assign N number of people to N number of tasks.

Process

First, a number of vertical lines are drawn, with numbers from 1 to N at the top and the tasks to be completed at the bottom (in random order). The numbers at the top correspond to each person. After that, a random number of staggered lines are drawn which interconnect each column.

For the picking process, a number is randomly picked. The idea is to follow down the line in the column. When an interconnecting line is first reached, cross over to the next column. After that, continue downward until another interconnecting line is reached. This is done until the task is reached at the end.

The following is an example. 3 people are supposed to complete 3 tasks. The mapping between person and task is random. The diagram is as follows (I made it horizontal instead of vertical):

1 ------------------------------------------ Task 1
              |           |       |
2 ------------------------------------------ Task 2
       |             |
3 ------------------------------------------ Task 3

So there are 3 interconnecting lines between 1 and 2, while there are only 2 interconnecting lines between 2 and 3. The number of interconnecting lines is irrelevant provided that they are staggered.

Here is an example, suppose 3 individuals (Jay, Sam and Bob) are required to perform three odious tasks. Which individual performs which task? If we use the Sadari Tagi (사다리 타기), we would construct the following diagram:

Jay ------------------------------------------ Task 1
              |           |       |
Sam ------------------------------------------ Task 2
       |             |
Bob ------------------------------------------ Task 3

To find out which task Jay is to do, start from Jay and move down until we hit the first intersection. Keep going down until we hit the second intersection. Keep going down until we reach Task #3.

Jay ------------------------------------------ Task 1
              |           |       |
Sam ------------------------------------------ Task 2
       |             |
Bob ------------------------------------------ Task 3

Using the same process, you will see that Sam is assigned to odious Task #2, and Bob is assigned to odious Task #1. (Apologies for the ASCII art!)

Question

My question is how do we know that there will always be a one-to-one mapping between number and task? So, could it be that, depending on the interconnected lines drawn, could it be that Sam and Bob be assigned to the same task? After constructing several such diagrams, I found out that the answer is no.

But how? The answer, in my mind, is quite simply. Suppose the entire diagram is broken into sections, where each section contains only one interconnecting line. For example, the diagram above is broken into 5 sections:

      #1  I  #2  I  #3  I #4 I  #5         
1 --------I------I------I----I-------------- Task 1
          I   |  I      I |  I    |        
2 --------I------I------I----I-------------- Task 2
       |  I      I   |  I    I               
3 --------I------I------I----I-------------- Task 3
          I      I      I    I               

For each section, if the process as described above is followed, there is always a one-to-one-mapping between the N numbers and each task. It does not matter how many sections there are, there always be a one-to-one mapping between N numbers and the task.

It would probably be easier just to draw straws (or randomly pick numbers from a hat) -- however the picking process of going through Sadari Tagi seems a lot more fun and exciting!

Friday, May 10, 2013

Function Call Can be Replaced by Set Literal

I got this warning in PyCharm today: Function call can be replaced by set literal

It was for the following code:
    hand[unique_id] = set([seat.player])

Basically it means that you don't have to say set([seat.player]) you could just do this:
    hand[unique_id] = {seat.player}

It took a while to process what the message was actually saying...