WEAVE 06.09 – Softwarearchitektur

Flexible User Interfaces

Damit Anwendungen einfach und flexibel angepasst werden können, müssen sie von vorneherein logisch aufgebaut sein. Das spart nicht nur Zeit und Geld sondern auch Nerven. Wie man ganz einfach ein Desktop-User-Interface gegen ein Web-UI austauscht, erklärt Golo Roden in WEAVE 06.2009

Das endgültige Resultat:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Microkernel
{
    ///
    /// Represents a service locator.
    ///
    public static class ServiceLocator
    {
        ///
        /// Contains the mappings from contracts to types.
        ///
        private static Dictionary _mappings;

        ///
        /// Initializes the  type.
        ///
        static ServiceLocator()
        {
            // Load the configuration.
            _mappings =
                (from mapping in
                    XElement.Load("ServiceLocator.xml").Element("mappings").Elements("mapping")
                select
                    new KeyValuePair(
                        Type.GetType(mapping.Attribute("contract").Value),
                        Type.GetType(mapping.Attribute("type").Value))).ToDictionary(
                    kvp => kvp.Key, kvp => kvp.Value);
        }

        ///
        /// Gets an instance for the specified contract.
        ///
        /// The type of the contract.
        /// An instance that matches the specified contract.
        public static TContract GetService() where TContract : class
        {
            // Instantiate the type and return it to the caller.
            return (TContract)Activator.CreateInstance(_mappings[typeof(TContract)]);
        }
    }
}

Die Konfigurationsdatei, damit der Mikrokernel weiß, welche Klasse bei der Anforderung einer Schnittstelle instanziiert werden soll.





	

TwitterFacebookMister-WongDeliciousGoogle BookmarksIdenti.caShare