Exemple #1
0
        private static IIocContainer SetContainer(IIocContainer value)
        {
            var config = value.Get("$AppSettings") as NameValueCollection
                         ?? AppSettings
                         ?? new NameValueCollection();

            var redisAddress = config.Get("redis.address");

            if (!string.IsNullOrWhiteSpace(redisAddress))
            {
                var sp = redisAddress.Split(':');
                if (sp.Length != 2)
                {
                    throw new ArgumentException("非法的 Redis 的连接地址 {0}。".Fmt(redisAddress));
                }
                Aoite.Redis.RedisManager.DefaultAddress = new Aoite.Net.SocketInfo(sp[0], int.Parse(sp[1]));
            }
            Aoite.Redis.RedisManager.DefaultPassword = config.Get("redis.password");

            if (config.Get <bool>("redis.enabled"))
            {
                value.Add <IRedisProvider>(new RedisProvider(value));
            }

            if (!value.Contains <IUserFactory>(true))
            {
                value.Add <IUserFactory>(value.Get <IIdentityStore>());
            }

            _WebContainer = value;
            return(value);
        }
Exemple #2
0
    /// <summary>
    /// 将指定服务添加到服务容器中。
    /// </summary>
    /// <typeparam name="TService">要添加的服务类型。</typeparam>
    /// <param name="container">服务容器。</param>
    /// <param name="callback">用于创建服务的回调对象。这允许将服务声明为可用,但将对象的创建延迟到请求该服务之后。</param>
    /// <param name="singletonMode">true,则启用单例模式;否则为 false。</param>
    /// <param name="promote">true,则将此请求提升到任何父服务容器;否则为 false。</param>
    /// <returns>服务容器。</returns>
    public static IIocContainer Add <TService>(this IIocContainer container, InstanceCreatorCallback callback, bool singletonMode = false, bool promote = false)
    {
        if (container == null)
        {
            throw new ArgumentNullException(nameof(container));
        }

        container.Add(typeof(TService), callback, singletonMode, promote);
        return(container);
    }
Exemple #3
0
    /// <summary>
    /// 将指定服务添加到服务容器中。
    /// </summary>
    /// <typeparam name="TService">要添加的服务类型。</typeparam>
    /// <param name="container">服务容器。</param>
    /// <param name="serviceInstance">要添加的服务的实例。 此对象必须实现 <typeparamref name="TService"/> 参数所指示的类型或从其继承。</param>
    /// <param name="promote">true,则将此请求提升到任何父服务容器;否则为 false。</param>
    /// <returns>服务容器。</returns>
    public static IIocContainer Add <TService>(this IIocContainer container, TService serviceInstance, bool promote = false)
    {
        if (container == null)
        {
            throw new ArgumentNullException(nameof(container));
        }

        container.Add(typeof(TService), serviceInstance, promote);
        return(container);
    }
Exemple #4
0
    /// <summary>
    /// 将指定的参数名和参数值添加到服务容器中,并绑定到关联的服务类型的构造函数。
    /// </summary>
    /// <typeparam name="TService">要添加的服务类型。</typeparam>
    /// <param name="container">服务容器。</param>
    /// <param name="name">参数名称。</param>
    /// <param name="value">参数值。</param>
    /// <param name="promote">true,则将此请求提升到任何父服务容器;否则为 false。</param>
    /// <returns>服务容器。</returns>
    public static IIocContainer Add <TService>(this IIocContainer container, string name, object value, bool promote = false)
    {
        if (container == null)
        {
            throw new ArgumentNullException(nameof(container));
        }

        container.Add(typeof(TService), name, value, promote);
        return(container);
    }
Exemple #5
0
    /// <summary>
    /// 将指定服务添加到服务容器中。
    /// </summary>
    /// <typeparam name="TService">要添加的服务类型。</typeparam>
    /// <typeparam name="TActual">实际的服务类型。</typeparam>
    /// <param name="container">服务容器。</param>
    /// <param name="singletonMode">true,则启用单例模式;否则为 false。</param>
    /// <param name="promote">true,则将此请求提升到任何父服务容器;否则为 false。</param>
    /// <returns>服务容器。</returns>
    public static IIocContainer Add <TService, TActual>(this IIocContainer container, bool singletonMode = false, bool promote = false)
        where TActual : TService
    {
        if (container == null)
        {
            throw new ArgumentNullException(nameof(container));
        }

        container.Add(typeof(TService), typeof(TActual), singletonMode, promote);
        return(container);
    }
Exemple #6
0
    /// <summary>
    /// 指定筛选器,自动映射类型。
    /// </summary>
    /// <param name="container">服务容器。</param>
    /// <param name="mapFilter">依赖注入与控制反转的映射筛选器。</param>
    /// <param name="expectTypeHandler">找到预期类型时发生。</param>
    /// <returns>服务容器。</returns>
    public static IIocContainer AutoMap(this IIocContainer container, IMapFilter mapFilter, Action <Type> expectTypeHandler = null)
    {
        if (container == null)
        {
            throw new ArgumentNullException(nameof(container));
        }
        if (mapFilter == null)
        {
            throw new ArgumentNullException(nameof(mapFilter));
        }

        var allTypes   = ObjectFactory.AllTypes;
        var hasHandler = expectTypeHandler != null;

        foreach (var item in allTypes)
        {
            foreach (var expectType in item.Value)
            {
                var ns = expectType.Namespace;
                if (mapFilter.IsExpectType(expectType))
                {
                    var actualType = mapFilter.FindActualType(allTypes, expectType);
                    if (actualType == null)
                    {
                        throw new Exception("无法找到预期定义类型“" + expectType.AssemblyQualifiedName + "”的实际映射类型。");
                    }
                    container.Add(expectType, actualType, mapFilter.IsSingletonMode(expectType, actualType));
                    if (hasHandler)
                    {
                        expectTypeHandler(expectType);
                    }
                    break;
                }
            }
        }
        return(container);
    }