示例#1
0
        /// <summary>
        /// Registers an instance of <see cref="Searcher"/> or <see cref="PostProcessor"/>
        /// without settings, using its type name.
        /// </summary>
        /// <param name="context">The search context to alter.</param>
        /// <param name="serviceName">Name of the service to register (case insensitive).</param>
        /// <param name="settings">The optional settings instance for the service.</param>
        /// <typeparam name="T">A type that inherits from <see cref="SearchContext"/>
        /// or <see cref="PostProcessor"/>.</typeparam>
        /// <returns>A <see cref="SearchContext"/> with the given service added.</returns>
        public static SearchContext With(this SearchContext context, string serviceName,
                                         object settings = null)
        {
            if (serviceName == null)
            {
                throw new ArgumentNullException(nameof(serviceName), $"{nameof(serviceName)} cannot be null");
            }
            if (serviceName.Trim() == string.Empty)
            {
                throw new ArgumentException($"{nameof(serviceName)} cannot be empty or contain only space", nameof(serviceName));
            }

            var type = context.GetType().GetTypeInfo().Assembly.GetTypes().Where(
                @this => @this.Name.Equals(serviceName, StringComparison.OrdinalIgnoreCase))
                       .SingleOrDefault();

            if (type == null)
            {
                throw new NotSupportedException($"{serviceName} service not found");
            }
            if (!type.IsSearcher() && !type.IsPostProcessor())
            {
                throw new NotSupportedException(
                          $"T must inherit from {nameof(Searcher)} or {nameof(PostProcessor)}");
            }

            var service = Activator.CreateInstance(type, settings);

            return(new SearchContext(
                       context.Services.Add(service).Cast <Service>(),
                       new ContextSettings {
                MaximumResults = context.Settings.MaximumResults
            }));
        }
        /// <summary>Registers an instance of <c>Searcher</c> or <c>PostProcessor</c> without settings,
        /// using its type name.</summary>
        public static SearchContext With(this SearchContext context, string serviceName,
                                         object settings = null)
        {
            Guard.AgainstNull(nameof(context), context);
            Guard.AgainstNull(nameof(serviceName), serviceName);
            Guard.AgainstEmptyWhiteSpace(nameof(serviceName), serviceName);

            var type = context.GetType().GetTypeInfo().Assembly.GetTypes().Where(
                @this => @this.Name.Equals(serviceName, StringComparison.OrdinalIgnoreCase))
                       .SingleOrDefault();

            if (type == null)
            {
                throw new NotSupportedException($"{serviceName} service not found");
            }

            var service = Activator.CreateInstance(type, settings);

            Guard.AgainstSubclassExcept <Service>(nameof(serviceName), service);
            return(new SearchContext(
                       context.Services.Add(service),
                       context.Settings.Clone()));
        }