Beispiel #1
0
        /// <summary>
        /// Get instance.
        /// </summary>
        /// <param name="context">Information used to create/fetch instance.</param>
        /// <param name="instance">Instance that was loaded/created</param>
        /// <returns>
        /// Created/Existing instance.
        /// </returns>
        public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
        {
            var ctx = (IConcreteInstanceStrategyContext)context;

            instance = ctx.CreateInstance();
            return(InstanceResult.Created);
        }
        /// <summary>
        /// Gets the service as a singleton (created or from the storage)
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="instance">The instance.</param>
        /// <returns></returns>
        protected InstanceResult GetSingleton(IInstanceStrategyContext context, out object instance)
        {
            instance = context.CreateContext.SingletonStorage.Retreive(context.BuildPlan);
            if (instance != null)
                return InstanceResult.Loaded;

            instance = CreateInstance(context);
            context.CreateContext.SingletonStorage.Store(context.BuildPlan, instance);
            return InstanceResult.Created;
        }
        /// <summary>
        /// Gets the service as a singleton (created or from the storage)
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="instance">The instance.</param>
        /// <returns></returns>
        protected InstanceResult GetSingleton(IInstanceStrategyContext context, out object instance)
        {
            instance = context.CreateContext.SingletonStorage.Retrieve(context.BuildPlan);
            if (instance != null)
            {
                return(InstanceResult.Loaded);
            }

            instance = CreateInstance(context);
            context.CreateContext.SingletonStorage.Store(context.BuildPlan, instance);
            return(InstanceResult.Created);
        }
        /// <summary>
        /// Gets the scoped object (created or from storage).
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="instance">The instance.</param>
        /// <returns></returns>
        protected InstanceResult GetScoped(IInstanceStrategyContext context, out object instance)
        {
            if (context.CreateContext.ScopedStorage == null)
                throw new NotSupportedException("Scoped registrations need a scoped container.");

            instance = context.CreateContext.ScopedStorage.Retreive(context.BuildPlan);
            if (instance != null)
                return InstanceResult.Loaded;

            instance = CreateInstance(context);

            context.CreateContext.ScopedStorage.Store(context.BuildPlan, instance);
            return InstanceResult.Created;
        }
        /// <summary>
        /// Get instance.
        /// </summary>
        /// <param name="context">Information used to create/fetch instance.</param>
        /// <param name="instance">Instance that was loaded/created</param>
        /// <returns>
        /// If the instance was created or loaded from a storage.
        /// </returns>
        public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
        {
            switch (_lifetime)
            {
                case Lifetime.Transient:
                    instance = CreateInstance(context);
                    return InstanceResult.Created;
                case Lifetime.Singleton:
                    return GetSingleton(context, out instance);
                case Lifetime.Scoped:
                    return GetScoped(context, out instance);
            }

            throw new NotSupportedException(string.Format("Lifetime not supported: {0}.", _lifetime));
        }
        /// <summary>
        /// Get instance.
        /// </summary>
        /// <param name="context">Information used to create/fetch instance.</param>
        /// <param name="instance">Instance that was loaded/created</param>
        /// <returns>
        /// Created/Existing instance.
        /// </returns>
        public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
        {
            if (context.CreateContext.ScopedStorage == null)
                throw new InvalidOperationException("Class '" + _concrete.FullName +
                                                    "' is a scoped object and can therefore not be created from the parent container.");

            instance = context.CreateContext.ScopedStorage.Retreive(context.BuildPlan);
            if (instance != null)
                return InstanceResult.Loaded;

            var ctx = (IConcreteInstanceStrategyContext)context;
            instance = ctx.CreateInstance();

            context.CreateContext.ScopedStorage.Store(context.BuildPlan, instance);
            return InstanceResult.Created;
        }
        /// <summary>
        /// Get instance.
        /// </summary>
        /// <param name="context">Information used to create/fetch instance.</param>
        /// <param name="instance">Instance that was loaded/created</param>
        /// <returns>
        /// Created/Existing instance.
        /// </returns>
        public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
        {
            if (context.CreateContext.ScopedStorage == null)
                throw new InvalidOperationException("Class '" + _concrete.FullName +
                                                    "' is a scoped concrete and can therefore not be created from the parent container. Classes which has been specified as scoped will most likely break if resolved and stored in the singleton scope. Use 'container.CreateChildContainer()' to create a scope.");

            instance = context.CreateContext.ScopedStorage.Retreive(context.BuildPlan);
            if (instance != null)
                return InstanceResult.Loaded;

            var ctx = (IConcreteInstanceStrategyContext)context;
            instance = ctx.CreateInstance();

            context.CreateContext.ScopedStorage.Store(context.BuildPlan, instance);
            return InstanceResult.Created;
        }
        /// <summary>
        /// Get instance.
        /// </summary>
        /// <param name="context">Information used to create/fetch instance.</param>
        /// <param name="instance">Instance that was loaded/created</param>
        /// <returns>
        /// If the instance was created or loaded from a storage.
        /// </returns>
        public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
        {
            switch (_lifetime)
            {
            case Lifetime.Transient:
                instance = CreateInstance(context);
                return(InstanceResult.Created);

            case Lifetime.Singleton:
                return(GetSingleton(context, out instance));

            case Lifetime.Scoped:
                return(GetScoped(context, out instance));
            }

            throw new NotSupportedException(string.Format("Lifetime not supported: {0}.", _lifetime));
        }
        /// <summary>
        /// Gets the scoped object (created or from storage).
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="instance">The instance.</param>
        /// <returns></returns>
        protected InstanceResult GetScoped(IInstanceStrategyContext context, out object instance)
        {
            if (context.CreateContext.ScopedStorage == null)
            {
                throw new NotSupportedException("Scoped registrations need a scoped container. Requested service: " + context.BuildPlan.DisplayName);
            }

            instance = context.CreateContext.ScopedStorage.Retrieve(context.BuildPlan);
            if (instance != null)
            {
                return(InstanceResult.Loaded);
            }

            instance = CreateInstance(context);

            context.CreateContext.ScopedStorage.Store(context.BuildPlan, instance);
            return(InstanceResult.Created);
        }
        /// <summary>
        /// Get instance.
        /// </summary>
        /// <param name="context">Information used to create/fetch instance.</param>
        /// <param name="instance">Instance that was loaded/created</param>
        /// <returns>
        /// If the instance was created or loaded from a storage.
        /// </returns>
        public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
        {
            instance = context.CreateContext.SingletonStorage.Retrieve(context.BuildPlan);
            if (instance != null)
                return InstanceResult.Loaded;

            lock (context.CreateContext.SingletonStorage)
            {
                instance = context.CreateContext.SingletonStorage.Retrieve(context.BuildPlan);
                if (instance != null)
                    return InstanceResult.Loaded;

                var ctx = (IConcreteInstanceStrategyContext)context;
                instance = ctx.CreateInstance();
                context.CreateContext.SingletonStorage.Store(context.BuildPlan, instance);
            }

            return InstanceResult.Created;
        }
        /// <summary>
        /// Get instance.
        /// </summary>
        /// <param name="context">Information used to create/fetch instance.</param>
        /// <param name="instance">Instance that was loaded/created</param>
        /// <returns>
        /// Created/Existing instance.
        /// </returns>
        public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
        {
            if (context.CreateContext.ScopedStorage == null)
            {
                throw new InvalidOperationException("Class '" + _concrete.FullName +
                                                    "' is a scoped concrete and can therefore not be created from the parent container. Classes which has been specified as scoped will most likely break if resolved and stored in the singleton scope. Use 'container.CreateChildContainer()' to create a scope.");
            }

            instance = context.CreateContext.ScopedStorage.Retrieve(context.BuildPlan);
            if (instance != null)
            {
                return(InstanceResult.Loaded);
            }

            var ctx = (IConcreteInstanceStrategyContext)context;

            instance = ctx.CreateInstance();

            context.CreateContext.ScopedStorage.Store(context.BuildPlan, instance);
            return(InstanceResult.Created);
        }
        /// <summary>
        /// Get instance.
        /// </summary>
        /// <param name="context">Information used to create/fetch instance.</param>
        /// <param name="instance">Instance that was loaded/created</param>
        /// <returns>
        /// If the instance was created or loaded from a storage.
        /// </returns>
        public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
        {
            instance = context.CreateContext.SingletonStorage.Retreive(context.BuildPlan);
            if (instance != null)
            {
                return(InstanceResult.Loaded);
            }

            lock (context.CreateContext.SingletonStorage)
            {
                instance = context.CreateContext.SingletonStorage.Retreive(context.BuildPlan);
                if (instance != null)
                {
                    return(InstanceResult.Loaded);
                }

                var ctx = (IConcreteInstanceStrategyContext)context;
                instance = ctx.CreateInstance();
                context.CreateContext.SingletonStorage.Store(context.BuildPlan, instance);
            }

            return(InstanceResult.Created);
        }
Beispiel #13
0
 /// <summary>
 /// Create a new instance.
 /// </summary>
 /// <param name="context">Context</param>
 /// <returns>Created instance</returns>
 protected override object CreateInstance(IInstanceStrategyContext context)
 {
     return(_factory(context.CreateContext.Container));
 }
 /// <summary>
 /// Get instance.
 /// </summary>
 /// <param name="context">Information used to create/fetch instance.</param>
 /// <param name="instance">Instance that was loaded/created</param>
 /// <returns>
 /// If the instance was created or loaded from a storage.
 /// </returns>
 public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
 {
     instance = _instance;
     return InstanceResult.Loaded;
 }
 /// <summary>
 /// Get instance.
 /// </summary>
 /// <param name="context">Information used to create/fetch instance.</param>
 /// <param name="instance">Instance that was loaded/created</param>
 /// <returns>
 /// Created/Existing instance.
 /// </returns>
 public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
 {
     var ctx = (IConcreteInstanceStrategyContext)context;
     instance = ctx.CreateInstance();
     return InstanceResult.Created;
 }
 /// <summary>
 /// Create a new instance.
 /// </summary>
 /// <param name="context">Context</param>
 /// <returns>Created instance</returns>
 protected abstract object CreateInstance(IInstanceStrategyContext context);
 /// <summary>
 /// Create a new instance.
 /// </summary>
 /// <param name="context">Context</param>
 /// <returns>Created instance</returns>
 protected abstract object CreateInstance(IInstanceStrategyContext context);
Beispiel #18
0
 /// <summary>
 /// Get instance.
 /// </summary>
 /// <param name="context">Information used to create/fetch instance.</param>
 /// <param name="instance">Instance that was loaded/created</param>
 /// <returns>
 /// If the instance was created or loaded from a storage.
 /// </returns>
 public InstanceResult GetInstance(IInstanceStrategyContext context, out object instance)
 {
     instance = _instance;
     return(InstanceResult.Loaded);
 }