Object Factory: Creating objects with Expression Trees and Attributes

Well, after a little rest from the blog – work and family take their time too – I’m back stronger than ever to publish some more new techniques for your .NET applications.

This time we will create an object factory using Linq Expression Trees, adding to that the use of CustomAttributes. This will allow us to totally decouple the object factory from the objects it creates. This method will allow you even having the factory in a separate layer of your application that nobody should touch to create their own objects. You just need to define the new class and add the custom attribute. The factory will scan all creatable objects at initializacion without having to modify any of its code (you wouldn’t even need to have the source files and put it in a nuGet package).

As always, you can download the whole sample project using Git or in a simple .ZIP file. Please note that if you want to run it using an older version of Visual Studio (2012, 2013) you will need to change the Console.WriteLine statements in Program.cs as I’m using the new syntax in C# 6 to create the output texts (ain’t it cute?).

Download ZIP: https://bitbucket.org/joanvilarino/messagehandlersample/get/62edf410d1ae.zip
Clone Git repository: https://joanvilarino@bitbucket.org/joanvilarino/messagehandlersample.git

Continue reading

New year, new address, new hosting!

Hi everyone! I hope you are readying your stuff for the incoming Christmas holidays!

With a new year comes a new address for my blog, which from now on will be “http://mycodingcorner.es” (not .com, please!), and I’ve moved to a new web hosting company. Let’s see how it plays for this 2016.

So, though I’ll keep the old ‘joanvilarino.info‘ domain pointing at this same blog, update your bookmarks (you had them, right?) since in a not-too-far future, it will point to a personal page or any other thing.

Merry Christmas and happy new year to everyone out there!

Cheers!

Using dynamic overloads

By the year 2010, Microsoft included in .NET a new static type called “dynamic“. This type, from my experience seeing other’s guys code, is rarely used, but I’ve found that in some scenarios can make our code much more readable and logic.

In this article, I’ll try to explain what makes dynamic so different from the rest of the .NET types, and I’ll give you a clear example of it’s power in a demo project using “dynamic overload“.

As always, you can clone the demo project from my Bitbucket repository, or download it via .ZIP file:

Continue reading

volatile in .NET, understanding such an “ethereal” keyword

I’ve found a nice article explaining the use of the volatile variable declaration in .NET when creating multi-threaded apps.

Surprisingly, a lot of people didn’t even know this keyword existed, so I pass along the article’s link in case anyone is interested.

http://firstcrazydeveloper.com/Blogs/BlogView.html/40/volatile-keyword-in-c?sthash.1pTifYFT.mjjo

Creating zillions of objects fast! Don’t “Activate”

Creating objects in .NET or any other language is an easy task, using it’s new() or equivalent keyword, but sometimes things are not that straightforward. There are many times when you don’t even know the type of object you will need to create.

Let’s take for example an entity mapper. It can need to map hundreds of thousands of records from a database, and normally you will use a “Generic Mapper” to create them.

Most people would use the .NET Activator class to do this:

public T Create<T>() {
    return Activator.CreateInstance(typeof(T)) as T;
}

While this is an easy and convenient approach, it’s painfully slow compared to a new() call.

Surfing the internet looking for some ideas, I found this article where you can see a pretty nifty idea to create zillions of objects much, much faster, using Linq expressions, rather than with Activator, that will allow you to do this:

ConstructorInfo ctor = typeof(Created).GetConstructors().First();
ObjectActivator<Created> createdActivator = GetActivator<Created>(ctor);
...
//create an instance:
Created instance = createdActivator (123, "Roger");

http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/

In this article they show you how to use Linq lambda expressions to create objects at a blazing fast speed, near the new() speed, giving you a good alternative for your mapper classes…

The results are really impressive, as the article shows, for a bulk of 1000000 (yeah, one million) objects:

Activator.CreateInstance: 8.74 sec
Linq Expressions: 0.104 sec

In some time, I’ll try to make a follow-up article for this one, showing you how to make your object factory using this method, but for the time being…

Enjoy it!

© 2017 My Coding Corner

Theme by Anders NorénUp ↑

%d bloggers like this:
Skip to toolbar