Click here to Skip to main content
Click here to Skip to main content
Go to top

Poor Man's IOC

, 10 Dec 2012
Rate this:
Please Sign up or sign in to vote.
No IOC library, no dependencies ..and I'm really really happy now.

Background

I maintain a white label chat site written in .NET MVC 4 for our company, and today I was refactoring a standalone DLL that contains tons of the RESX files which enable us to decline the site into many different languages.

The twist is that there are many places in the translations where I want to be able to replace entries with variables instead of the usual string translation facilities that RESX files provide.

Welcome to [[Brand]]
Visit [[Domain]]
{0} is not a valid value for {1}

To do this I use T4Resx, which enables me to transform all RESX files into a huge .cs file (then a DLL), and can invoke the translations in my .cshtml files with:

@Resources.User.Age

bind them to a ViewModel with:

[Display(Name = "Age", ResourceType = typeof(Resources.User.Age))]

or print out formatted messages:

@Resources.User.AgeInvalid(33, "ageTextBox")

public string GetAgeError(int age, string element) {
    return Resources.User.AgeInvalid(age, element);
}

Problem

The replacement function i was using resides in another DLL. At the moment I simply added a reference of that DLL to my RESX project and all was fine.

But while I was refactoring, I wanted to add a reference of my RESX project to the DLL that contains the replacement function, because I wanted to start using certain translations on ViewModels and the like.

However when i compiled the project, i got a nice error about circular references, arg !

...of course I should have seen that coming.

Solution

At first I started thinking, oh god ..now I'm going to have to add some IOC magic, which implies adding more dependencies to my projects. Nothing against IOC libraries in general, just the fact that i simply hate dependencies.

So I started running around looking at the usual IOC libraries (Ninject, Unity, Windsor, StructureMap ..) out there, since up to now I never really had the need to resort to any type of IOC in my projects.

While looking at those libraries, it still bothered me to think of having to add extra dependencies because of a stupidly simple replacement function, when it hit me that we have Func<> in .NET ! 

Func<> is nothing more than a cute shortcut to declaring delegates. The nice thing is they are shorter to write, and you can declare them inline, instead of doing this 

public delegate string ReplacementDelegate(string key);
public static ReplacementDelegate GetReplacement = key => key;   

They allow you to declare a function with a signature, and then later attach the actual function to it. So now we have a sort of dependency injection ..at least at the function level.

And after a bit of testing, I came up with the following scenario:

In my RESX project I declare:

public static Func<string, string> GetReplacement = key => key;

And still in the RESX project I now call my replacement function with:

Utilities.GetReplacement("somevalue");

Now as long I don't set up the bindings GetReplacement() will simply return the value that was passed to it. However I can now remove the dependency of the project that was providing me with the original replacement function from my RESX project. And can also add my RESX project to the project I originally wanted to add it to, without getting circular reference errors.

The replacements actually take place on the website, so that's where I will want to setup the bindings for my replacement function. And it couldn't be easier:

In my global.asax, inside Application_Start(), I simply declare the following:

Resources.Utilities.GetReplacement = c => DBController.Current.GetReplacement(c); 

The replacement function inside the RESX project is now bound to another replacement function that can access a database and return all my replacements.

No IOC library, no dependencies ..and I'm really really happy now. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Robert Hoffmann
Software Developer (Senior) Index Multimedia
France France
Internet & Technology related professional since 1994. Passionate about WEB 2.0 and Community/Social networking related type Websites, or anything that is tech related to bridging the gap between the User and his everyday Multimedia Experience.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 1 PinmemberLautas10-Dec-12 1:20 
QuestionRe: My vote of 1 PinmemberRobert Hoffmann10-Dec-12 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 10 Dec 2012
Article Copyright 2012 by Robert Hoffmann
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid