Exemplo n.º 1
0
        private static IIocContainer SetContainer(IIocContainer value)
        {
            var config = value.GetValue("$AppSettings") as NameValueCollection
                         ?? System.Web.Configuration.WebConfigurationManager.AppSettings
                         ?? new NameValueCollection();

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

            if (!string.IsNullOrEmpty(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.AddService <IRedisProvider>(new RedisProvider(value));
            }

            if (!value.ContainsService <IUserFactory>(true))
            {
                value.AddService <IUserFactory>(value.GetService <IIdentityStore>());
            }

            HttpContext.Current.Application[ContainerName] = value;
            return(value);
        }
Exemplo n.º 2
0
    /// <summary>
    /// 指定筛选器,自动映射类型。
    /// </summary>
    /// <param name="container">服务容器。</param>
    /// <param name="mapFilter">依赖注入与控制反转的映射筛选器。</param>
    /// <param name="expectTypeHandler">找到预期类型时发生。</param>
    public static void AutoMap(this IIocContainer container, IMapFilter mapFilter, Action <Type> expectTypeHandler = null)
    {
        if (mapFilter == null)
        {
            throw new ArgumentNullException("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.AddService(expectType, actualType, mapFilter.IsSingletonMode(expectType, actualType));
                    if (hasHandler)
                    {
                        expectTypeHandler(expectType);
                    }
                    break;
                }
            }
        }
    }
Exemplo n.º 3
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>
    public static void AddService <TService>(this IIocContainer container, InstanceCreatorCallback callback, bool singletonMode = false, bool promote = false)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }

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

        container.AddService(typeof(TService), serviceInstance, promote);
    }
Exemplo n.º 5
0
    /// <summary>
    /// 将指定服务添加到服务容器中。
    /// </summary>
    /// <typeparam name="TService">要添加的服务类型。</typeparam>
    /// <param name="container">服务容器。</param>
    /// <param name="singletonMode">true,则启用单例模式;否则为 false。</param>
    /// <param name="promote">true,则将此请求提升到任何父服务容器;否则为 false。</param>
    /// <returns>返回服务容器。</returns>
    public static IIocContainer AddService <TService>(this IIocContainer container, bool singletonMode = false, bool promote = false)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }

        container.AddService(typeof(TService), singletonMode, promote);
        return(container);
    }
Exemplo n.º 6
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>
    public static void AddService <TService, TActual>(this IIocContainer container, bool singletonMode = false, bool promote = false)
        where TActual : TService
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }

        container.AddService(typeof(TService), typeof(TActual), singletonMode, promote);
    }