Click here to Skip to main content
15,860,943 members
Articles / Desktop Programming / WPF

Introduction to Model Driven Development with Sculpture – Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (23 votes)
3 Sep 2008CPOL15 min read 113.5K   759   124  
This article introduces how to create and manage .NET enterprise applications using your favorite technology (Data Access Application Block, LINQ, NHibernate, ASMX, and WCF) with the Model Driven Development approach by Sculpture.
//----------------------------------------------------------------------------------------
// patterns & practices - Smart Client Software Factory - Guidance Package
//
// This file was generated by this guidance package as part of the solution template
//
// The EntityTranslatorService class is a service that provides a registry of
// translators and translation services between two classes. The user must implement
// the translators and register them with the service.
// 
// For more information see: 
// ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.practices.scsf.2007may/SCSF/html/03-01-090-How_to_Translate_Between_Business_Entities_and_Service_Entities.htm
//
// Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182
//----------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using DialogBox.Infrastructure.Interface.Services;

namespace DialogBox.Infrastructure.Library.Services
{

    public class EntityTranslatorService : IEntityTranslatorService
    {
        private List<IEntityTranslator> _translators = new List<IEntityTranslator>();

        public void RegisterEntityTranslator(IEntityTranslator translator)
        {
            if (translator == null)
                throw new ArgumentNullException("translator");

            _translators.Add(translator);
        }

        public void RemoveEntityTranslator(IEntityTranslator translator)
        {
            if (translator == null)
                throw new ArgumentNullException("translator");

            _translators.Remove(translator);
        }

        public bool CanTranslate<TTarget, TSource>()
        {
            return CanTranslate(typeof(TTarget), typeof(TSource));
        }

        public bool CanTranslate(Type targetType, Type sourceType)
        {
            if (targetType == null)
                throw new ArgumentNullException("targetType");
            if (sourceType == null)
                throw new ArgumentNullException("sourceType");

            return IsArrayConversionPossible(targetType, sourceType) || FindTranslator(targetType, sourceType) != null;
        }

        public TTarget Translate<TTarget>(object source)
        {
            return (TTarget)Translate(typeof(TTarget), source);
        }

        public object Translate(Type targetType, object source)
        {
            if (targetType == null)
                throw new ArgumentNullException("targetType");

            if (source == null)
            {
                if (targetType.IsArray)
                {
                    return null;
                }
                else
                {
                    throw new ArgumentNullException("source");
                }
            }

            Type sourceType = source.GetType();

            if (IsArrayConversionPossible(targetType, sourceType))
            {
                return TranslateArray(targetType, source);
            }
            else
            {
                IEntityTranslator translator = FindTranslator(targetType, sourceType);
                if (translator != null)
                {
                    return translator.Translate(this, targetType, source);
                }
            }

            throw new EntityTranslatorException("No translator is available to perform the operation.");
        }

        private object TranslateArray(Type targetType, object source)
        {
            Type targetItemType = targetType.GetElementType();
            Array sourceArray = (Array)source;
            Array result = (Array)Activator.CreateInstance(targetType, sourceArray.Length);
            for (int i = 0; i < sourceArray.Length; i++)
            {
                object value = sourceArray.GetValue(i);
                if (value != null)
                    result.SetValue(Translate(targetItemType, sourceArray.GetValue(i)), i);
            }
            return result;
        }

        private bool IsArrayConversionPossible(Type targetType, Type sourceType)
        {
            if (targetType.IsArray && targetType.GetArrayRank() == 1 && sourceType.IsArray && sourceType.GetArrayRank() == 1)
            {
                return CanTranslate(targetType.GetElementType(), sourceType.GetElementType());
            }
            return false;
        }

        private IEntityTranslator FindTranslator(Type targetType, Type sourceType)
        {
            IEntityTranslator translator = _translators.Find(delegate(IEntityTranslator test)
            {
                return test.CanTranslate(targetType, sourceType);
            });

            return translator;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer www.Dawliasoft.com
Egypt Egypt
Program Manager in Sculpture project, Interesting in .NET Model driven development.

Comments and Discussions