Beispiel #1
0
        public override ResolveDelegate <BuilderContext> GetResolver(Type type, IPolicySet registration, ResolveDelegate <BuilderContext> seed)
        {
            // Select ConstructorInfo
            var selector  = GetOrDefault(registration);
            var selection = selector.Select(type, registration)
                            .FirstOrDefault();

            // Select constructor for the Type
            ConstructorInfo info;

            object[] resolvers = null;

            switch (selection)
            {
            case ConstructorInfo memberInfo:
                info = memberInfo;
                break;

            case MethodBase <ConstructorInfo> injectionMember:
                info      = injectionMember.MemberInfo(type);
                resolvers = injectionMember.Data;
                break;

            case Exception exception:
                return((ref BuilderContext c) =>
                {
                    if (null == c.Existing)
                    {
                        throw exception;
                    }

                    return c.Existing;
                });

            default:
                return((ref BuilderContext c) =>
                {
                    if (null == c.Existing)
                    {
                        throw new InvalidOperationException($"No public constructor is available for type {c.Type}.",
                                                            new InvalidRegistrationException());
                    }

                    return c.Existing;
                });
            }

            // Get lifetime manager
            var lifetimeManager = (LifetimeManager)registration.Get(typeof(LifetimeManager));

            return(lifetimeManager is PerResolveLifetimeManager
                ? GetPerResolveDelegate(info, resolvers)
                : GetResolverDelegate(info, resolvers));
        }
        public override IEnumerable <Expression> GetExpressions(Type type, IPolicySet registration)
        {
            // Select ConstructorInfo
            var selector  = GetOrDefault(registration);
            var selection = selector.Select(type, registration)
                            .FirstOrDefault();

            // Select constructor for the Type
            object[]                 resolvers = null;
            ConstructorInfo          info      = null;
            IEnumerable <Expression> parametersExpr;

            switch (selection)
            {
            case ConstructorInfo memberInfo:
                info           = memberInfo;
                parametersExpr = CreateParameterExpressions(info.GetParameters());
                break;

            case MethodBase <ConstructorInfo> injectionMember:
                info           = injectionMember.MemberInfo(type);
                resolvers      = injectionMember.Data;
                parametersExpr = CreateParameterExpressions(info.GetParameters(), resolvers);
                break;

            case Exception exception:
                return(new[] { Expression.IfThen(
                                   Expression.Equal(Expression.Constant(null), BuilderContextExpression.Existing),
                                   Expression.Throw(Expression.Constant(exception))) });

            default:
                return(NoConstructorExpr);
            }

            // Get lifetime manager
            var lifetimeManager = (LifetimeManager)registration.Get(typeof(LifetimeManager));

            return(lifetimeManager is PerResolveLifetimeManager
                ? new[] { GetResolverExpression(info, resolvers), SetPerBuildSingletonExpr }
                : new Expression[] { GetResolverExpression(info, resolvers) });
        }
 public override MemberSelector <PropertyInfo> GetOrDefault(IPolicySet?registration) =>
 registration?.Get <MemberSelector <PropertyInfo> >() ?? Defaults.SelectProperty;
Beispiel #4
0
 public override MemberSelector <FieldInfo> GetOrDefault(IPolicySet?registration) =>
 registration?.Get <MemberSelector <FieldInfo> >() ?? Defaults.SelectField;
 public override ISelect <FieldInfo> GetOrDefault(IPolicySet registration) =>
 registration.Get <ISelect <FieldInfo> >() ?? Defaults.FieldsSelector;
Beispiel #6
0
 public override MemberSelector <ConstructorInfo> GetOrDefault(IPolicySet registration) =>
 registration.Get <MemberSelector <ConstructorInfo> >() ??
 Defaults.SelectConstructor;
Beispiel #7
0
 public override MemberSelector <MethodInfo> GetOrDefault(IPolicySet registration) =>
 registration.Get <MemberSelector <MethodInfo> >() ?? Defaults.SelectMethod;
Beispiel #8
0
        private ResolveDelegate <PipelineContext> PipelineFromTypeFactory(ref HashKey key, UnityContainer container, IPolicySet set)
        {
            Debug.Assert(null != _registry);
            Debug.Assert(null != key.Type);

            LifetimeManager?manager = null;
            ResolveDelegate <PipelineContext>?pipeline = null;
            var typeFactory = (TypeFactoryDelegate)set.Get(typeof(TypeFactoryDelegate));

            // Add Pipeline to the Registry
            lock (_syncRegistry)
            {
                bool adding       = true;
                var  collisions   = 0;
                var  targetBucket = key.HashCode % _registry.Buckets.Length;
                for (var i = _registry.Buckets[targetBucket]; i >= 0; i = _registry.Entries[i].Next)
                {
                    ref var candidate = ref _registry.Entries[i];
                    if (candidate.Key != key)
                    {
                        collisions++;
                        continue;
                    }

                    // Pipeline already been created
                    if (null != candidate.Pipeline)
                    {
                        return(candidate.Pipeline);
                    }

                    // Lifetime Manager
                    manager = (LifetimeManager)set.Get(typeof(LifetimeManager)) ??
                              Context.TypeLifetimeManager.CreateLifetimePolicy();
                    manager.PipelineDelegate = (ResolveDelegate <PipelineContext>)SpinWait;

                    // Type has not been registered
                    if (null == candidate.Registration)
                    {
                        candidate.Pipeline = manager.Pipeline;
                    }

                    adding = false;
                    break;
                }

                if (adding)
                {
                    // Expand if required
                    if (_registry.RequireToGrow || CollisionsCutPoint < collisions)
                    {
                        _registry    = new Registry(_registry);
                        targetBucket = key.HashCode % _registry.Buckets.Length;
                    }

                    // Lifetime Manager
                    manager = (LifetimeManager)set.Get(typeof(LifetimeManager)) ??
                              Context.TypeLifetimeManager.CreateLifetimePolicy();
                    manager.PipelineDelegate = (ResolveDelegate <PipelineContext>)SpinWait;

                    // Create new entry
                    ref var entry = ref _registry.Entries[_registry.Count];
                    entry.Key      = key;
                    entry.Pipeline = manager.Pipeline;
                    entry.Next     = _registry.Buckets[targetBucket];
                    _registry.Buckets[targetBucket] = _registry.Count++;
                }
        private IBuilderPolicy this[Type type, string name, Type interfaceType]
        {
            get
            {
                var        collisions   = 0;
                var        hashCode     = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;
                var        targetBucket = hashCode % _registrations.Buckets.Length;
                IPolicySet registration = null;
                lock (_syncRoot)
                {
                    for (var i = _registrations.Buckets[targetBucket]; i >= 0; i = _registrations.Entries[i].Next)
                    {
                        if (_registrations.Entries[i].HashCode != hashCode ||
                            _registrations.Entries[i].Key != type)
                        {
                            collisions++;
                            continue;
                        }

                        var existing = _registrations.Entries[i].Value;
                        if (existing.RequireToGrow)
                        {
                            existing = existing is HashRegistry <string, IPolicySet> registry
                                     ? new HashRegistry <string, IPolicySet>(registry)
                                     : new HashRegistry <string, IPolicySet>(LinkedRegistry.ListToHashCutoverPoint * 2,
                                                                             (LinkedRegistry)existing);

                            _registrations.Entries[i].Value = existing;
                        }

                        registration = existing.GetOrAdd(name, () => CreateRegistration(type, name));
                        break;
                    }

                    if (null == registration)
                    {
                        if (_registrations.RequireToGrow || ListToHashCutoverPoint < collisions)
                        {
                            _registrations = new HashRegistry <Type, IRegistry <string, IPolicySet> >(_registrations);
                            targetBucket   = hashCode % _registrations.Buckets.Length;
                        }

                        registration = CreateRegistration(type, name);
                        _registrations.Entries[_registrations.Count].HashCode = hashCode;
                        _registrations.Entries[_registrations.Count].Next     = _registrations.Buckets[targetBucket];
                        _registrations.Entries[_registrations.Count].Key      = type;
                        _registrations.Entries[_registrations.Count].Value    = new LinkedRegistry(name, registration);
                        _registrations.Buckets[targetBucket] = _registrations.Count;
                        _registrations.Count++;
                    }
                }

                return(registration.Get(interfaceType));
            }
            set
            {
                var collisions   = 0;
                var hashCode     = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;
                var targetBucket = hashCode % _registrations.Buckets.Length;
                lock (_syncRoot)
                {
                    for (var i = _registrations.Buckets[targetBucket]; i >= 0; i = _registrations.Entries[i].Next)
                    {
                        if (_registrations.Entries[i].HashCode != hashCode ||
                            _registrations.Entries[i].Key != type)
                        {
                            collisions++;
                            continue;
                        }

                        var existing = _registrations.Entries[i].Value;
                        if (existing.RequireToGrow)
                        {
                            existing = existing is HashRegistry <string, IPolicySet> registry
                                     ? new HashRegistry <string, IPolicySet>(registry)
                                     : new HashRegistry <string, IPolicySet>(LinkedRegistry.ListToHashCutoverPoint * 2,
                                                                             (LinkedRegistry)existing);

                            _registrations.Entries[i].Value = existing;
                        }

                        existing.GetOrAdd(name, () => CreateRegistration(type, name)).Set(interfaceType, value);
                        return;
                    }

                    if (_registrations.RequireToGrow || ListToHashCutoverPoint < collisions)
                    {
                        _registrations = new HashRegistry <Type, IRegistry <string, IPolicySet> >(_registrations);
                        targetBucket   = hashCode % _registrations.Buckets.Length;
                    }

                    var registration = CreateRegistration(type, name);
                    registration.Set(interfaceType, value);

                    _registrations.Entries[_registrations.Count].HashCode = hashCode;
                    _registrations.Entries[_registrations.Count].Next     = _registrations.Buckets[targetBucket];
                    _registrations.Entries[_registrations.Count].Key      = type;
                    _registrations.Entries[_registrations.Count].Value    = new LinkedRegistry(name, registration);
                    _registrations.Buckets[targetBucket] = _registrations.Count;
                    _registrations.Count++;
                }
            }
        }
Beispiel #10
0
 public static T Get <T>(this IPolicySet policySet)
 {
     return((T)policySet.Get(typeof(T)));
 }
Beispiel #11
0
 public override ISelect <ConstructorInfo> GetOrDefault(IPolicySet registration) =>
 registration.Get <ISelect <ConstructorInfo> >() ?? Defaults.CtorSelector;
Beispiel #12
0
 public override ISelect <PropertyInfo> GetOrDefault(IPolicySet registration) =>
 registration.Get <ISelect <PropertyInfo> >() ?? Defaults.PropertiesSelector;