Home > Development > StructureMap Registries

StructureMap Registries


Registries allow you to extract the code you use to register components into different locations. Why is this important. Well I use always use a bootstrapper in my applications to load all the types for me. Now I can have each assembly if applicable define a registry of how to mapping in their own assembly.

Lets look at an example.

using System;
using System.Reflection;
using StructureMap;
using StructureMap.Configuration.DSL;

namespace StructureMapExamples
{

    public class EntityBase
    {
        public Guid Id { get; set; }
    }

    public class AggregateRoot : EntityBase { }

    public interface IRepository<TEntity>
        where TEntity : AggregateRoot
    {
        void Save(TEntity entity);
        void Update(TEntity entity);
        void Delete(TEntity entity);
        Guid Get(int id);
    }

    public class Customer : AggregateRoot
    {
        private string _name;

        public Customer(string name)
        {
            _name = name;
        }

        public void AssignName(string name)
        {
            _name = name;
        }
    }

    public class RepositoryBase<TEntity> : IRepository<TEntity>
        where TEntity : AggregateRoot
    {
        public void Save(TEntity entity)
        {
            Console.WriteLine("Saving TEntity: {0}", entity);
        }

        public void Update(TEntity entity)
        {
            Console.WriteLine("Update TEntity: {0}", entity);
        }

        public void Delete(TEntity entity)
        {
            Console.WriteLine("Delete TEntity: {0}", entity);
        }

        public Guid Get(int id)
        {
            Console.WriteLine("Getting TEntity: {0}", id);
            return Guid.Empty;
        }
    }

    public interface ICustomerRepository : IRepository<Customer>
    {

    }

    public class CustomerRepository : RepositoryBase<Customer>, ICustomerRepository
    {

        
    }

    public static class Bootstrapper
    {
        public static void WireUp(IContainer container)
        {
            container.Configure(x => x.Scan(y =>
                                                {

                                                    y.TheCallingAssembly();
                                                    y.LookForRegistries();

                                                }));   
        }
    }

    public class ExampleRegistry : Registry
    {
        public ExampleRegistry()
        {
            For<ICustomerRepository>().Use<CustomerRepository>();
        }
    }

    public class Program
    {

        static void Main(string[] args)
        {
            Program program = new Program();
            program.InitializeContainer();
            program.PrintWhatHasBeenRegisteredInStructureMap();

            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
        }

        public void InitializeContainer()
        {
            Bootstrapper.WireUp(ObjectFactory.Container);
        }

        public void PrintWhatHasBeenRegisteredInStructureMap()
        {
            Console.WriteLine(ObjectFactory.WhatDoIHave());
        }

    }
}


The bootstrapper scans all assemblies for there registries and each registry know how to do their little bit of registration.

Blair..

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: