title: Injecting a factory method using StructureMap
body: <p>I'm using a <a href="http://mvcmembership.codeplex.com/" rel="noreferrer">wrapper for the ASP.NET Membership provider</a> so that I can have more loosely coupled usage of the library. I'm wanting to use StructureMap to provide true IoC, but I'm having trouble with configuring it with a User-to-Profile factory object that I'm using to put instantiate the profile in the context of a user.  Here's the relevant details, first the interface and wrapper from the library:</p>
  
  <pre><code>// From ASP.Net MVC Membership Starter Kit
  public interface IProfileService
  {
      object this[string propertyName] { get; set; }
      void SetPropertyValue(string propertyName, object propertyValue);
      object GetPropertyValue(string propertyName);
      void Save();
  }
  
  public class AspNetProfileBaseWrapper : IProfileService
  {
      public AspNetProfileBaseWrapper(string email) {}
  
      // ...
  }
  </code></pre>
  
  <p>Next, a repository for interacting with a particular property in the profile data:</p>
  
  <pre><code>class UserDataRepository : IUserDataRepository
  {
      Func&lt;MembershipUser, IProfileService&gt; _profileServiceFactory;
  
      // takes the factory as a ctor param 
              // to configure it to the context of the given user
      public UserDataRepository(
                Func&lt;MembershipUser, IProfileService&gt; profileServiceFactory)
      {
          _profileServiceFactory = profileServiceFactory;
      }
  
      public object GetUserData(MembershipUser user)
      {
          // profile is used in context of a user like so:
          var profile = _profileServiceFactory(user);
          return profile.GetPropertyValue("UserData");
      }
  }
  </code></pre>
  
  <p>Here's my first attempt at providing a StructureMap registry config, but it obviously doesn't work:</p>
  
  <pre><code>public class ProfileRegistry : Registry
  {
      public ProfileRegistry()
      {
          // doesn't work, still wants MembershipUser and a default ctor for AspNetProfileBaseWrapper
          For&lt;IProfileService&gt;().Use&lt;AspNetProfileBaseWrapper&gt;();
      }
  }
  </code></pre>
  
  <p>Theoretically how I'd like to register it would look something like:</p>
  
  <pre><code>// syntax failure :)
  For&lt;Func&lt;MembershipUser, IProfileService&gt;&gt;()
    .Use&lt;u =&gt; new AspNetProfileBaseWrapper(u.Email)&gt;();
  </code></pre>
  
  <p>...where I can define the factory object in the configuration.  This is obviously not valid syntax.  Is there an easy way to accomplish this?  Should I be using some other pattern to allow constructing my UserDataRepository in the context of a user?  Thanks!</p>