Click here to Skip to main content
15,896,477 members
Articles / Web Development / HTML

A WPF Template solution using MVVM and Castle Windsor

Rate me:
Please Sign up or sign in to vote.
4.78/5 (11 votes)
18 Nov 2013CPOL5 min read 34.9K   1.2K   19  
A WPF application base solution using Castle Windsor and commonly used utilities.
using System;
using System.IO;
using System.Reflection;
using System.Windows;
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using Interfaces;

namespace Main
{
    public class Installers : IWindsorInstaller
    {
        /// <summary>
        /// Performs the installation in the <see cref="T:Castle.Windsor.IWindsorContainer" />.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="store">The configuration store.</param>
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            try
            {
                //Adding this code into the installer tells castle that we 
                //are about to specify a new type factory, we now just need 
                //to add a new line to identify the factory as shown below
                container.AddFacility<TypedFactoryFacility>();

                container.Register(Component.For<IAbstructFactory>().AsFactory());
                container.Register(Component.For<IShell<MainWindow>>().ImplementedBy<Shell>().LifestyleTransient());

                container.Register(Classes.FromAssemblyInDirectory(new AssemblyFilter(AssemblyDirectory))
                        .BasedOn<IView>()
                        .Configure(c => c.LifestyleTransient().Named(c.Implementation.Name))
                        .WithService.Base()
                        .WithService.FromInterface(typeof(IView)));

                container.Register(Classes.FromAssemblyInDirectory(new AssemblyFilter(AssemblyDirectory))
                        .BasedOn<IViewModel>()
                        .Configure(c => c.LifestyleTransient().Named(c.Implementation.Name))
                        .WithService.Base()
                        .WithService.FromInterface(typeof(IViewModel)));

                container.Register(Types.FromAssemblyInDirectory(new AssemblyFilter(AssemblyDirectory + "\\CommonData"))
                     .Where(type => type.Name.StartsWith("Common"))
                     .WithService.AllInterfaces());

                container.Register(Types.FromAssemblyInDirectory(new AssemblyFilter(AssemblyDirectory + "\\CommonGui"))
                     .Where(type => type.Name.StartsWith("Common"))
                     .WithService.AllInterfaces());

                container.Register(Types.FromAssemblyInDirectory(new AssemblyFilter(AssemblyDirectory + "\\CommonUtilities"))
                     .Where(type => type.Name.StartsWith("Common"))
                     .WithService.AllInterfaces());


                container.Register(Types.FromAssemblyInDirectory(new AssemblyFilter(AssemblyDirectory + "\\Map"))
                     .Where(type => type.Name.StartsWith("Map"))
                     .WithService.AllInterfaces());


                container.Register(Component.For<IViewFactory>().AsFactory().LifestyleTransient());
                container.Register(Component.For<MainWindow>().LifestyleTransient());

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error installing some components: " + ex.Message);
            }

        }


        /// <summary>
        /// Gets the assembly directory.
        /// </summary>
        /// <value>
        /// The assembly directory.
        /// </value>
        static public string AssemblyDirectory
        {
            get
            {
                var codeBase = Assembly.GetExecutingAssembly().CodeBase;

                var uri = new UriBuilder(codeBase);

                var path = Uri.UnescapeDataString(uri.Path);

                return Path.GetDirectoryName(path);
            }
        }
    }
}

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
Software Developer (Senior) Scodix
Israel Israel
Project Manager and Application Developer, in a wide variety of business applications. Particularly interested in client/server and Graphical User Interface design using Visual C#.

Specialties: 13 Y'rs in C#, 10 Y'rs experience in C++ Highly experienced in wide technologies, IT projects, military projects etc'.

Comments and Discussions