Home > Development > StructureMap Automatic Registration Via Scanning – Part 1

StructureMap Automatic Registration Via Scanning – Part 1


After looking at the previous examples you could imagine if you had a large project this would be a bit of a pain manually.
We can get StructureMap to automatically wire up most of this stuff for us. For example we can get StructureMap to Scan the assembly using the Scan method and wire up concrete class to their interfaces automatically. This maybe to basic for you and we will explorer more advanced usages in other sessions.

using System;
using System.Reflection;
using StructureMap;

namespace StructureMapSamples
{

    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 interface ICustomerRepository : IRepository<Customer>
    {

    }

    public class CustomerRepository : ICustomerRepository
    {

        public void Save(Customer entity)
        {
            Console.WriteLine("Saving Customer: {0}", entity);
        }

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

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

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

    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()
        {
            ObjectFactory.Configure(
                    x => x.Scan(
                            y =>
                            {
                                y.WithDefaultConventions();
                                y.Assembly(Assembly.GetExecutingAssembly());
                            }

                        )
                );
        }

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


    }
}

The with defaults conventions does the automatic concrete class to interface map as mentioned before.

Hope this helps.
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: