title: How to reuse a transient dependency in same context with Castle Windsor DI container
body: <p>If I have the following setup, how can I configure my container to use the same database, when objects are created in the same context</p>
  
  <pre><code>public class Database { }
  public interface IRepository { Database Database { get; } }
  public interface IFooRepository : IRepository { }
  public interface IBarRepository : IRepository { }
  
  public class FooRepository : IFooRepository
  {
      public Database Database { get; private set; }
      public FooRepository(Database database) { this.Database = database; }
  }
  
  public class BarRepository : IBarRepository
  {
      public Database Database { get; private set; }
      public BarRepository(Database database) { this.Database = database; }
  }
  
  public class Consumer
  {
      public IFooRepository fooRepository { get; private set; }
      public IBarRepository barRepository { get; private set; }
      public Consumer(IFooRepository fooRepository, IBarRepository barRepository)
      {
          this.fooRepository = fooRepository;
          this.barRepository = barRepository;
      }
  }
  
  [TestClass]
  public class ConfigurationTest
  {
      private IWindsorContainer container;
  
      [TestMethod]
      public void SameDatabaseIsUsed()
      {
          Consumer consumer = container.Resolve&lt;Consumer&gt;();
          IFooRepository fooRepository = consumer.fooRepository;
          IBarRepository barRepository = consumer.barRepository;
          Assert.AreEqual(fooRepository.Database, barRepository.Database); //FAILS
      }
  
      [TestMethod]
      public void DifferentDatabaseIsUsed()
      {
          Consumer consumer = container.Resolve&lt;Consumer&gt;();
          IFooRepository fooRepository = consumer.fooRepository;
          Consumer consumer2 = container.Resolve&lt;Consumer&gt;();
          IBarRepository barRepository = consumer2.barRepository;
          Assert.AreNotEqual(fooRepository.Database, barRepository.Database); //PASSES
      }
  
      [TestInitialize]
      public void SetUp()
      {
          container = new WindsorContainer();
          container.Register(
              Component.For&lt;Database&gt;().ImplementedBy&lt;Database&gt;().LifeStyle.Transient,
              AllTypes.FromThisAssembly()
                      .BasedOn&lt;IRepository&gt;().WithService.FromInterface()
                      .Configure(c =&gt; c.LifeStyle.Transient),
              Component.For&lt;Consumer&gt;().ImplementedBy&lt;Consumer&gt;().LifeStyle.Transient
                      );
  
      }
  }
  </code></pre>
  
  <p>EDIT:
  I have tried to use a custom lifestyle, but i cannot figure out what I can use to detect that i have switched context</p>
  
  <pre><code>public class DatabaseLifestyleManager : AbstractLifestyleManager
  {
      private CreationContext context;
      private Database database;
  
      private Database Database
      {
          get
          {
              if (database == null) database = new Database();
              return database;
          }
          set 
          {
              database = null;
          }
      }
  
      public override object Resolve(CreationContext context)
      {
          if (this.context!=null &amp;&amp; this.context.??? == context.???)
              return Database;
          else
          {
              this.context = context;
              Database = null;
              return Database;
          }
  
      }
  
      public override void Dispose()
      {
          database = null;
          context = null;
      }
  }
  ......
  Component.For&lt;Database&gt;().ImplementedBy&lt;Database&gt;().LifeStyle.Custom(typeof(DatabaseLifestyleManager)
  </code></pre>