Created
November 25, 2011 09:10
-
-
Save hazzik/1393099 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Diagnostics; | |
using Castle.Facilities.TypedFactory; | |
using Castle.MicroKernel.Registration; | |
using Castle.Windsor; | |
using Xunit; | |
public class LifestileTests | |
{ | |
[Fact] | |
public void TestForTypedFactories() | |
{ | |
using (var container = new WindsorContainer()) | |
{ | |
container.AddFacility<TypedFactoryFacility>(); | |
container.Register(Component.For<IFactory>().AsFactory(), | |
Component.For(typeof(IInterface)).ImplementedBy(typeof(InterfaceImpl)).LifeStyle.Transient); | |
IFactory childFactory; | |
using (var childContainer = new WindsorContainer()) | |
{ | |
container.AddChildContainer(childContainer); | |
childFactory = childContainer.Resolve<IFactory>(); | |
} // childFactory is disposing here | |
var factory = container.Resolve<IFactory>(); | |
Assert.Same(factory, childFactory); | |
Assert.DoesNotThrow(() => factory.Create()); // throws an ObjectDisposedException exception | |
} // but should be disposed here | |
} | |
[Fact] | |
public void TestForSerivces() | |
{ | |
using (var container = new WindsorContainer()) | |
{ | |
container.Register(Component.For<IInterface>().ImplementedBy<InterfaceImpl>()); | |
IInterface childInterface; | |
using (var childContainer = new WindsorContainer()) | |
{ | |
container.AddChildContainer(childContainer); | |
childInterface = container.Resolve<IInterface>(); | |
} // childIhterface is NOT disposing here | |
var @interface = container.Resolve<IInterface>(); | |
Assert.Same(@interface, childInterface); | |
@interface.Do(); | |
} // but is disposing here and this is right behavior | |
} | |
#region Nested type: IFactory | |
public interface IFactory | |
{ | |
IInterface Create(); | |
} | |
#endregion | |
#region Nested type: IInterface | |
public interface IInterface | |
{ | |
void Do(); | |
} | |
#endregion | |
#region Nested type: InterfaceImpl | |
public class InterfaceImpl : IInterface,IDisposable | |
{ | |
private bool disposed; | |
public void Dispose() | |
{ | |
disposed = true; | |
Console.WriteLine(new StackTrace(true)); | |
} | |
public void Do() | |
{ | |
if (disposed) | |
throw new NotSupportedException(); | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment