title: Castle Windsor Factory implementation
body: <p>I am using a Typed Factory supplied by Windsor Castle DI container.  I am quite new to DI containers, so am after a bit of guidance with my solution.</p>
  
  <p>Here is my implementation:</p>
  
  <p>*updated registration &amp; DB call</p>
  
  <pre><code>public interface IAgent {    }
  public class Agent : IAgent     {    }
  
  public interface IAgentFactory     
  {
      IAgent Create();
      IAgent Create(int agentId);
      IAgent Create(AgentDTO agentDTO);
  }
  
  class AgentFactory : IAgentFactory     
  {        
      public IAgent Create()         
      {            
          return InitNewEntity(new Agent());
      }
      public IAgent Create(int agentId, IDBContext dbContext) //Not happy with this, as it's a dependency that isn't factored out :(    
      {
          return dbContext.GetAgent(agentId);
      }
      public IAgent Create(AgentDTO agentDTO)
      {
           Agent agent = InitNewEntity(new Agent());
           agent.ParseDTO(agentDTO);
           return agent;
      }
      private IAgent InitNewEntity(IAgent agent)
      {
           agent.Username = "";        /// + other fields to initialise
           agent.DOB = DateTime.Now;   /// etc.
           return agent;
      }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>Container.AddFacility&lt;TypedFactoryFacility&gt;()
              .Register( Component.For&lt;IAgentFactory&gt;()
                                  .ImplementedBy&lt;AgentFactory&gt;());
  </code></pre>
  
  <p>which I'm using the following call to get some entities</p>
  
  <pre><code>IAgentFactory agentFactory = ViewModel.Container.Resolve&lt;IAgentFactory&gt;();
  
  IAgent agent = agentFactory.Create();  //Creates new Agent entity
  agent = agentFactory.Create(66, dbContext); //Looks up entity in database, don't like passing in a db context, another dependency
  agent = agentFactory.Create(agentDTO);      //Creates Agent entity from DTO object
  </code></pre>
  
  <p>I have several concerns about this.</p>
  
  <p>There are 3 possible scenarios regarding creating a new Agent, </p>
  
  <p>1: Create a new agent from scratch</p>
  
  <p>2: Create an agent using an existing DTO (Data Transfer Object)</p>
  
  <p>3: Create an agent with a call to the database.</p>
  
  <p>I decided to put all this functionality into a factory, as it seems to fit the bill, however, I am not entirely sure if this is the correct or best way to accomplish this.</p>
  
  <p>The question is, is it possible to leverage all 3 Create scenarios into the DI container Register statement and omit the concrete factory implementation altogether, so that I no longer have an AgentFactory class, but only the IAgentFactory interface.</p>
  
  <p>Another question I have regarding this implementation, is it possible to do a call, such that if I request an Agent entity, Windsor will first make a call to the factory and return the entity created from the factory, rather than create the entity without referencing the factory.
  ie.</p>
  
  <pre><code>IAgent agent = Container.Resolve&lt;IAgent&gt;(66);
  </code></pre>
  
  <p>With a Callstack:</p>
  
  <p>Container -> AgentFactory -> Create(66) -> returns agent from factory.</p>
  
  <p>Which Windsor will intercept and automatically use the factory to create the instance.  However, I tried this and it didn't use the factory to create it, it just went ahead and created the Agent without referencing the factory.</p>
  
  <p>I did have this, but it doesn't call the factory methods that I have defined.</p>
  
  <pre><code>Container.AddFacility&lt;TypedFactoryFacility&gt;()
              .Register(Component.For&lt;IAgentFactory&gt;().AsFactory(),
                        Component.For&lt;IAgent&gt;().ImplementedBy&lt;Agent&gt;());
  </code></pre>
  
  <p>Advice is appreciated, thank you.</p>