CategoryArticles

Object Factory: Creating objects with Expression Trees and Attributes

Follow me

Joan Vilariño

Senior .NET Developer at Ohpen
Developer for more than 25 years and IT, gadget, mobile and electronics enthusiast working and publishing from Barcelona.
Follow me

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

Using dynamic overloads

Follow me

Joan Vilariño

Senior .NET Developer at Ohpen
Developer for more than 25 years and IT, gadget, mobile and electronics enthusiast working and publishing from Barcelona.
Follow me

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

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

Follow me

Joan Vilariño

Senior .NET Developer at Ohpen
Developer for more than 25 years and IT, gadget, mobile and electronics enthusiast working and publishing from Barcelona.
Follow me

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!

!Any() vs All()

Follow me

Joan Vilariño

Senior .NET Developer at Ohpen
Developer for more than 25 years and IT, gadget, mobile and electronics enthusiast working and publishing from Barcelona.
Follow me

The dilema

Sometimes you need to do some action to check if an element is not present in a collection, for example, check if a customer is in the Customers collection before trying to add it.

With .NET there are several ways to do so: For example if the collection is a List you can use Exists() or if you want to check the presence of the object itself, you can use Contains(). Using Linq, we can also do it with Any and All

In today’s article, we’re gonna find if it’s better to use !.Any() or .All()

Continue reading

Entity Framework 6.0 code first & TPT, persistence of polymorphic collections

Follow me

Joan Vilariño

Senior .NET Developer at Ohpen
Developer for more than 25 years and IT, gadget, mobile and electronics enthusiast working and publishing from Barcelona.
Follow me

Many times the data structure we need in our applications isn’t as simple as the ideal examples found at most manuals. Sometimes we need more advanced mechanics for the persistence of our data entities.

In today’s article, we’re going to create an entity structure containing a collection of polymorphic elements, in other words, a collection that can contain several object types, all of them inheriting from a common ancestor class.

The problem with polymorphic collections is that, even though their components share a common class, they don’t share all the same properties implemented, so they can’t be saved in a single table at the database. They need a different table for every different final entity.

This is a very common problem but it’s a little difficult to explain. For this, I’ve created a little example project in a “real world” context to try to explain it the best I can, using Entity Framework 6.0 Code First with POCO entities and using the TPT (Table Per Type) feature of EF 6.0+ that simplifies a lot the scenario.

This article includes a link to my Git repository, where you can find a full example solution that will allow you to see the result by yourself, and get your hands dirty making new tests.

Download the solution in .ZIP format
Clone the GIT repository

Continue reading

© 2018 My Coding Corner

Theme by Anders NorénUp ↑

%d bloggers like this:
Skip to toolbar