Thursday 10 July 2014

structure map with factory pattern using structuremap.config

I was looking for a very simple example around for integrating structure map with factory pattern , since I found it in bit and pieces.. I am here documenting my own version of it.


Before I  jump in to code and configuration the jist of below code is that, I would like to create Italian or Indian food object based on what customer request.

StructureMap.config -  make sure you set property Copy to out put directory as Copy always

feel free to download complete code from https://github.com/varunpathak/samples.git
 
 <?xml version="1.0" encoding="utf-8" ?>
<StructureMap>


  <PluginFamily Type="StructureMapHelloWorld.IFood" Assembly="StructureMapHelloWorld" DefaultKey="IndianFoodKey">
    <Plugin Type="StructureMapHelloWorld.SouthIndian" Assembly="StructureMapHelloWorld" ConcreteKey="IndianFoodKey" />
    <Plugin Type="StructureMapHelloWorld.Italian" Assembly="StructureMapHelloWorld" ConcreteKey="ItalianFoodKey" />
  </PluginFamily>



</StructureMap>


 FoodEngine.cs

namespace StructureMapHelloWorld
{
    
    public interface IFood
    {
        string GetFood();
    }

    public class SouthIndian : IFood
    {
        public string GetFood()
        {
            return "Idly-Dosa";
        }
    }

    public class Italian : IFood
    {
        public string GetFood()
        {
            return "Pizza";
        }
    }


}





Program.cs


using StructureMap;
using System;

namespace StructureMapHelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            ConfigureDependencies();


            Console.WriteLine("Type indian for Indian food / type italian for Italian Food / Press E to Exit");

            string foodKind = Console.ReadLine();

            while (String.IsNullOrEmpty(foodKind) == false && foodKind.ToLower() != "e")
            {

                IFood food;

                if (foodKind.ToLower().Contains("italian"))
                {
                    food = ObjectFactory.GetNamedInstance<IFood>("ItalianFoodKey");
                    Console.WriteLine("Thanks for Ordering " + food.GetFood());
                    foodKind = Console.ReadLine();
                }

                else if (foodKind.ToLower().Contains("indian"))
                {
                    food = ObjectFactory.GetNamedInstance<IFood>("IndianFoodKey");
                    Console.WriteLine("Thanks for Ordering " + food.GetFood());
                    foodKind = Console.ReadLine();
                }

                else
                {
                    Console.WriteLine("Type indian for Indian food / type italian for Italian Food / Press E to Exit");
                    foodKind = Console.ReadLine();
                }


            }

            Environment.Exit(0);

        }


        private static void ConfigureDependencies()
        {
            
            ObjectFactory.Initialize(x =>
            {
                // We put the properties for an NHibernate ISession
                // in the StructureMap.config file, so this file
                // must be there for our application to
                // function correctly
                //http://docs.structuremap.net/ConfiguringStructureMap.htm
                x.UseDefaultStructureMapConfigFile = true;
                //x.PullConfigurationFromAppConfig = true;
            });

        }
    }
}
 

No comments:

Post a Comment