Sunday, October 17, 2010

C# inverse of control

I've used my simple inverse of control class, but I needed a singleton for a configuration class.
Here is a new version, just a little bit more complex, but capable of handling singletons.

    public enum RegisterScope
    {
        Instance = 1,
        Singleton
    }
    public static class Container
    {
        private static IDictionary<Type, Type> _types = null;
        private static IDictionary<Type, object> _singletonInstances = new Dictionary<Type, object>();
        public static void AutoRegister()
        {
            if (_types == null)
            {
                _types = new Dictionary<Type, Type>();
                Assembly assembly = Assembly.GetExecutingAssembly();
                foreach (Type contractType in assembly.GetTypes())
                {
                    if (contractType.IsInterface)
                    {
                        Type implementationType = assembly.GetType(contractType.FullName.Replace("Interfaces.I", "Implementation."));
                        if (implementationType != null)
                            _types[contractType] = implementationType;
                    }
                }
            }
        }
        public static void Register<TContract, TImplementation>(RegisterScope registerScope)
        {
            Register<TContract, TImplementation>();
            if (registerScope == RegisterScope.Singleton)
                _singletonInstances.Add(typeof(TImplementation), null);
        }
        public static void Register<TContract, TImplementation>()
        {
            if (_types == null) AutoRegister();
            _types[typeof(TContract)] = typeof(TImplementation);
        }
        public static T Resolve<T>(RegisterScope registerScope)
        {
            return (T)Resolve(typeof(T), registerScope);
        }
        public static T Resolve<T>()
        {
            return (T)Resolve(typeof(T));
        }
        public static object Resolve(Type contract, RegisterScope registerScope)
        {
            if (_types == null) AutoRegister();
            if (registerScope == RegisterScope.Singleton)
            {
                Type implementation = _types[contract];
                if (!_singletonInstances.Keys.Contains(implementation))
                    _singletonInstances.Add(implementation, null);
            }
            return Resolve(contract);
        }
        public static object Resolve(Type contract)
        {
            if (_types == null) AutoRegister();
            Type implementation = _types[contract];
            if (!_singletonInstances.Keys.Contains(implementation))
                return Instanciate(implementation);
            else
            {
                if (_singletonInstances[implementation] == null)
                    _singletonInstances[implementation] = Instanciate(implementation);
                return _singletonInstances[implementation];
            }
        }
        private static object Instanciate(Type type)
        {
            ConstructorInfo constructor = type.GetConstructors()[0];
            ParameterInfo[] constructorParameters = constructor.GetParameters();
            return constructorParameters.Length == 0
                ? Activator.CreateInstance(type)
                : constructor.Invoke(constructorParameters.Select(parameterInfo => Resolve(parameterInfo.ParameterType)).ToArray());
        }
    }

Same as the simple container, the only restriction is that you have to use a naming convention for default implementations.

  • Interface example: YourNamespace.Interfaces.ISomeObject
  • Implementation example: YourNamespace.Implementation.SomeObject

You may use the auto registration and then override some interface registration (for mockups, Web or services for instance).

The code to use afterwards... again, a single line.
ISomeObject someObject = Container.Resolve<ISomeObject>(RegisterScope.Singleton);
Or if you need a new instance, use the Resolve method without parameters.

ISomeObject someObject = Container.Resolve<ISomeObject>();

You may use this code in your project if it fits your needs. Fell free to add a comment if you have questions.

Simple C# inverse of control

I try to avoid third party tools whenever possible. Almost every time I used one, I had to deal with several updates, filtering functionality too complex for my needs, lack of support or maintenance over the time an so on.
I'm using a lot of Domain Driven Design lately and I really like it. Plus, I use a technique called inverse of control which allows dealing only with interfaces on Web applications. This is a great deal, because I'm able to use the implementation I need (Web, service or even mock-ups during development), plus it makes testing much easier.
There are a lot of third party components implementing inverse of control: Castle Windsor, StructureMap, Spring.NET, Autofac, and so on.
But since I don't like to use third party tools, I'm more inclined to use a simple version good enough for my project needs. There are several code examples on the net, like IoC container in 15 lines or IocContainer in 15 minutes.
Here is a simple version I've build, which allows the Interfaces to be automatically registered, son I don't have to write additional code or to use a configuration file. Everything is much simpler, so this is my thing.

    public static class SimpleContainer
    {
        private static IDictionary<Type, Type> _types = null;
        public static void AutoRegister()
        {
            if (_types == null)
            {
                _types = new Dictionary<Type, Type>();
                Assembly assembly = Assembly.GetExecutingAssembly();
                foreach (Type contractType in assembly.GetTypes())
                {
                    if (contractType.IsInterface)
                    {
                        Type implementationType = assembly.GetType(contractType.FullName.Replace("Interfaces.I", "Implementation."));
                        if (implementationType != null)
                            _types[contractType] = implementationType;
                    }
                }
            }
        }
        public static void Register<TContract, TImplementation>()
        {
            if (_types == null) AutoRegister();
            _types[typeof(TContract)] = typeof(TImplementation);
        }
        public static T Resolve<T>()
        {
            return (T)Resolve(typeof(T));
        }
        public static object Resolve(Type contract)
        {
            if (_types == null) AutoRegister();
            Type implementation = _types[contract];
            ConstructorInfo constructor = implementation.GetConstructors()[0];
            ParameterInfo[] constructorParameters = constructor.GetParameters();
            return constructorParameters.Length == 0
                ? Activator.CreateInstance(implementation)
                : constructor.Invoke(constructorParameters.Select(parameterInfo => Resolve(parameterInfo.ParameterType)).ToArray());
        }

Prety simple. The only restriction is that you have to use a naming convention for default implementations.
  • Interface example: YourNamespace.Interfaces.ISomeObject
  • Implementation example: YourNamespace.Implementation.SomeObject
Of course, you may use the auto registration and then override some interface registration (for mockups for instance).

The code to use after is really simple, a single line. No config, no extra registration lines, no nothing. Just use the resolve method and you're done.
ISomeObject someObject = Container.Resolve<ISomeObject>();


Feel free to use this piece of code if it fits your needs.

Monday, October 11, 2010

HTML5 - the new kid on the block

We all like HTML5, CSS3 and the new Web standards, but we may think that we are not ready to use them. Well, it's not true. The world goes mobile, 95% of mobile browsers are Webkit based so HTML5 is well supported on mobile space - I'm talking about smartphones, of course; everybody should and will have a smartphone soon enough. Plus, there are smart frameworks on the net allowing to develop HTML5+CSS3+JavaScript applications that can be published to native apps for iPhone, Adroid, Blackberry and other. Take a look at http://www.phonegap.com/ for instance.
For more information, please check my HTML5 presentation on Slideshare (http://www.slideshare.net/MarianBorca/html5-the-new-kid-on-the-block).
This is the future of the Web, starting now!

Wednesday, September 29, 2010

Unifying server and client programing using node.js

I recently discovered node.js, a cool approach to build scalable network programs. The server code is written in JavaScript, a language every Web developer must know. It is also very performant, handling more requests per second than other popular servers like nginx, thin or mongrel.
This is very cool, you don't have to write the code twice, once on the server side and again in the browser. It still has some problems, like memory utilisation and SSL support, but it is definitely something to keep an eye at in the future, maybe this is the way to bring all Web developers together to use a common, optimized language.

Tuesday, September 28, 2010

Analyzing big data

I've tried for some time now to find the best way to analyze big data. I've used different map-reduce systems, nosql databases and even a custom solution based on independent files for each user data. Not successfully until now. The custom solution is limited by the large number of files and there is no operating system Today that can efficiently handle those, even though the files are grouped in folders based on user IDs. Other systems like Cassandra or MongoDB seem to need a very large number of node on a cluster and are more appropriate to serve real-time results. What I need is an offline solution able to process huge information.
I was thrilled participate to a great presentation at Web 2.0 Expo in New York, where Kevin Weil from Twitter talked about the way Twitter handles big data. Twitter analysis is pretty much my use case, at a smaller scale.
The challenges are: collect the data, large-scale storage and analysis, rapid learning over big data.
Twitter has about 12 terabytes of data per day for a total of 4 petabytes a year.
They used syslog-ng at first, but it didn't scale. Facebook had the same problems so they developed and open sourced Scribe, which is a log collection framework. Twitter and Facebook are working together to make it better.
Twitter is using HDFS (Hadoop file system) to hold the data. HDFS is a distributed fault tolerant operating system, works very well over a cluster of commodity servers. It ensures automatic replication and transparently reads and writes across multiple machines. Each file has three extra copies in the cluster.
Yahoo is using also HDFS on a 4000 nodes cluster, able to sort one terabyte of random integers in about 62 seconds.
HDFS is now very easy to install, RPMs are available from Cloudera.
Here is a typical scenario of Hadoop map reduce utilization over HDFS at Twitter: to get the total number of tweets per day from key-value pairs (user ID and tweet info), map output key user ID with a value of 1, shuffle sort by user ID, reduce for each user using a sum, then output the user ID and the tweet count. Of course, doubling the machines practically doubles the speed of this query.
Currently Twitter counts about 160 million users and 20 billion tweets, this is a lot of data.
Because map reduce utilization is somewhat complex in native implementations like Java, they are using a high level language built on top of Hadoop, called Pig, which was developed by Yahoo and is open source.
Pig allows a considerable reduction of code, about 5% of Java for instance, this means of course 5% of development time. There is still a performance penalty, but it is not major, about 20% and going down with each release (first implementation was 110%).
All Twitter data is currently stored on a 100 nodes cluster and they plan on going to 3 or 4000 nodes. Pretty amazing, not a lot of servers for all tweets.
I will definitely try Hadoop and Pig and get back with some metrics.
Here is a list of queries Twitter are conducting on their data:

  • Requests per day
  • Average latency
  • Response distribution per hour
  • Twitter searches per day
  • Unique users
  • Links per day per domain
  • Geographic distribution
  • Usage difference for mobile users
  • What features get users hooked
  • What can we tell from a user's tweets
  • Language detection
  • Duplicates detection, anti-spam

It seems that Amazon is offering a cloud based hosting solution using Hadoop. Something to consider also.

Improving user experience for mobile applications - web standards to the rescue

I've been using for some time a Framework called PhoneGap for creating iPhone applications using HTML, CSS and JavaScript. It's pretty neat and I recently found that it can be complemented with another layer, bringing native look and feel to web pages, so in the end you can use PhoneGap to build HTML pages that interact with pretty much all iPhone functionality, such as vibration, location, notifications and so on, plus the mentioned layer of native layout. An example of such layer, open source and very easy to use, is jQTouch. Another Framework, more complex but more powerful, is Sencha Touch. It costs about $100, but it's pretty neat and very useful, so I suppose it worths the price.
The two Frameworks use HTML5 and CSS3.
Another important aspect to mention is that almost all smartphone browsers in use Today are Webkit based. This is the case with iPhone, all Android phones, Blackberry, Palm's WebOS (now HP)' even Nokia. The only notable exception are Windows phones. Webkit browsers represent around 95% of smartphone mobile space, so it makes perfect sense to develop primarily for these platforms.
Great job Sencha, for both the open source jQTouch and the commercial Sensa Touch.

Monday, September 27, 2010

HTML 5 for Desktop and Mobile

First day at Web 2.0 Expo in New York - Alex Russell from Google presented HTML5 for the Desktop and Mobile. Cool presentation, I think this is the way of seeing and doing things for the new generation of Web applications.


Today's browsers have a lot of problems related to HTML rendering: compatibility, slow requests, JavaScript execution flow. We have to find a way to optimise everything.
Most modern browsers are implementing a set of HTML5 tools for improved overall performance and fast rendering, ultimately improving user experience. Most problems are coming from IE (versions 6 to 8), in order to move forward we have to be develop for modern browsers (including IE9 and IE<=8 with Chrome frame plugin). If we continue to support old technologies the user experience will not be better.

HTML5 comes to the rescue, significantly reducing requests and scripting:
  • semantic tags (header, footer, nav section etc.)
  • new input tags (slider, search input, masked input)
  • canvas tag and canvas JavaScript API allowing all kind of animations and complex drawing

CSS3 helps a lot also:
  • rounded corners
  • animations
  • multiple backgrounds
  • gradients
  • shadows

A lot can be done using the latest version of JavaScript (EC5), including improved selectors using querySelectorAll.

More to come about this topic in the following days!


Sunday, September 26, 2010

Web 2.0 Expo in New York

I'm really excited to participate to this year Web 2.0 Expo, co-produced by O'Reilly Media and UBM TechWeb. Web 2.0 Expo showcases the latest business models, development paradigms and design strategies for the builders of the next-generation Web.

More info:

  • Web 2.0 Expo on Twitter
  • Join Web 2.0 Expo on Facebook
  • Read the Web 2.0 Expo Blog
  • Sign Up for the Newsletter
  • Watch conferences and keynotes live (registration required)