title: C# Proxies and the var keyword
body: <p>This question is related to a previous post of mine <a href="https://stackoverflow.com/questions/227225/is-injecting-dao-into-entities-a-bad-thing">Here</a>. Basically, I want to inject a DAO into an entity i.e. </p>
  
  <pre><code>public class User
  {
     IUserDAO userDAO;
     public User()
     {
           userDAO = IoCContainer.Resolve&lt;IUserDAO&gt;;
     }
    public User(IUserDAO userDAO)
     {
           this.userDAO = userDAO;
     }
     //Wrapped DAO methods i.e
     public User Save()
     {
         return userDAO.Save(this);
     }
  
  }
  </code></pre>
  
  <p>Here if I had a custom methods in my DAO then I basically have to wrap them in the entity object. So if I had a IUserDAO.Register() I would then have to create a User.Register() method to wrap it. </p>
  
  <p>What would be better is to create a proxy object where the methods from the DAO are dynamically assign to the User object. So I may have something that looks like this:</p>
  
  <pre><code>var User = DAOProxyService.Create(new User());
  User.Save();
  </code></pre>
  
  <p>This would mean that I can keep the User entity as a pretty dumb class suitable for data transfer over the wire, but also magically give it a bunch of DAO methods.</p>
  
  <p>This is very much out of my confort zone though, and I wondered what I would need to accomplish this? Could I use Castles Dynamic proxy? Also would the C# compiler be able to cope with this and know about the dynamically added methods? </p>
  
  <p>Feel free to let me know if this is nonsense. </p>
  
  <p>EDIT:</p>
  
  <blockquote>
    <p>What we need to do it somehow declare DAOProxyService.Create() as returning a User object -- at compile time. This can be done with generics.</p>
  </blockquote>
  
  <p>This isnt quite true, what I want to return isn't a User object but a User object with dynamically added UserDAO methods. As this class isn't defnied anywhere the compiler will not know what to make of it. </p>
  
  <p>What I am essentially returning is a new object that looks like: User : IUserDAO, so I guess I could cast as required. But this seems messy. </p>
  
  <p>Looks like what I am looking for is similar to this: <a href="http://www.almostserio.us/articles/2005/01/12/mixins-using-castle-dynamicproxy" rel="nofollow noreferrer">Mixins</a></p>