示例#1
0
        private static ButterflyParser LoadStrategiesFromTypes(this ButterflyParser parser, IParseStrategyFactory strategyFactory, IEnumerable<Type> types)
        {
            var strategyInterface = typeof(ParseStrategy);
            types = types.Where(type => type.IsClass && !type.IsAbstract && strategyInterface.IsAssignableFrom(type));

            types.Walk(type => parser.AddStrategy(strategyFactory.Create(type)));

            return parser;
        }
 public static PocketContainer IncludeDependencyResolver(
     this PocketContainer container,
     IDependencyResolver dependencyResolver)
 {
     return container.AddStrategy(type =>
     {
         if (dependencyResolver.GetService(type) != null)
         {
             return c => dependencyResolver.GetService(type);
         }
         return null;
     });
 }
 /// <summary>
 /// Uses Its.Configuration to resolve unregistered types whose name ends with "Settings".
 /// </summary>
 /// <param name="container">The container.</param>
 /// <returns>The same container instance.</returns>
 public static PocketContainer UseItsConfigurationForSettings(
     this PocketContainer container)
 {
     return container.AddStrategy(type =>
     {
         if (!type.IsInterface &&
             !type.IsAbstract &&
             !type.IsGenericTypeDefinition &&
             type.Name.EndsWith("Settings"))
         {
             return c => Settings.Get(type);
         }
         return null;
     });
 }
        public static PocketContainer UseImmediateCommandScheduling(this PocketContainer container)
        {
            return container.AddStrategy(type =>
            {
                if (type.IsInterface &&
                    type.IsGenericType &&
                    type.GetGenericTypeDefinition() == typeof (ICommandScheduler<>))
                {
                    var targetType = type.GetGenericArguments().First();
                    var schedulerType = typeof (CommandScheduler<>).MakeGenericType(targetType);

                    return c => c.Resolve(schedulerType);
                }

                return null;
            });
        }
 public static PocketContainer AutoMockInterfacesAndAbstractClasses(
     this PocketContainer container)
 {
     return container.AddStrategy(type =>
     {
         if (type.IsInterface || type.IsAbstract)
         {
             var moqType = typeof (Mock<>).MakeGenericType(type);
             return c =>
             {
                 var mock = Activator.CreateInstance(moqType) as Mock;
                 mock.DefaultValue = DefaultValue.Mock;
                 return ((dynamic) mock).Object;
             };
         }
         return null;
     });
 }
        public static PocketContainer IfOnlyOneImplementationUseIt(
            this PocketContainer container)
        {
            return container.AddStrategy(type =>
            {
                if (type.IsInterface || type.IsAbstract)
                {
                    var implementations = Discover.ConcreteTypesDerivedFrom(type)
                                                  .ToArray();

                    if (implementations.Count() == 1)
                    {
                        var implementation = implementations.Single();
                        return c => c.Resolve(implementation);
                    }
                }
                return null;
            });
        }
        public static PocketContainer AddStoreStrategy(this PocketContainer container)
        {
            return container.AddStrategy(type =>
            {
                if (type.IsInterface &&
                    type.IsGenericType &&
                    type.GetGenericTypeDefinition() == typeof (IStore<>))
                {
                    var targetType = type.GetGenericArguments().First();

                    Type applierType;
                    if (typeof (IEventSourced).IsAssignableFrom(targetType))
                    {
                        applierType = typeof (IEventSourcedRepository<>).MakeGenericType(targetType);

                        return c => c.Resolve(applierType);
                    }
                }

                return null;
            });
        }
        /// <summary>
        /// Registers an open generic type to another open generic type, allowing, for example, IService&amp;T&amp; to be registered to resolve to Service&amp;T&amp;.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="variantsOf">The open generic interface that callers will attempt to resolve, e.g. typeof(IService&amp;T&amp;).</param>
        /// <param name="to">The open generic type to resolve, e.g. typeof(Service&amp;T&amp;).</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">
        /// Parameter 'variantsOf' is not an open generic type, e.g. typeof(IService&amp;T&amp;)
        /// or
        /// Parameter 'to' is not an open generic type, e.g. typeof(Service&amp;T&amp;)
        /// </exception>
        public static PocketContainer RegisterGeneric(this PocketContainer container, Type variantsOf, Type to)
        {
            if (!variantsOf.IsGenericTypeDefinition)
            {
                throw new ArgumentException("Parameter 'variantsOf' is not an open generic type, e.g. typeof(IService<>)");
            }

            if (!to.IsGenericTypeDefinition)
            {
                throw new ArgumentException("Parameter 'to' is not an open generic type, e.g. typeof(Service<>)");
            }

            return container.AddStrategy(t =>
            {
                if (t.IsGenericType && t.GetGenericTypeDefinition() == variantsOf)
                {
                    var closedGenericType = to.MakeGenericType(t.GetGenericArguments());

                    return c => c.Resolve(closedGenericType);
                }
                return null;
            });
        }
        internal static PocketContainer RegisterDefaultClockName(this PocketContainer container) =>
            container.AddStrategy(t =>
            {
                if (t == typeof (GetClockName))
                {
                    return c => new GetClockName(e => CommandScheduler.Clock.DefaultClockName);
                }

                return null;
            });
        internal static PocketContainer AddFallbackToDefaultClock(this PocketContainer container)
        {
            return container.AddStrategy(t =>
            {
                if (t == typeof (GetClockName))
                {
                    return c => new GetClockName(e => CommandScheduler.SqlCommandScheduler.DefaultClockName);
                }

                return null;
            });
        }