Created
September 23, 2014 01:21
-
-
Save muralinarasimhan/49361dd717e9cffcee6b to your computer and use it in GitHub Desktop.
Parameterized Abstract Factory with NancyFx TinyIoC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CafeModule : NancyModule | |
{ | |
public CafeModule( IDrinkFactory iDrinkFactory) | |
: base("/api/drinks") | |
{ | |
Get["/{id}"] = _ => | |
{ | |
IDrink drink = iDrinkFactory.Create((int) _.id); | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Coffee : IDrink | |
{ | |
public string GetLabel() | |
{ | |
return "Nescafe"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomNancyBootstrapper : DefaultNancyBootstrapper | |
{ | |
protected override void ApplicationStartup( | |
TinyIoCContainer container, | |
Nancy.Bootstrapper.IPipelines pipelines) | |
{ | |
Coffee coffee = new Coffee(); | |
Tea tea = new Tea(); | |
container.Register<IDrink>(coffee, | |
EnumHelper.GetEnumDescription(DrinkType.Coffee)); | |
container.Register<IDrink>(tea, | |
EnumHelper.GetEnumDescription(DrinkType.Tea)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DrinkFactory : IDrinkFactory | |
{ | |
private Func<String, IDrink> drink; | |
public DrinkFactory(Func<String, IDrink> iDrink) | |
{ | |
drink = iDrink; | |
} | |
public IDrink Create(int drinkType) | |
{ | |
switch (drinkType) | |
{ | |
case 1: | |
return drink.Invoke( | |
EnumHelper.GetEnumDescription(DrinkType.Coffee)); | |
case 2: | |
return drink.Invoke( | |
EnumHelper.GetEnumDescription(DrinkType.Tea)); | |
default: | |
return null; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum DrinkType | |
{ | |
[Description("Coffee")] | |
Coffee = 1, | |
[Description("Tea")] | |
Tea | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EnumHelper | |
{ | |
public static string GetEnumDescription(Enum value) | |
{ | |
FieldInfo fi = value.GetType().GetField(value.ToString()); | |
DescriptionAttribute[] attributes = | |
(DescriptionAttribute[]) fi.GetCustomAttributes( | |
typeof (DescriptionAttribute), false); | |
if (attributes != null && attributes.Length > 0) | |
{ | |
return attributes[0].Description; | |
} | |
else | |
{ | |
return value.ToString(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDrink | |
{ | |
string GetLabel(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDrinkFactory | |
{ | |
IDrink Create(int drinkType); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Tea : IDrink | |
{ | |
public string GetLabel() | |
{ | |
return "Tetley"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment