示例#1
0
        /// <summary>
        /// Gets a persistence handler for a given type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IPersistenceHandler GetPersistenceHandler(Type type)
        {
            if (type.IsGenericType)
            {
                type = type.GetGenericTypeDefinition();
            }

            IPersistenceHandler handler = persistenceHandlerPool.GetHandler(type, (t) =>
            {
                // 1. If a handler type is registered, use an instance of this handler
                var newHandler = this.configContainer.Resolve <IPersistenceHandler>();

                // 2. try to use an NDOPersistenceHandler
                if (newHandler == null)
                {
                    newHandler = new SqlPersistenceHandler(this.configContainer);
                }

                return(newHandler);
            });

            Mappings mappings = configContainer.Resolve <Mappings>();

            // The dataSet will be used as template to create a DataTable for the query results.
            handler.Initialize(mappings, type, ReleaseHandler);

            return(handler);
        }
示例#2
0
        /// <summary>
        /// Creates an object and resolves the parameters
        /// </summary>
        /// <param name="resolvingContainer"></param>
        /// <param name="overrides"></param>
        /// <returns></returns>
        protected virtual object CreateObject(INDOContainer resolvingContainer, ParameterOverride[] overrides)
        {
            List <object> parameters = new List <object>();

            var             registrations = resolvingContainer.Registrations;
            ConstructorInfo constructor   = null;

            foreach (var constr in tTo.GetConstructors().OrderBy(ci => ci.GetParameters().Length))
            {
                bool canResolve = true;
                parameters.Clear();

                foreach (var p in constr.GetParameters())
                {
                    if (p.ParameterType != typeof(INDOContainer) &&
                        !overrides.Any(ov => ov.Name == null && p.ParameterType.IsAssignableFrom(ov.Value.GetType()) || ov.Name == p.Name) &&
                        !registrations.Any(r => r.Key == p.ParameterType))
                    {
                        canResolve = false;
                        break;
                    }
                }

                if (canResolve)
                {
                    constructor = constr;
                    //isDefaultConstructor = constr.GetParameters().Length == 0;
                    break;
                }
            }


            if (constructor == null)
            {
                throw new NDOException(116, $"Cant find a resolvable constructor for class '{tFrom.FullName}'");
            }

            foreach (var p in constructor.GetParameters())
            {
                if (p.ParameterType == typeof(INDOContainer))
                {
                    parameters.Add(resolvingContainer);
                    continue;
                }
                var overrde = overrides.FirstOrDefault(ov => ov.Name == null && p.ParameterType.IsAssignableFrom(ov.Value.GetType()) || ov.Name == p.Name);
                if (overrde != null)
                {
                    parameters.Add(overrde.Value);
                }
                else
                {
                    parameters.Add(resolvingContainer.Resolve(p.ParameterType));
                }
            }

            return(constructor.Invoke(parameters.ToArray()));
        }
示例#3
0
        // <summary>
        // Determines, if the underlying type has a default constructor.
        // </summary>
        //protected bool IsDefaultConstructor => isDefaultConstructor;

        /// <inheritdoc/>
        public virtual IPersistenceCapable CreateObject(INDOContainer configContainer)
        {
            if (isDefaultConstructor)
            {
                return(CreateObject());
            }

            List <object> parameters = new List <object>();

            if (this.constructor == null)
            {
                var registrations = configContainer.Registrations;
                foreach (var constr in t.GetConstructors().OrderBy(c => c.GetParameters().Length))
                {
                    bool canResolve = true;
                    parameters.Clear();

                    foreach (var p in constr.GetParameters())
                    {
                        if (!registrations.Any(r => r.Key == p.ParameterType))
                        {
                            canResolve = false;
                            break;
                        }
                    }

                    if (canResolve)
                    {
                        this.constructor     = constr;
                        isDefaultConstructor = constr.GetParameters().Length == 0;
                        break;
                    }
                }
            }

            if (constructor == null)
            {
                throw new NDOException(116, $"Cant find a resolvable constructor for class '{t.FullName}'");
            }

            foreach (var p in this.constructor.GetParameters())
            {
                var o = configContainer.Resolve(p.ParameterType);
                parameters.Add(o);
            }

            return((IPersistenceCapable)this.constructor.Invoke(parameters.ToArray()));
        }