Latest posts by Joan Vilariño (see all)
- Object Factory: Creating objects with Expression Trees and Attributes - 17/02/2016
- New year, new address, new hosting! - 02/12/2015
dynamicoverloads - 21/10/2015
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://firstname.lastname@example.org/joanvilarino/messagehandlersample.git