开发者

Castle Windsor Typed Factory Weirdness

I'm getting some very unexpected (I think) behavior with the Typed Factory facility. Basically, it's reusing transient component instances for constructor injection with Func<T>.

Here is the gist of it:

// this guy is registered as service EntityFrameworkRepositoryProvider, not IRepository
[Transient] 
public class EntityFrameworkRepositoryProvider : IRepository 
{
    public EntityFrameworkRepositoryProvider(ObjectContext objectContext, Assembly assembly)
    {
         // HOLY MOLY BATMAN!! Every time I hit this constructor when getProvider gets called,
         // I don't actually get the arguments passed into getProvider. I get the 
         // arguments that were passed into getProvider the very FIRST time 
         // it was called...in other words I get the wrong 
         // ObjectContext (for the wrong connection string)...BIG PROBLEM


         // do stuff...
    }
}

[Singleton]
// this guy gets registered on startup
internal class EntityFrameworkRepositoryProviderFactory : IRepositoryProviderFactory
{
      private readonly Func<ObjectContext, Assembly, EntityFrameworkRepositoryProvider> getProvider;

      public EntityFrameworkRepositoryProviderFactory(Func<ObjectContext, Assembly, EntityFrameworkRepositor开发者_JAVA技巧yProvider> getProvider) 
      {
          this.getProvider = getProvider;
          // do stuff...
      }

      public IRepository CreateRepository()
      {
          var provider = getProvider(new ObjectContext(GetConnectionString()),
              Assembly.GetExecutingAssembly);
          // do stuff...
      }

      public string GetConnectionString() 
      { 
             // returns one of a few diff EF connection strings
      }          
}

I also have a standard LazyComponentLoader. What am I doing wrong here? What should I do instead?

Thanks.


Problem was this seemingly harmless code in the LazyComponentLoader:

        public IRegistration Load(string key, Type service, IDictionary arguments)
        {
            var component = Component.For(service);

            if (!string.IsNullOrEmtpy(key))
            {
                component = component.Named(key);
            }

            if (arguments != null)
            {
                // This makes the typed factory facility screw up. (Merge is an extension method that merges dictionaries)
                component.DynamicParameters((k, d) => d.Merge(arguments));
            }                

            return component;
        }

   public static void Merge(this IDictionary target, IDictionary source)
    {
        foreach (object key in source.Keys)
        {
            target[key] = source[key];
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜