Unity Container: What was wrong with this code?

Recently, I reviewed code from a candidate. He wrote a simple Web Application using WebApi. And he used Unity as IoC Container. And this is part of the code I asked him the question

SampeCodeWithUnity

My question to him: What is wrong with that code?

And his answer: Due to from best practice from this post:

http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver

And it is good to use suggestion implementation from Mike Wasson. However, I think he should think a little deeper with my question.

Anyway, I think it is interesting to explain my answer here.

  1. Exception handling: When the unity cannot resolve a component, and throw that exception, how to know: WHY? There is something wrong with your code. And you just assume it will be null and move on. Most of the time, the container should tell us why it cannot resolve a component. In the windsor container world, most of the time, it is lacking of component registered.
  2. About caller code: With that resolve strategy (return null on failed), the caller code have to check for null every time. Else you have a chance to get: NullReferenceException. Which means you introduce the unstable in your code, and the chance to debug it – in production code.

My suggestion: Lets it failed and throw exception. And you should have a error handler in the application scope.

Support serialization with Expando object

Recently I have a chance to work with project that need to store dynamic data, work with dynamic data. The data will be stored in RavenDB. After some considerations, I decided to use this implementation from Rick  Strahl. Everything works fine, except saving to database. It is store as empty object in RavenDB.

Just add these lines of code will solve the problem


public override IEnumerable<string> GetDynamicMemberNames()
{
return Properties.Keys;
}

The end result is nice

ExpandoRavenDB

WPF: Mystery bug: Hunt, fix, but still not understand why

I got a bug report from client for a WPF application. Please look at the picture below:

mysterybug

The expected dropdown values are: I , unique II, III, IV, V

But it displayed “II” instead of “III”. I then check the value from data source. The data was correct. I then brought up my Visual Studio and ran the app. No, issue found.

After some discussion, I then tried with Windows 7, and Windows 8. And after some tries with changing the value for “III” item. The conclusions are:

  1. It works fine with Windows Server 2008 R2.
  2. It does not work on Windows 7 and Windows 8
  3. Only happen if “III” stands alone. If you add space (“I II”), it works

After hunting down the code, I ended up at the template to display the combo box item ( I need to use custom template for my own purpose)

<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" Height="35" Padding="5"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>

Accidentally, I changed Padding from “5” to “3” ( I love number “3”). To my all surprise, it worked Smile. I then tried with number “4”. It did not.

It will work with value less than or equal 3.

But I still do not understand the reason Sad smile Just post it here if someone can explain to me or if someone has the same problem might find the answer.

Design and implement a template engine–part 2

In the Design and implement a template engine – part 1 I mentioned about a better solution: Reuse Razor Engine. In the post, I go in detail of how to implement it. But, first, we need to know some concepts:

Concepts

  1. Template Engine: Use to render a template into string
  2. Template Model Builder: Build the Model object which is used to fill in the data in template
  3. Template Model Expander: Expand the Model object with more properties
  4. And Template: the template

Implementation

The implementation assume that you already know about dynamic object and Razor Syntax (which is used in MVC – Views)

public interface ITemplateModelExpander
{
    /// <summary>
    /// Expand a dynamic object with more properties. The input object is sharing between expanders. Each expander will expand the properties that most make sense to them
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    dynamic Expand(dynamic obj);
    /// <summary>
    /// True if want to tell the <see cref="ITemplateModelBuilder"/> that, use the result of Expand method as the input for the next expander.
    /// The implementation should be very careful when setting true. Most of the time, it should be false
    /// </summary>
    bool UseResultForNextExpander { get; }
}

ITemplateModelExpander is the one that will be implemented in many classes. As its purpose, we want to expand the dynamic object to build up a real Model for the template.

Next we will see how we define a Template Model Builder.

/// <summary>
/// Builder is for building the object model using the inner expanders
/// </summary>
public interface ITemplateModelBuilder
{
    /// <summary>
    /// return the dynamic object that can be used in template
    /// </summary>
    /// <returns></returns>
    object Build();
    /// <summary>
    /// Append expander
    /// </summary>
    /// <param name="expander"></param>
    void AppendExpander(ITemplateModelExpander expander);
    void AppendExpanders(IEnumerable<ITemplateModelExpander> expanders);
    /// <summary>
    /// Clear all expanders. It is useful when you want to empty the object model and start adding new expander.
    /// Mostly you will use it if there is a single <see cref="ITemplateModelBuilder"/> in the system (Singleton lifestyle)
    /// </summary>
    void ClearExpanders();
}

It does 1 main thing: Build; which builds the Model object (of course, dynamic object) using expanders. That’s why you see 3 more methods related to Expander.

Then I make a simple implementation for the Model Builder:

public class StandardTemplateModelBuilder : ITemplateModelBuilder
{
    private readonly List<ITemplateModelExpander> _expanders = new List<ITemplateModelExpander>();

    public object Build()
    {
        var model = new ExpandoObject();
        foreach (var expander in _expanders)
        {
            var result = expander.Expand(model);
            if (expander.UseResultForNextExpander)
            {
                model = result;
            }
        }
        return model;
    }

    public void AppendExpander(ITemplateModelExpander expander)
    {
        _expanders.Add(expander);
    }

    public void AppendExpanders(IEnumerable<ITemplateModelExpander> expanders)
    {
        _expanders.AddRange(expanders);
    }

    public void ClearExpanders()
    {
        _expanders.Clear();
    }
}

Pretty simple right Smile

Let’s talk about Template, I have this simple interface:

/// <summary>
/// Define a template entry.
/// </summary>
public interface ITemplate
{
    /// <summary>
    /// The template, which tell <see cref="ITemplateEngine"/> how to render output
    /// </summary>
    string Template { get; set; }
    /// <summary>
    /// The builder which will build the model to populate data for the template
    /// </summary>
    ITemplateModelBuilder ModelBuilder { get; }
}

Define it as interface gives me power to change the template type. Even though, I just have Razor template for now:

public class RazorTemplate : ITemplate
{
    public RazorTemplate(ITemplateModelBuilder modelBuilder)
    {
        if(modelBuilder == null)
            throw new ArgumentNullException("modelBuilder");
        ModelBuilder = modelBuilder;
    }
    public string Template { get; set; }
    public ITemplateModelBuilder ModelBuilder { get; private set; }
}

You might wander why do I not inject template property directly in the constructor? Because, I want to resolve the ITemplate instance using an IoC container, such as Windsor and wire up with correct ITemplateModelBuilder which is StandardTemplateModelBuilder in this case. (I will show you how to use it later)

The last part, the Template Engine:

/// <summary>
/// Engine to render output base on template
/// </summary>
public interface ITemplateEngine
{
    string Render(ITemplate template);
}

Very simple. It just render an ITemplate instance.

The implementation is based on Razor Engine

/// <summary>
/// Simple implementation using Razor engine
/// </summary>
public class RazorBasedTemplateEngine : ITemplateEngine
{
    public string Render(ITemplate template)
    {
        return Razor.Parse(template.Template, template.ModelBuilder.Build());
    }
}

It asks the ModelBuilder from Template to build up the dynamic model object which is used to populate the template.

Usage

Have a look at this Unit Test:

[Test]
public void Test_simple_render_engine()
{
    // Arrange
    var engine = new RazorBasedTemplateEngine();
    var modelBuilder = new StandardTemplateModelBuilder();
    var template = new RazorTemplate(modelBuilder)
    {
        Template = @"Hello @Model.Name"
    };
    modelBuilder.AppendExpander(new SampleByNameExpander("Thai Anh Duc"));
    // Act
    var result = engine.Render(template);
    // Assert
    Console.WriteLine(result);
    Assert.AreEqual("Hello Thai Anh Duc", result);
}

I setup Engine, ModelBuilder, and Razor Template. These objects can be wire up automatically by IoC, see below

public class TestIoCWireUp
{
    private readonly ITemplateEngine _engine;
    private readonly ITemplate _template;

    public TestIoCWireUp(ITemplateEngine engine, ITemplate template)
    {
        if (engine == null)
            throw new ArgumentNullException("engine");
        if (template == null)
            throw new ArgumentNullException("template");
        _engine = engine;
        _template = template;
    }

    public string RenderNameTemplate(string template)
    {
        _template.Template = template;
        _template.ModelBuilder.AppendExpander(new SampleByNameExpander("Thai Anh Duc"));
        return _engine.Render(_template);
    }
}

And very simple implementation for SampleByNameExpander:

public class SampleByNameExpander : ITemplateModelExpander
{
    private readonly string _name;

    public SampleByNameExpander(string name)
    {
        _name = name;
    }
    public dynamic Expand(dynamic obj)
    {
        obj.Name = _name;
        return obj;
    }

    public bool UseResultForNextExpander { get {return false;} }
}

If you want to expand the Model with more properties that make sense to your application, you can add as many as possible. Just make sure you add the expander for the ModelBuilder of the Template you want. Or you can create a big giant Expander which will expose lot of properties at once. However, I will not encourage that. It is much more maintainable with simple small classes.

And, that’s it. Hope it help Smile

Can we code when age?

I am a developer living in Vietnam. I got many friends from the university, from companies I worked. When I was under 30; just a number to say that when I was young developer; my friends usually say:

You cannot code when you are age; assuming over 30 as they said. You have to do project manager, learn about economic. You become a project manager and let young developer do the coding task.

My first reaction: WHY?

Pretty much I got the same answer from them:

  • You are old, so you cannot think fast. Technology changes frequently and you cannot keep up with
  • Young developer is better than you
  • Being a project manager will give you more income with less work

At that time, I still did not understand why they thought that. They were a good programmer or at least did good job. And they thought of giving it up, and dreamed of another title, another job – believe me, some of them took economic class or MBA course (local MBA course).

Time flies. And now I am over 30 (31 precisely). And I am a developer. Actually, I am a technical manager, project manager. I have a team of more than 10 people with both developers and testers. But the main point is:

I do the coding. I still code and I love code. And I make a living with my coding job

Then I started to find out the reason: Why do they think that? What are the root causes?

Due to the expansion of the team, and of my new startup company, I made interview with some candidates. They are good programmers and might take many roles in their company. I asked them:

What books have you read since university? Which blog post do you follow? Do you know any famous programmer names?

To my surprise: NO. They have not read any technical books or books that can help them in coding life. They do not know Uncle Bob, Ayende, …

The answer is obvious:

They have not learned anything since university. They think they are good. They do the normal every day task and get bored. Their mind are slowing down.

In term of science, there is no evident for being stupid when you are age. Except you are over 70 or the like. Your brain needs food. Knowledge is the food. You do not give brain enough food.

Young developer and developer in general, I just want to tell you:

Foster your brain with knowledge. Learning everyday. Read more books. Do more code

Happy coding Smile