Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@marcin-chwedczuk
Created July 30, 2014 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcin-chwedczuk/862ab6c62b5b4a444202 to your computer and use it in GitHub Desktop.
Save marcin-chwedczuk/862ab6c62b5b4a444202 to your computer and use it in GitHub Desktop.
Using scoped lifestyle windsor.castle
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Castle.MicroKernel.Lifestyle;
namespace Castle_ScopeLifetime {
class Program {
static void Main(string[] args) {
IWindsorContainer installer = new WindsorContainer();
installer.Register(
Component.For<ITranscient>()
.ImplementedBy<Transcient>()
.LifestyleTransient(),
Component.For<ISingleton>()
.ImplementedBy<Singleton>()
.LifestyleSingleton(),
Component.For<IScoped>()
.ImplementedBy<Scoped>()
.LifestyleScoped(),
Component.For<IScopeProvider>()
.Instance(new ScopeProvider(installer))
);
IScopeProvider provider = installer.Resolve<IScopeProvider>();
installer.Release(provider);
provider = installer.Resolve<IScopeProvider>();
for (int i = 0; i < 3; i++) {
Console.WriteLine("-------------------------------");
var transcient = installer.Resolve<ITranscient>();
transcient.DoSomething();
installer.Release(transcient);
var singleton = installer.Resolve<ISingleton>();
singleton.DoSomething();
installer.Release(singleton);
using (provider.BeginScope()) {
for (int j = 0; j < 3; j++) {
var scoped = installer.Resolve<IScoped>();
scoped.DoSomething();
}
}
}
installer.Dispose();
Console.WriteLine("THE END");
Console.ReadLine();
}
}
public class Scope : IDisposable {
private readonly IDisposable _windsorScopeObject;
public Scope(IWindsorContainer container) {
if (container == null)
throw new ArgumentNullException("container");
Console.WriteLine("SCOPE START");
_windsorScopeObject = container.BeginScope();
}
public void Dispose() {
_windsorScopeObject.Dispose();
Console.WriteLine("SCOPE END");
}
}
public interface IScopeProvider {
Scope BeginScope();
}
public class ScopeProvider : IScopeProvider {
private readonly IWindsorContainer _container;
public ScopeProvider(IWindsorContainer container) {
if (container == null)
throw new ArgumentNullException("container");
_container = container;
}
public Scope BeginScope() {
return new Scope(_container);
}
}
public interface ISingleton {
void DoSomething();
}
class Singleton : ISingleton, IDisposable {
public Singleton() {
Console.WriteLine("Singleton constructor");
}
public void DoSomething() {
Console.WriteLine("Singleton");
}
public void Dispose() {
Console.WriteLine("Singleton Disposed");
}
}
public interface ITranscient {
void DoSomething();
}
public class Transcient : ITranscient, IDisposable {
public Transcient() {
Console.WriteLine("Transcient constructor");
}
public void DoSomething() {
Console.WriteLine("Transcient");
}
public void Dispose() {
Console.WriteLine("Transcient Disposed");
}
}
public interface IScoped {
void DoSomething();
}
public class Scoped : IScoped, IDisposable {
public Scoped() {
Console.WriteLine("Scoped constructor");
}
public void DoSomething() {
Console.WriteLine("Scoped");
}
public void Dispose() {
Console.WriteLine("Scoped Disposed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment