CODEGATOR

.NET info worth sinking your teeth into!
Welcome to CODEGATOR Sign in | Join | Help
in Search

Martin Cook

Yet another C# developer with a blog.

Complex type converter - part 1

Today I'm going to talk about a utility class that I recently added to my CG.Data library named Converter. The purpose of this class is to convert between instances of model classes in a multi-tiered database application. For instance, in most layered architectures there is a data access layer and a business logic layer. Usually both layers contain one or more model classes that represent items of interest in the system (customers or order or whatever). Some systems are created with a single model library, which works but doesn't do anything to protect different parts of the system from the kind of changes that routinely happen in a complex database application. Having separate model classes in each layer prevents changes in one layer from creating problems in the next layer. 

For instance, in the code sample below I have two typical model classes. DataModel lives in the data access layer and BusinessModel lives in the business logic layer. This is a good arrangement because if we have to add or remove properties to the DataModel class, to support the logic in the business logic layer, we can do that without having to change the code in the BusinessModel class. The same holds true if we need to modify the BusinessModel class - we can do so without touching anything in the data access layer. 

public class DataModel

{

    private string blah;

 

    public string Blah

    {

        get { return blah; }

        set { blah = value; }

    }

}

 

public class BusinessModel

{

    private string blah;

 

    public string Blah

    {

        get { return blah; }

        set { blah = value; }

    }

}


Some folks don't layer their database systems, or they use the DataSet class as a model, or they use a single model class that they share between layers. If you fit within any of those categories then my Converter class may not be of interest to you - but feel free to read on anyway!

So what's the point of all this? Well, having two model classes for each item of interest in a system means more initial coding for the developer - since there are two model classes instead of one. The developer also has the write code to convert from one model class to another. Personally, I can deal with the duplicate model classes but writing the code to convert the data in those models from one class to another just drives me crazy! That is what I created the Converter class for - it will dynamically convert between any two instances of a model class for you. The only caveat is that both types have to contain public properties that are type compatible with each other. The types for each property don't have to be exactly the same but they do have to be something that .NET can convert between. (For instance, .NET can't convert between a double and a DateTime)

Here is a high level look at the interface for my Converter class:


I'll explain about the IConverter interface when I start covering the Converter internals later on. For now, just make note of the fact that the class contains a single public method named Convert. So how do we use that method ask? Good question! We use it just like this:

static class ConverterTest

{

    public static void Test()

    {

        DataModel source = new DataModel();

        source.Blah = "blah to you!";

 

        BusinessModel dest = (BusinessModel)Converter.Default.Convert(

            source,

            typeof(BusinessModel)

            );

 

        MessageBox.Show(dest.Blah);

    }

}


The conversion is capable of going both ways, even though I only demonstrate converting from the data model to the business model. Don't be thrown by the use of the Default property - that's provided by Converter's base class. SingletonBase turns any class into a singleton, and the code for that class is also part of the CG.Common library. [Edit: The SingletonBase class has been renamed to Singleton and moved to the CG.Core library]


So how does the Converter class know what the properties of the model classes are? How does it know how to map property values at runtime? I'll start covering those things tomorrow. Until then, go download the CG.Data library and start playing, or go read some of the cool comics in my cartoons page.

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Martin

I work as a software engineer specializing in designing and building object-oriented business solutions for Windows platforms using C#. I have been programming professionally for roughly 20 years.

This Blog

Syndication

Terms of Service | Privacy Statement