Created
May 12, 2015 03:25
-
-
Save iterativo/9b6555ba6c52072a35b7 to your computer and use it in GitHub Desktop.
Working With StructureMap 3 Profiles
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
interface ICar { } | |
public class Brandless : ICar { } | |
public class Honda : ICar { } | |
public class Toyota : ICar { } | |
[TestFixture] | |
public class WorkingWithProfiles | |
{ | |
[Test] | |
public void ShouldRespectProfiles() | |
{ | |
var container = new Container(_ => | |
{ | |
_.Scan(s => s.TheCallingAssembly()); | |
_.Profile("honda", r => r.For<ICar>().Use<Honda>()); | |
_.Profile("toyota", r => r.For<ICar>().Use<Toyota>()); | |
}); | |
try | |
{ | |
container.GetInstance<ICar>(); | |
} | |
catch (StructureMapConfigurationException) | |
{ | |
Assert.Pass("ICar was not configured in the main container"); | |
} | |
container.GetProfile("honda").GetInstance<ICar>().ShouldBeType<Honda>(); | |
container.GetProfile("toyota").GetInstance<ICar>().ShouldBeType<Toyota>(); | |
} | |
[Test] | |
public void ShouldRespectSingletonsInProfiles() | |
{ | |
var container = new Container(_ => | |
{ | |
_.Scan(s => s.TheCallingAssembly()); | |
_.Profile("honda", r => r.For<ICar>().Use<Honda>().Singleton()); | |
_.Profile("toyota", r => r.For<ICar>().Use<Toyota>()); | |
}); | |
container.GetProfile("honda").GetInstance<ICar>().ShouldBeSameAs(container.GetProfile("honda").GetInstance<ICar>()); | |
container.GetProfile("toyota").GetInstance<ICar>().ShouldNotBeSameAs(container.GetProfile("toyota").GetInstance<ICar>()); | |
} | |
[Test] | |
public void ShouldRespectSingletonsInRootContainer() | |
{ | |
var container = new Container(_ => | |
{ | |
_.Scan(s => s.TheCallingAssembly()); | |
_.For<ICar>().Use<Brandless>().Singleton(); | |
_.Profile("honda", r => r.For<ICar>().Use<Honda>().Singleton()); | |
_.Profile("toyota", r => r.For<ICar>().Use<Toyota>()); | |
}); | |
container.GetInstance<ICar>().ShouldBeSameAs(container.GetInstance<ICar>()); | |
container.GetProfile("honda").GetInstance<ICar>().ShouldBeSameAs(container.GetProfile("honda").GetInstance<ICar>()); | |
container.GetProfile("toyota").GetInstance<ICar>().ShouldNotBeSameAs(container.GetProfile("toyota").GetInstance<ICar>()); | |
} | |
} |
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 ICoffee { } | |
public interface IBeans { } | |
public interface IMilk { } | |
public class Latte : ICoffee | |
{ | |
public readonly IBeans Beans; | |
public readonly IMilk Milk; | |
public Latte(IBeans beans, IMilk milk) | |
{ | |
Beans = beans; | |
Milk = milk; | |
} | |
} | |
public class SpecialtyLatte : ICoffee | |
{ | |
public readonly IBeans Beans; | |
public readonly IMilk Milk; | |
public SpecialtyLatte(IBeans beans, IMilk milk) | |
{ | |
Beans = beans; | |
Milk = milk; | |
} | |
} | |
public class BlendedBeans : IBeans { } | |
public class KenyaBeans : IBeans { } | |
public class SuperSecretBeans : IBeans { } | |
public class WholeMilkGradeA : IMilk { } | |
public class WholeMilkGradeB : IMilk { } | |
[TestFixture] | |
public class MoreComplexProfileDefinitions | |
{ | |
[Test] | |
public void WorkingWithNestedContainers() | |
{ | |
var container = new Container(c => | |
{ | |
c.For<ICoffee>().Use<Latte>(); | |
c.For<IBeans>().Use<BlendedBeans>(); | |
c.Profile("localshop", r => | |
{ | |
r.For<IBeans>().Use<KenyaBeans>(); | |
r.For<IMilk>().Use<WholeMilkGradeA>(); | |
}); | |
c.Profile("starbucks", r => r.For<IMilk>().Use<WholeMilkGradeB>()); | |
c.Profile("specialtyshop", r => | |
{ | |
r.For<ICoffee>().Use<SpecialtyLatte>(); | |
r.For<IBeans>().Use<SuperSecretBeans>(); | |
r.For<IMilk>().Use<WholeMilkGradeA>(); | |
}); | |
}); | |
using (var localshop = container.GetNestedContainer("localshop")) | |
{ | |
var latte = (Latte)localshop.GetInstance<ICoffee>(); | |
latte.Beans.ShouldBeType<KenyaBeans>(); | |
latte.Milk.ShouldBeType<WholeMilkGradeA>(); | |
} | |
using (var starbucks = container.GetNestedContainer("starbucks")) | |
{ | |
var latte = (Latte)starbucks.GetInstance<ICoffee>(); | |
latte.Beans.ShouldBeType<BlendedBeans>(); | |
latte.Milk.ShouldBeType<WholeMilkGradeB>(); | |
} | |
using (var specialtyshop = container.GetNestedContainer("specialtyshop")) | |
{ | |
var specialtyLatte = (SpecialtyLatte) specialtyshop.GetInstance<ICoffee>(); | |
specialtyLatte.Beans.ShouldBeType<SuperSecretBeans>(); | |
specialtyLatte.Milk.ShouldBeType<WholeMilkGradeA>(); | |
} | |
} | |
[Test] | |
public void WorkingWithProfiles() | |
{ | |
var container = new Container(c => | |
{ | |
c.For<ICoffee>().Use<Latte>(); | |
c.For<IBeans>().Use<BlendedBeans>(); | |
c.Profile("localshop", r => | |
{ | |
r.For<IBeans>().Use<KenyaBeans>(); | |
r.For<IMilk>().Use<WholeMilkGradeA>(); | |
}); | |
c.Profile("starbucks", r => r.For<IMilk>().Use<WholeMilkGradeB>()); | |
c.Profile("specialtyshop", r => | |
{ | |
r.For<ICoffee>().Use<SpecialtyLatte>(); | |
r.For<IBeans>().Use<SuperSecretBeans>(); | |
r.For<IMilk>().Use<WholeMilkGradeA>(); | |
}); | |
}); | |
using (var localshop = container.GetProfile("localshop")) | |
{ | |
var latte = (Latte)localshop.GetInstance<ICoffee>(); | |
latte.Beans.ShouldBeType<KenyaBeans>(); | |
latte.Milk.ShouldBeType<WholeMilkGradeA>(); | |
} | |
using (var starbucks = container.GetProfile("starbucks")) | |
{ | |
var latte = (Latte)starbucks.GetInstance<ICoffee>(); | |
latte.Beans.ShouldBeType<BlendedBeans>(); | |
latte.Milk.ShouldBeType<WholeMilkGradeB>(); | |
} | |
using (var specialtyshop = container.GetProfile("specialtyshop")) | |
{ | |
var specialtyLatte = (SpecialtyLatte) specialtyshop.GetInstance<ICoffee>(); | |
specialtyLatte.Beans.ShouldBeType<SuperSecretBeans>(); | |
specialtyLatte.Milk.ShouldBeType<WholeMilkGradeA>(); | |
} | |
} | |
[Test] | |
public void GettingContainerViaGetProfileIsNotTheSameAsGettingProfileViaGetNestedContainer() | |
{ | |
var container = new Container(c => | |
{ | |
c.For<IMilk>().Use<WholeMilkGradeB>(); | |
c.Profile("specialtyshop", r => r.For<IMilk>().Use<WholeMilkGradeA>()); | |
}); | |
var specialtyShopProfile = container.GetProfile("specialtyshop"); | |
var specialtyShopAsNestedContainer = container.GetNestedContainer("specialtyshop"); | |
specialtyShopProfile.ProfileName.ShouldEqual("specialtyshop"); | |
specialtyShopAsNestedContainer.ProfileName.ShouldEqual("specialtyshop - Nested"); | |
container.Role.ShouldEqual(ContainerRole.Root); | |
specialtyShopProfile.Role.ShouldEqual(ContainerRole.ProfileOrChild); | |
specialtyShopAsNestedContainer.Role.ShouldEqual(ContainerRole.Nested); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment