# Sample IoC container project

Tags: , , | Categories: .NET Posted by oleksii on 2/16/2011 4:22 PM | Comments (2)

Dependency injection is now heavily applied in software development. I find it useful to utilize a separate project for injections of types.

Typically, I have a library type project that is called a Container (really clever I know). This project has references to inversion of control container, such as Castle Windsor (see the list of others), plus a link to the core project. Thus it depends on the underlying IoC container and simple domain types. To see clearly what I can do with such container I have a base interface

public interface IContainer{	void Register(params IRegistration[] regParams);	T Resolve<T>();	Type Resolve(Type type);	void Dispose();}

This interface is implemented by the Container class. All the main types are registered by the Initialize method. If I need to register additional types, like controllers from ASP.NET MVC presentation layer, I can do it later by calling the Register method.

public class Container : IContainer, IDisposable{	private static bool isConfigured;	private static readonly object synchRoot = new object();	private IWindsorContainer container;	#region IContainer Members	public void Register(params IRegistration[] regParams)	{		Initialize();		container.Register(regParams);	}	public void Dispose()	{		if (container != null)		{			container.Dispose();			container = null;		}	}	public T Resolve<T>()	{		return container.Resolve<T>();	}	public Type Resolve(Type type)	{		return (Type) container.Resolve(type);	}	#endregion	public IWindsorContainer Initialize()	{		lock (synchRoot)		{			if (!isConfigured)			{				container = new WindsorContainer();								container.Register(                                    Component                                      .For<IRandomBitGenerator>()                                      .ImplementedBy<RandomBitGenerator>());				//...				isConfigured = true;			}		}		return container;	}}

This class is managed through the service that allows a single instance of the container.  Multithreading is implemented on the lower level by IoC container itself.

public class ContainerService{	/// <summary>	/// Returns lazily loaded Container	/// </summary>	public static IContainer Instance	{		get { return LazyLoadingContainerService.instance; }	}	#region Nested type: LazyLoadingContainerService	internal class LazyLoadingContainerService	{		// Explicit static constructor to tell                 //C# compiler not to mark type as beforefieldinit		internal static readonly Container instance;		static LazyLoadingContainerService()		{			instance = new Container();			instance.Initialize();		}	}	#endregion}

Such implementation can be used in the following way

public abstract class AbstractUtilsTest{	protected IRandomBitGenerator rndBitGenerator;		protected AbstractUtilsTest()	{		rndBitGenerator = ContainerService.Instance                                   .Resolve<IRandomBitGenerator>();        	}}

If you enjoyed this post, make sure you subscribe to my RSS feed!

# Easy Installation of Ubuntu

Tags: | Categories: Tweaks Posted by oleksii on 2/14/2011 11:46 AM | Comments (1)

A good friend of mine mentioned he started using Ubuntu a while ago. It seems like it is time to install it for myself. Following his recommendations I will use wubi which creates a virtual drive for Ubuntu and installs it. So I can have 2 OS at the same time. Sounds sweet to me!

If you enjoyed this post, make sure you subscribe to my RSS feed!

# Sample Chain of Responsibility pattern

Tags: , | Categories: .NET Posted by oleksii on 2/11/2011 11:27 PM | Comments (0)

See previous post.

The service that performs analysis will be hosted on a 3-rd party server and I must keep resource consumption low. One of the possible high level solutions for that is to run analysis on small blocks of data – windows, calculate outcome and dispose resources. In this way huge task can be split into multiple small iterations that are easy to manage.

Every iteration has same tasks to do: read data, look for repeatable patterns, save results and dispose. In fact, it can be joined into chain of responsibilities.

Abstract handler defines handling mechanism and allows to set successor

public abstract class AbstractHandler
{
protected AbstractHandler successor;

public void SetSuccessor(AbstractHandler successor)
{
this.successor = successor;
}

public abstract void HandleRequest(Request request);
}


Any derived class, would implement the actual responsibility

class AnalysisHandler : AbstractHandler
{
public override void HandleRequest(Request request)
{
{
//analyse data
}
else if (successor != null)
{
successor.HandleRequest(request);
}
}
}


The work starts by chaining responsibilities and triggering the first method

public static class ChainOfResponsibility
{
public static void Run()
{
AbstractHandler h2 = new AnalysisHandler();
AbstractHandler h3 = new ResultHandler();

h1.SetSuccessor(h2);
h2.SetSuccessor(h3);

var request = new Request();
request.State = RequestState.Pending;

while (request.State != RequestState.OperationCompleted)
{
h1.HandleRequest(request);
}
}
}


If you enjoyed this post, make sure you subscribe to my RSS feed!

# Why brute force never going to work?

Tags: , | Categories: Math Posted by oleksii on 2/11/2011 10:54 PM | Comments (0)

See previous post.

During development of a brute force analysis service, I calculated the amount of operations and memory needed for that. It is merely outstanding.

I need to go through time series of data with the alphabet of {0, 1, 2} and find out some patterns – repeatable occurrences. The sequence length itself usually stays within 1.000.000 border. Such numbers will tell me if a person was sitting, standing or walking during one week period with the resolution of 10 symbols a second.

I would want to look for the patterns, say starting with the length of 5 symbols. The number of all possible permutations is quite easy to calculate: 3 letters from the alphabet can appear up to 5 times at any of 5 places within the pattern, which gives 35 = 243 different possibilities.

So there could be 243 different permutations with repetitions.  If applied to physical activity 5 symbols would correspond to half a second of living monitoring. If I dare to look at the patterns of one hour length, I would need to consider 336.000 possible variations, having most patterns reoccur not at one hour rate, but rather at 12/24/24*7 hours. This brings the number of calculations to the point where nobody wants to deal with. Well, that’s brute force and this is why it is rubbish.

See next post.

If you enjoyed this post, make sure you subscribe to my RSS feed!

# How to crack data with brute force?

Tags: , | Categories: Math Posted by oleksii on 2/10/2011 10:23 AM | Comments (0)

I have an exciting idea for a while. In short, what if one needs to extract some meaningful patterns from a sequence that doesn’t seem to show any consequential information straightaway. For example, consider an entry sequence of: 000000000011111111000111100112222222001111111. What can you get from it? Numbers? :)

My real sequence is similar, but has got around 6 * 10^6 symbols of 1, 2 and 3. To complicate things further, I have many of such sequences, and the task is to find a universal trend and individual patterns. Having developed a methodology for such an analysis, I am in the process of writing a scientific paper. Proposed methodology takes a few minutes to compute 120 of such sequence and provide useful feedback.

But I was just wondering how blunt brute force is going to work. Perhaps it is easier to use it? I am thinking of writing a small service application that will perform such calculations and see if I can go through the walls. Is that possible? I assume not, but will quite entertaining.

See next post.

If you enjoyed this post, make sure you subscribe to my RSS feed!