Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IxSingletonInstance"/> class.
        /// </summary>
        /// <param name="providerNode">Singleton provider.</param>
        /// <param name="parentInstance">Parent instance.</param>
        /// <param name="context">The resolve context.</param>
        /// <param name="frame">The resolution frame in the resolve sequence.</param>
        /// <param name="creatorTempLock">First temp lock for the creator of a new instance.</param>
        public IxSingletonInstance(
            IxSingletonProvider providerNode,
            IIxInstance parentInstance,
            IxHost.IxResolveContext context,
            [CanBeNull] IxResolveFrame frame,
            out IIxInstanceLock creatorTempLock)
            : base(providerNode, parentInstance, out creatorTempLock)
        {
            if (parentInstance == null)
            {
                throw new ArgumentNullException(nameof(parentInstance));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            Debug.Assert(ProviderNode.InstanceFactory != null, "IxSingletonProvider always have instance factory.");

            var newFrame = new IxResolveFrame(frame, this);

            SetObjectCreationTask(
                ProviderNode.InstanceFactory.Factory(
                    this,
                    parentInstance,
                    context,
                    newFrame));
        }
Exemplo n.º 2
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously

        /// <inheritdoc/>
        public override async ValueTask <IIxInstanceLock> GetInstance(
            IIxInstance parentInstance,
            IxIdentifier identifier,
            IxHost.IxResolveContext context,
            [CanBeNull] IxResolveFrame frame)
        {
            if (parentInstance == null)
            {
                throw new ArgumentNullException(nameof(parentInstance));
            }

            lock (Host.InstanceTreeSyncRoot)
            {
                IIxInstanceLock scopeLock;
                IxScopeInstance singleton;
                object          data = parentInstance.GetData(this);
                if (data == null)
                {
                    singleton = new IxScopeInstance(this, parentInstance, out scopeLock);
                    parentInstance.SetData(this, singleton);
                }
                else
                {
                    singleton = (IxScopeInstance)data;
                    scopeLock = new IxInstancePinLock(singleton);
                }

                return(scopeLock);
            }
        }
Exemplo n.º 3
0
 public override ValueTask <IIxInstanceLock> GetInstance(
     IIxInstance parentInstance,
     IxIdentifier identifier,
     IxHost.IxResolveContext context,
     [CanBeNull] IxResolveFrame frame)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 4
0
 /// <inheritdoc/>
 public override async ValueTask <IIxInstanceLock> GetInstance(
     IIxInstance parentInstance,
     IxIdentifier identifier,
     IxHost.IxResolveContext context,
     [CanBeNull] IxResolveFrame frame)
 {
     Critical.Assert(false, "Not supported.");
     return(null);
 }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public override async ValueTask <IIxInstanceLock> GetInstance(
            IIxInstance parentInstance,
            IxIdentifier identifier,
            IxHost.IxResolveContext context,
            [CanBeNull] IxResolveFrame frame)
        {
            if (parentInstance == null)
            {
                throw new ArgumentNullException(nameof(parentInstance));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            IIxInstanceLock     creatorLock = null;
            IxSingletonInstance instance;

            // Re-implement this with more advanced Half-Instantiation with loop detection.
            lock (Host.InstanceTreeSyncRoot)
            {
                instance = parentInstance.GetData(this) as IxSingletonInstance;
                if (instance == null)
                {
                    // TODO: Detect cycles.
                    Debug.Assert(InstanceFactory != null, "InstanceFactory != null");

                    instance = new IxSingletonInstance(this, parentInstance, context, frame, out creatorLock);

                    parentInstance.SetData(this, instance);
                }
            }

            try
            {
                await instance.ObjectCreationTask;
                return(creatorLock ?? new IxInstancePinLock(instance));
            }
            catch
            {
                if (creatorLock != null)
                {
                    lock (Host.InstanceTreeSyncRoot)
                    {
                        parentInstance.SetData(this, null);
                    }

                    creatorLock.Dispose();
                }

                throw;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Tries to get argument value for the provided identifier.
        /// </summary>
        /// <param name="identifier">Argument identifier.</param>
        /// <param name="context">The resolve context.</param>
        /// <returns>Argument instance lock or null, if no argument found for the provided identifier.</returns>
        public IIxInstanceLock TryGetInstance(IxIdentifier identifier, IxHost.IxResolveContext context)
        {
            IxHost.IxResolveContext curContext = context;
            while (curContext != null)
            {
                if (context.Arguments.TryGetValue(identifier, out var result))
                {
                    return(new IxInstancePinLock(new IxArgumentInstance(this, result)));
                }

                curContext = context.ParentContext;
            }

            // ReSharper disable once AssignNullToNotNullAttribute
            return(null);
        }
Exemplo n.º 7
0
 public abstract ValueTask <IIxInstanceLock> GetInstance(
     IIxInstance parentInstance,
     IxIdentifier identifier,
     IxHost.IxResolveContext context,
     [CanBeNull] IxResolveFrame frame);