Tuesday, June 23, 2009

Json.NET and Its Usage (1)

Json.NET is an open source project by James Newton-King. I have used this framework in my project in the past months and I am really enjoying its simplicity and power.

I mainly use this framework to convert objects or instances to an Json string or XML string, vice visa. The framework provides many classes but I only use a very small set of them. For my purpose, what I need its classes related serialization and de-serialization. Based on my practice and experience, I wrote a wrapper class with several methods for my usages. I like to wrapper Json.NET framework is based on the following reasons:

  • To hide Json.NET from its clients or users. As a result, users will not see Json.NET or need to add reference to Json.NET. All my projects need is to reference to my wrapper class.
  • To provide a clean and simplified set of methods for my usages. This relates to the previous reason. Users will not see rich and complicated Json.NET framework.

Basicaly, I use Json.NET for the following usages:
  • Configuration file for applications; and
  • Simplify my overrides of ToString() methods for classes.

Microsoft .Net provides framework for configuration. By default, you can have configuration in XML files like app.config or web.config. The problem is that the configuration file is very restrict in sense of its structure. You have to follow its XML structure to put your settings there. The most annoying thing is that when you save some settings, whatever comments you used as reference will be gone.

I like to write configuration in any way I would like. With Json.NET, this is possible. The configuration file can be either Json string or XML string in a file. Both of them can be easily converted or mapped to an instance of a class, called as configuration class. For example, I load my XML configuration file by using TextReader class as XML string. Then I convert it a Json string. From Json string, I use Json.NET to deserialize it to an instance of my confiugration class.

In case I need to make changes of some configuration settings, I use XMLDocument and Parser classes to make changes in XML string and leave other parts not touched. Finally I save the XML string to my configuration file.

For my purpose of overriding ToString() methods, before Json.NET I had to write hard codes to format property values to a string. This is really inconvenient. If I make changes of property names, I have to remember to change my ToString(); otherwise, my ToString() is unsynched. I do need to override ToString() when I need to debug my codes into a log file. With Json.NET, I can simply to serialize instance of this to a Json string. The serialization takes care of all the properties in a nice Json style. This is really very handy!

I'll continue to discuss my wrapper class in detail in my next blog.


Thursday, June 18, 2009

Google's aBowman

This is a very interesting gadget from Google. I was search in Google this evening. Then I was asked to sign in to Google. After I signed in, I quickly got to aBowman page.

Gadgets >> Hamster

I was using Firefox with this web page. I found this hamster. There is Get & Share section under the gadget, such as send email and blogger. I tried to send email and add to blogger, but I could not type in any words in the text area. Not sure why. Anyway, I copied the link to Safari and everything is working there. It is much faster when I am in Safari. However I could not use Vimperator in


Saturday, June 06, 2009

Dependency Injection and StructureMap (5)

In my preview articles, I have covered most commonly cases to describe dependency mappings. In this article, I'll conclude this series with chained registry strategy.

For many applications, there may be a lots of relationships between interfaces and implementations. You could define all those DMs in one Registry DSL library. However, I prefer to break them into several libraries and chain them together. The advantage of this strategy is to make the DM library easy to maintain and reusable.

The key in StructureMap Regisry DSL is to define a customized class based on Registry. Therefore, in each chained library, you should define your DMs in one or more Registry classes. The root library of the chain will call all the other libraries to get lists of Registry instances and add them to StructureMap framework.

For example, I have a console application, called as MyConsoleApp, which needs an implementation instance of IProcessControler. I'll create a SM library, SMForMyConsoleApp to define the DM, and it only defines the mapping relationship: how to create or map implementation class to IProcessController. The implementation class may need other implementations for other interfaces such as IDataReader, IDataProcessor and IDataWriter, but those mappings are from other chained libraries, where implementation mappings are defined.

The SMForForMyConsoleApp has two classes:

public static class SMForMyConsoleApp {
private static _registered = false;
public static void Initialize()
if (!_registered )
IEnumerable<Registry> listRegs = SMResgiry.GetRegs();
ObjectFactory.Initialize(x =>
// Get registry list from other Registry classes
// and add them to x
// ...

foreach (Registry reg in listRegs)

public static T GetInstance<T>() where T: class
T instance = default(T);
if (typeof(T) == typeof(IProcessController>))
instance = ObjectFactory.GetInstance() as T;
return instance;

internal class SMRegistry: Registry {
public SMRegistry() {
// define the DMs

public static IEnumerable<Registry> GetRegs()
List<Registry> list = new List<Registry>();
// Add registry from other chained registry
// ...

list.Add(new SMRegistry()); // finally add this instance
return list;

The class SMForMyConsoleApp has two static methods. Initialize() is used to add all Registry instances to StructureMap framework. This method should be called in MyConsoleApp to initialize mappings. The second method GetInsance() is used to get mapped instance, for IProcessController in this example, and then start the process.

Other chained SMxxx libraries have similar structure. In this way, the console application's mapping library does not need to know the complete mapping relations. It will let sub/chain libraries to do the mapping. You can see, this is a very clean strategy with great flexibilities.


Tuesday, June 02, 2009

Finishd iPhone classes on iTunes by Standford U

Today I finished all the 16 classes, as well as some lectures by guests on iTunes. Not sure if there are any more coming or not. Anyway, it is really good program. It does provides very clear and overall picture about iPhone development. It helps me a lot to understand Cocoa and Object-C.

With many years of .Net development experience, I have to relate some aspects to .Net C#. Cocoa and Object-C are really interesting framework and it looks very powerful. I really like their unique features.

For example, objective-C is a dynamic type language. You can add or define properties or methods dynamically and you can call on methods without worry their existence. Calling a method on nil will not cause any exception. Of course, it is controversial issue, as the instructor mentioned. No exception may bring some bugs. You expect some results but nothing happening. However, xCode and some tools are available to help to detect all those issues.

Another great feature is the delegates in Cocoa and Objective-C. They look like methods defined in an interface in .Net, but they can be optional. SEL is a great will to describe delegates and it is widely used to describe and detect their availabilities. Delegate is a way to delegate actions to objects to inject actions instead inheritance. This provides valuable flexibility to delegate class or object to take responsibilities.

Cocoa framework provides many functions, classes and controls which makes Mac and iPhone development much easy with great features. Cocoa and Object-C works well C codes. This provides much wide range of APIs and tools from open-source world, or you can build tools and libraries based on them.

I have been watching those class shows in the past weeks, one show a day during business days and two shows on weekends. Now I finished all of them. It is time to roll my sleeves up and start a new adventure in Mac & iPhone development.