Click here to Skip to main content
15,889,034 members
Articles / Desktop Programming / Windows Forms

Creating an outlook like side bar using the smart client software factory

Rate me:
Please Sign up or sign in to vote.
4.61/5 (14 votes)
26 Sep 200611 min read 146.7K   6.2K   111  
This is basically a tutorial to use the smart client software factory to create an outlook like side bar using Matias Woloski's outlookbar workspace
//----------------------------------------------------------------------------------------
// 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.scsf.2006jun/SCSF/html/03-470-Translating%20Entities.htm
//
// Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182
//----------------------------------------------------------------------------------------

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

namespace Matias.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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions