Exemplo n.º 1
0
        /// <summary>
        /// The get.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="name">The name.</param>
        /// <returns>
        /// Resolved Type Reference.
        /// </returns>
        public static object TryGet(Type serviceType, string name = null)
        {
            Type service = serviceType;

            if (Bindings.ContainsKey(service))
            {
                IBindingInfo binding = Resolve(serviceType, name);

                if (binding != null)
                {
                    try
                    {
                        object instance = binding.GetInstance();

                        if (instance != null)
                        {
                            return(instance);
                        }
                    }
                    catch
                    {
                        return(null);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <param name="dependencyInfo">The binding info.</param>
        /// <returns>
        /// Instance of Dependency Object.
        /// </returns>
        /// <author>Anwar</author>
        /// <date>11/10/2011</date>
        public object GetInstance(IBindingInfo dependencyInfo)
        {
            object          instance = null;
            HttpContextBase context  = contextFunc();

            if (context != null && context.Session != null)
            {
                Dictionary <string, object> instanceCache = context.Session[IocConstants.LifetimeManagerKey] as Dictionary <string, object>
                                                            ??
                                                            new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

                if (instanceCache.ContainsKey(dependencyInfo.UniqueID))
                {
                    instance = instanceCache[dependencyInfo.UniqueID];
                }
                else
                {
                    instance = dependencyInfo.Instance();

                    instanceCache.Add(dependencyInfo.UniqueID, instance);
                }

                context.Session[IocConstants.LifetimeManagerKey] = instanceCache;
            }

            return(instance);
        }
Exemplo n.º 3
0
        public ISchemaBuilder AddBinding(IBindingInfo binding)
        {
            if (binding == null)
            {
                throw new ArgumentNullException(nameof(binding));
            }

            if (!binding.IsValid())
            {
                // TODO : resources
                throw new ArgumentException(
                          "binding is not valid",
                          nameof(binding));
            }

            if (!_bindingCompiler.CanHandle(binding))
            {
                // TODO : resources
                throw new ArgumentException(
                          "cannot handle binding",
                          nameof(binding));
            }

            _bindingCompiler.AddBinding(binding);
            return(this);
        }
Exemplo n.º 4
0
        /// <summary>
        /// The get.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="name">The name.</param>
        /// <returns>
        /// Resolved Type Reference.
        /// </returns>
        public static object Get(Type serviceType, string name = null)
        {
            Type service = serviceType;

            if (!Bindings.ContainsKey(service))
            {
                throw new ActivationException(
                          string.Format("No registration for type {0} could be found.", service.FullName));
            }

            IBindingInfo binding = Resolve(service, name);

            if (binding == null)
            {
                throw new ActivationException(
                          string.Format("No registration for type {0} could be found.", service.FullName));
            }

            try
            {
                object instance = binding.GetInstance();

                if (instance == null)
                {
                    throw new ActivationException(
                              string.Format("The registered delegate for type {0} returned null.", service.FullName));
                }

                return(instance);
            }
            catch (Exception exception)
            {
                throw new ActivationException("Cannot Resolve type " + service.FullName, exception);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// The get.
        /// </summary>
        /// <typeparam name="TType">The type of the type.</typeparam>
        /// <param name="name">The name.</param>
        /// <returns>
        /// Resolved Type Reference.
        /// </returns>
        public static TType TryGet <TType>(string name = null)
        {
            Type service = typeof(TType);

            if (Bindings.ContainsKey(service))
            {
                IBindingInfo binding = Resolve <TType>(name);

                if (binding != null)
                {
                    try
                    {
                        object instance = binding.GetInstance();

                        if (instance != null)
                        {
                            return((TType)instance);
                        }
                    }
                    catch
                    {
                        return(default(TType));
                    }
                }
            }

            return(default(TType));
        }
Exemplo n.º 6
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        /// Removes the type from DI.
        /// </summary>
        /// <param name="service">
        /// The service.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// -------------------------------------------------------------------------------------------------
        public static void Remove(Type service, string name = null)
        {
            IBindingInfo existingBinding = Resolve(service, name);

            if (existingBinding != null)
            {
                existingBinding.ReleaseInstance();
                Bindings.Remove(service, existingBinding);
            }
        }
        public void ReleaseInstance(IBindingInfo dependencyInfo)
        {
            if (this.CachedInstance != null)
            {
                IDisposable disposable = this.CachedInstance as IDisposable;

                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Binds the specified type.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="name">The name.</param>
        /// <returns>
        /// Binding Info as a <see cref="IBindingInfo{T}"/>.
        /// </returns>
        public static IBindingInfo <object> Bind(Type service, string name = null)
        {
            IBindingInfo existingBinding = Resolve(service, name);

            if (existingBinding == null)
            {
                BindingInfo <object> binding = new BindingInfo <object>(service, name);
                Bindings.Add(service, binding);

                existingBinding = binding;
            }

            return((IBindingInfo <object>)existingBinding);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Binds the specified <typeparamref name="TType"/>.
        /// </summary>
        /// <typeparam name="TType">The type of the type.</typeparam>
        /// <param name="name">The name.</param>
        /// <returns>
        /// Binding Info as a <see cref="IBindingInfo{T}"/>.
        /// </returns>
        public static IBindingInfo <TType> Bind <TType>(string name = null)
        {
            Type service = typeof(TType);

            IBindingInfo existingBinding = Resolve <TType>(name);

            if (existingBinding == null)
            {
                BindingInfo <TType> binding = new BindingInfo <TType>(name);
                Bindings.Add(service, binding);

                existingBinding = binding;
            }

            return((IBindingInfo <TType>)existingBinding);
        }
Exemplo n.º 10
0
        public void AddBinding(IBindingInfo binding)
        {
            if (binding == null)
            {
                throw new ArgumentNullException(nameof(binding));
            }

            if (!CanHandle(binding))
            {
                throw new ArgumentException(
                          "The specified binding cannot be handled.",
                          nameof(binding));
            }

            _bindings.Add(binding);
        }
Exemplo n.º 11
0
        public static void BindInstance(Type serviceType, Type implmentedType, string name, ILifetime lifetime, int?order = null)
        {
            MethodInfo bindInstanceMethod      = MakeGenericBindMethod(serviceType);
            MethodInfo implementInstanceMethod = MakeGenericToMethod(serviceType, implmentedType);

            MethodCallExpression methodCallExpression = Expression.Call(bindInstanceMethod, new Expression[] { Expression.Constant(name, typeof(string)) });

            Func <IBindingInfo> binder  = Expression.Lambda <Func <IBindingInfo> >(methodCallExpression).Compile();
            IBindingInfo        binding = binder();

            MethodInfo           containsMethod         = MakeGenericContainsMethod(serviceType);
            MethodCallExpression containsCallExpression = Expression.Call(containsMethod, new Expression[] { Expression.Constant(name, typeof(string)) });
            Func <bool>          containsFunc           = Expression.Lambda <Func <bool> >(containsCallExpression).Compile();

            if (containsFunc())
            {
                if (order == null)
                {
                    OrderAttribute priorityAttribute = implmentedType.GetCustomAttribute <OrderAttribute>();

                    int priority = priorityAttribute != null ? priorityAttribute.Value : int.MaxValue;

                    if (binding.Order.HasValue && priority < binding.Order.Value)
                    {
                        return;
                    }

                    binding.Order = priority;
                }
                else
                {
                    binding.Order = order;
                }
            }

            MethodCallExpression implementCallExpression = Expression.Call(Expression.Constant(binding), implementInstanceMethod);
            Func <IBindingInfo>  implementer             = Expression.Lambda <Func <IBindingInfo> >(implementCallExpression).Compile();

            binding = implementer();
            binding.LifetimeManager = lifetime;
        }
Exemplo n.º 12
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <param name="dependencyInfo">
        /// Information describing the dependency.
        /// </param>
        /// <returns>
        /// Instance of Dependency Object.
        /// </returns>
        /// -------------------------------------------------------------------------------------------------
        public object GetInstance(IBindingInfo dependencyInfo)
        {
            if (Instances.Value.ContainsKey(dependencyInfo.Instance))
            {
                return(Instances.Value[dependencyInfo.Instance]);
            }

            lock (SyncLock.Value)
            {
                if (Instances.Value.ContainsKey(dependencyInfo.Instance))
                {
                    return(Instances.Value[dependencyInfo.Instance]);
                }

                object instance = dependencyInfo.Instance();

                Instances.Value.Add(dependencyInfo.Instance, instance);

                return(instance);
            }
        }
        /// <summary>
        /// Dispose the instance if exists.
        /// </summary>
        /// <param name="dependencyInfo">The binding info.</param>
        /// <author>Anwar</author>
        /// <date>11/9/2011</date>
        public void ReleaseInstance(IBindingInfo dependencyInfo)
        {
            HttpContextBase context = contextFunc();

            if (context != null)
            {
                object instance = context.Items[dependencyInfo.UniqueID];

                if (instance != null)
                {
                    IDisposable disposable = instance as IDisposable;

                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }

                    context.Items.Remove(dependencyInfo.UniqueID);
                }
            }
        }
Exemplo n.º 14
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        /// Dispose the instance if exists.
        /// </summary>
        /// <param name="dependencyInfo">
        /// Information describing the dependency.
        /// </param>
        /// -------------------------------------------------------------------------------------------------
        public void ReleaseInstance(IBindingInfo dependencyInfo)
        {
            lock (SyncLock.Value)
            {
                if (Instances.Value.ContainsKey(dependencyInfo.Instance))
                {
                    object instance = Instances.Value[dependencyInfo.Instance];

                    if (instance != null)
                    {
                        IDisposable disposable = instance as IDisposable;

                        if (disposable != null)
                        {
                            disposable.Dispose();
                        }
                    }

                    Instances.Value.Remove(dependencyInfo.Instance);
                }
            }
        }
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <param name="dependencyInfo">The binding info.</param>
        /// <returns>
        /// Instance of Dependency Object.
        /// </returns>
        /// <author>Anwar</author>
        /// <date>11/9/2011</date>
        public object GetInstance(IBindingInfo dependencyInfo)
        {
            object          instance = null;
            HttpContextBase context  = contextFunc();

            if (context != null)
            {
                instance = context.Items[dependencyInfo.UniqueID];

                if (instance == null)
                {
                    instance = dependencyInfo.Instance();

                    context.Items[dependencyInfo.UniqueID] = instance;
                }
                else
                {
                    instance = null;
                }
            }

            return(instance);
        }
Exemplo n.º 16
0
        public ISchemaBuilder AddBinding(IBindingInfo binding)
        {
            if (binding == null)
            {
                throw new ArgumentNullException(nameof(binding));
            }

            if (!binding.IsValid())
            {
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_Binding_Invalid,
                          nameof(binding));
            }

            if (!_bindingCompiler.CanHandle(binding))
            {
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_Binding_CannotBeHandled,
                          nameof(binding));
            }

            _bindingCompiler.AddBinding(binding);
            return(this);
        }
Exemplo n.º 17
0
 public bool CanHandle(IBindingInfo binding)
 {
     return(binding != null &&
            _supportedBindings.Contains(binding.GetType()));
 }
Exemplo n.º 18
0
        private static void TestTwoWayBindingInfo(IBindingInfo<ModelTestClass, ControlTestClass, string, string> binding, ModelTestClass model, ControlTestClass control, ref int modelCount, ref int controlCount)
        {
            Assert.Equal(BindingMode.TwoWay, binding.Direction);
            Assert.Equal("First", control.Text);
            Assert.Equal(1, controlCount);
            Assert.Equal(0, modelCount);

            model.Text = 2.ToText();
            Assert.Equal(1, modelCount);
            Assert.Equal(2, controlCount);
            Assert.Equal("Second", control.Text);

            binding.Model = null;
            model.Text = 3.ToText();
            Assert.Equal(2, modelCount);

            binding.Model = model;
            Assert.Equal("Third", control.Text);
            Assert.Equal(3, controlCount);

            control.Text = "Forth";
            Assert.Equal(4, controlCount);
            Assert.Equal(3, modelCount);
            Assert.Equal("Forth", model.Text);
        }
Exemplo n.º 19
0
        private static void TestOneWayToSourceBindingInfo(IBindingInfo<ModelTestClass, ControlTestClass, string, string> binding, ModelTestClass model, ControlTestClass control, int[] modelCounts, int[] controlCounts)
        {
            Assert.Equal(BindingMode.OneWayToSource, binding.Direction);
            Assert.Equal("First", model.Text);
            Assert.Equal(0, controlCounts[0]);
            Assert.Equal(1, modelCounts[0]);

            control.Text = 2.ToText();
            Assert.Equal(2, modelCounts[0]);
            Assert.Equal(1, controlCounts[0]);
            Assert.Equal("Second", model.Text);

            binding.Model = null;
            control.Text = 3.ToText();
            Assert.Equal(2, controlCounts[0]);

            binding.Model = model;
            Assert.Equal("Third", model.Text);
            Assert.Equal(3, modelCounts[0]);
            Assert.Equal(2, controlCounts[0]);

            model.Text = "Forth";
            Assert.Equal(4, modelCounts[0]);
            Assert.Equal(2, controlCounts[0]);
        }
Exemplo n.º 20
0
        public object GetInstance(IBindingInfo dependencyInfo)
        {
            object instance = dependencyInfo.Instance();

            return(instance);
        }
Exemplo n.º 21
0
 public object GetInstance(IBindingInfo dependencyInfo)
 {
     return(this.CachedInstance ?? (this.CachedInstance = dependencyInfo.Instance()));
 }
Exemplo n.º 22
0
 /// <summary>
 /// Dispose the instance if exists.
 /// </summary>
 /// <param name="dependencyInfo">The binding info.</param>
 /// <author>Anwar</author>
 /// <date>11/10/2011</date>
 public void ReleaseInstance(IBindingInfo dependencyInfo)
 {
     // Do Nothing
 }