/*private static ConfiguredInstance ApplyResolver(GenericFamilyExpression builder, StaticResolver resolver)
         *      {
         *              return builder.Add(resolver.Target);
         *      }
         *
         *      private static ObjectInstance ApplyResolver(GenericFamilyExpression builder, InstanceResolver resolver)
         *      {
         *              return builder.Add(resolver.Instance);
         *      }
         *
         *      private static LambdaInstance<object> ApplyResolver(GenericFamilyExpression builder, DynamicResolver resolver)
         *      {
         *              return builder.Add(c => resolver.FactoryFunc());
         *      }*/

        private static GenericFamilyExpression ApplyLifestyleSingle(GenericFamilyExpression registration, Lifestyle lifestyle)
        {
            if (lifestyle.Name == Lifestyle.Singleton.Name)
            {
                return(registration.Singleton());
            }

            if (lifestyle.Name == Lifestyle.Transient.Name)
            {
                return(registration.LifecycleIs(new TransientLifecycle()));
            }

            if (lifestyle.Name == Lifestyle.PerWebRequest.Name)
            {
                return(registration.LifecycleIs(new UniquePerRequestLifecycle()));
            }

            if (lifestyle.Name == Lifestyle.Unmanaged.Name)
            {
                return(registration);
            }

            if (lifestyle.Name == Lifestyle.ProviderDefault.Name)
            {
                return(registration);
            }

            if (lifestyle.Name == Lifestyle.Default.Name)
            {
                return(registration.Singleton());
            }

            throw new ArgumentException(string.Format("Unknown lifestyle : {0}", lifestyle), "lifestyle");
        }
示例#2
0
 public static GenericFamilyExpression Use(this GenericFamilyExpression expression,
                                           object instance, bool dispose)
 {
     if (dispose)
     {
         expression.Use(instance);
     }
     else
     {
         expression.UseLightweightInstance(instance).WithNoLifecycle();
     }
     return(expression);
 }
示例#3
0
        private GenericFamilyExpression SetLifetime(GenericFamilyExpression e)
        {
            switch (LifetimeForRegisters)
            {
            case ServiceRegistrarLifetime.Transient: return(e);    // e.LifecycleIs(InstanceScope.Transient);

            case ServiceRegistrarLifetime.Singleton: return(e.LifecycleIs(InstanceScope.Singleton));

            case ServiceRegistrarLifetime.Thread: return(e.LifecycleIs(InstanceScope.ThreadLocal));

            case ServiceRegistrarLifetime.Request: return(e.LifecycleIs(InstanceScope.PerRequest));

            default: throw new NotSupportedException();
            }
        }
        private static ConfiguredInstance CreateExpression(this GenericFamilyExpression obj,
                                                           bool IsSingleton, Type concrete, string Name)
        {
            if (IsSingleton)
            {
                obj = obj.Singleton();
            }

            var res = obj.Use(concrete);

            if (!string.IsNullOrEmpty(Name))
            {
                res.Named(Name);
            }
            return(res);
        }
        public static GenericFamilyExpression LifecycleIs(this GenericFamilyExpression instance, ServiceLifetime lifetime)
        {
            switch (lifetime)
            {
            case ServiceLifetime.Singleton:
                return(instance.LifecycleIs(Lifecycles.Singleton));

            case ServiceLifetime.Scoped:
                return(instance.LifecycleIs(Lifecycles.Container));

            case ServiceLifetime.Transient:
                return(instance.LifecycleIs(Lifecycles.Unique));

            default:
                throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null);
            }
        }
        //todo: very weirdly, after updating StructureMap to version 3, double dispatch via dynamic not works anymore(http://stackoverflow.com/questions/27011810/mystery-with-dynamic-type-double-dispatch)
        private static object ApplyResolver(GenericFamilyExpression builder, IImplementationResolver resolver)
        {
            if (resolver is StaticResolver)
            {
                return(builder.Add(((StaticResolver)resolver).Target));
            }
            if (resolver is InstanceResolver)
            {
                return(builder.Add(((InstanceResolver)resolver).Instance));
            }
            if (resolver is DynamicResolver)
            {
                return(builder.Add(c => ((DynamicResolver)resolver).FactoryFunc()));
            }

            Assumes.Fail("Unsupported IImplementationResolver implementation");
            return(null);
        }
示例#7
0
 private void addConcretionsThatCouldBeClosed(Type @interface, GenericFamilyExpression expression)
 {
     _concretions.Where(x => x.IsOpenGeneric())
     .Where(x => x.CouldCloseTo(@interface))
     .Each(type =>
     {
         try
         {
             expression.Add(
                 type.MakeGenericType(@interface.GetGenericArguments()));
         }
         catch (Exception)
         {
             // Because I'm too lazy to fight with the f*****g type constraints to "know"
             // if it's possible to make the generic type and this is just easier.
         }
     });
 }
        private static GenericFamilyExpression ApplyLifestyleSingle(GenericFamilyExpression registration, Lifestyle lifestyle)
        {
            if (lifestyle.Name == Lifestyle.Singleton.Name)
                return registration.Singleton();

            if (lifestyle.Name == Lifestyle.Transient.Name)
                return registration.LifecycleIs(InstanceScope.PerRequest);

            if (lifestyle.Name == Lifestyle.PerWebRequest.Name)
                return registration.HttpContextScoped();

            if (lifestyle.Name == Lifestyle.Unmanaged.Name)
                return registration;

            if (lifestyle.Name == Lifestyle.ProviderDefault.Name)
                return registration;

            if (lifestyle.Name == Lifestyle.Default.Name)
                return registration.Singleton();

            throw new ArgumentException(string.Format("Unknown lifestyle : {0}", lifestyle), "lifestyle");
        }
 /// <summary>
 /// Convenience method to mark a PluginFamily as HttpContext scoped
 /// </summary>
 /// <returns></returns>
 public static GenericFamilyExpression HttpContextScoped(this GenericFamilyExpression expression)
 {
     return(expression.LifecycleIs(WebLifecycles.HttpContext));
 }
 /// <summary>
 /// Convenience method to mark a PluginFamily as a Hybrid lifecycle
 /// </summary>
 /// <returns></returns>
 public static GenericFamilyExpression HybridHttpOrThreadLocalScoped(this GenericFamilyExpression expression)
 {
     return(expression.LifecycleIs(WebLifecycles.Hybrid));
 }
 private static ObjectInstance ApplyResolver(GenericFamilyExpression builder, InstanceResolver resolver)
 {
     return builder.Add(resolver.Instance);
 }
 private static ConfiguredInstance ApplyResolver(GenericFamilyExpression builder, StaticResolver resolver)
 {
     return builder.Add(resolver.Target);
 }
 private static LambdaInstance<object> ApplyResolver(GenericFamilyExpression builder, DynamicResolver resolver)
 {
     return builder.Add(c => resolver.FactoryFunc());
 }
示例#14
0
 public static GenericFamilyExpression UseLightweightInstance(
     this GenericFamilyExpression expression, object instance)
 {
     expression.Use(new LightweightObjectInstance(instance));
     return(expression);
 }
示例#15
0
 public static GenericFamilyExpression WithNoLifecycle(this GenericFamilyExpression expression)
 {
     expression.LifecycleIs(new NoLifecycle());
     return(expression);
 }