Example #1
0
        protected virtual ICollection <InheritanceMappingAttribute> GetInheritanceMappingAttributes(
            Type type,
            AttributedMetaModel model)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            return((InheritanceMappingAttribute[])type.GetCustomAttributes(typeof(InheritanceMappingAttribute), true));
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="metaType">The parent meta type.</param>
        /// <param name="mi">The method info.</param>
        public AttributedMetaFunction(AttributedMetaModel model, MethodInfo mi) {
            this.model = model;
            this.methodInfo = mi;
            this.rowTypes = _emptyTypes;

            this.functionAttrib = Attribute.GetCustomAttribute(mi, typeof(FunctionAttribute), false) as FunctionAttribute;
            System.Diagnostics.Debug.Assert(functionAttrib != null);

            // Gather up all mapped results
            ResultTypeAttribute[] attrs = (ResultTypeAttribute[])Attribute.GetCustomAttributes(mi, typeof(ResultTypeAttribute));
            if (attrs.Length == 0 && mi.ReturnType == typeof(IMultipleResults)) {
                throw Error.NoResultTypesDeclaredForFunction(mi.Name);
            }
            else if (attrs.Length > 1 && mi.ReturnType != typeof(IMultipleResults)) {
                throw Error.TooManyResultTypesDeclaredForFunction(mi.Name);
            }
            else if (attrs.Length <= 1 && mi.ReturnType.IsGenericType &&
                     (mi.ReturnType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||
                      mi.ReturnType.GetGenericTypeDefinition() == typeof(ISingleResult<>) ||
                      mi.ReturnType.GetGenericTypeDefinition() == typeof(IQueryable<>))) {
                Type elementType = TypeSystem.GetElementType(mi.ReturnType);
                this.rowTypes = new List<MetaType>(1) { this.GetMetaType(elementType) }.AsReadOnly();
            }
            else if (attrs.Length > 0) {
                List<MetaType> rowTypes = new List<MetaType>();
                foreach (ResultTypeAttribute rat in attrs) {
                    Type type = rat.Type;
                    MetaType mt = this.GetMetaType(type);
                    // Only add unique meta types
                    if (!rowTypes.Contains(mt)) {
                        rowTypes.Add(mt);
                    } 
                }
                this.rowTypes = rowTypes.AsReadOnly();
            }
            else {
                this.returnParameter = new AttributedMetaParameter(this.methodInfo.ReturnParameter);
            }

            // gather up all meta parameter
            ParameterInfo[] pis = mi.GetParameters();
            if (pis.Length > 0) {
                List<MetaParameter> mps = new List<MetaParameter>(pis.Length);
                for (int i = 0, n = pis.Length; i < n; i++) {
                    AttributedMetaParameter metaParam = new AttributedMetaParameter(pis[i]);
                    mps.Add(metaParam);
                }
                this.parameters = mps.AsReadOnly();
            }
            else {
                this.parameters = _emptyParameters;
            }
        }
        internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type)
            : base(model, table, type, null) {

            // check for inheritance and create all other types
            InheritanceMappingAttribute[] inheritanceInfo = (InheritanceMappingAttribute[])type.GetCustomAttributes(typeof(InheritanceMappingAttribute), true);
            if (inheritanceInfo.Length > 0) {
                if (this.Discriminator == null) {
                    throw Error.NoDiscriminatorFound(type);
                }
                if (!MappingSystem.IsSupportedDiscriminatorType(this.Discriminator.Type)) {
                    throw Error.DiscriminatorClrTypeNotSupported(this.Discriminator.DeclaringType.Name, this.Discriminator.Name, this.Discriminator.Type);
                }
                this.types = new Dictionary<Type, MetaType>();
                this.types.Add(type, this); // add self
                this.codeMap = new Dictionary<object, MetaType>();

                // initialize inheritance types
                foreach (InheritanceMappingAttribute attr in inheritanceInfo) {
                    if (!type.IsAssignableFrom(attr.Type)) {
                        throw Error.InheritanceTypeDoesNotDeriveFromRoot(attr.Type, type);
                    }
                    if (attr.Type.IsAbstract) {
                        throw Error.AbstractClassAssignInheritanceDiscriminator(attr.Type);
                    }
                    AttributedMetaType mt = this.CreateInheritedType(type, attr.Type);
                    if (attr.Code == null) {
                        throw Error.InheritanceCodeMayNotBeNull();
                    }
                    if (mt.inheritanceCode != null) {
                        throw Error.InheritanceTypeHasMultipleDiscriminators(attr.Type);
                    }
                    object codeValue = DBConvert.ChangeType(attr.Code, this.Discriminator.Type);                
                    foreach (object d in codeMap.Keys) {
                        // if the keys are equal, or if they are both strings containing only spaces
                        // they are considered equal
                        if ((codeValue.GetType() == typeof(string) && ((string)codeValue).Trim().Length == 0 &&
                            d.GetType() == typeof(string) && ((string)d).Trim().Length == 0) ||
                            object.Equals(d, codeValue)) {
                            throw Error.InheritanceCodeUsedForMultipleTypes(codeValue);
                        }
                    }
                    mt.inheritanceCode = codeValue;
                    this.codeMap.Add(codeValue, mt);
                    if (attr.IsDefault) {
                        if (this.inheritanceDefault != null) {
                            throw Error.InheritanceTypeHasMultipleDefaults(type);
                        }
                        this.inheritanceDefault = mt;
                    }
                }

                if (this.inheritanceDefault == null) {
                    throw Error.InheritanceHierarchyDoesNotDefineDefault(type);
                }
            }

            if (this.types != null) {
                this.inheritanceTypes = this.types.Values.ToList().AsReadOnly();
            }
            else {
                this.inheritanceTypes = new MetaType[] { this }.ToList().AsReadOnly();
            }
            this.Validate();
        }
 internal AttributedMetaTable(AttributedMetaModel model, TableAttribute attr, Type rowType) {
     this.model = model;
     this.tableName = string.IsNullOrEmpty(attr.Name) ? rowType.Name : attr.Name;
     this.rowType = new AttributedRootType(model, this, rowType);
 }
Example #5
0
        internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type)
            : base(model, table, type, null)
        {
            // check for inheritance and create all other types
            InheritanceMappingAttribute[] inheritanceInfo = (InheritanceMappingAttribute[])type.GetCustomAttributes(typeof(InheritanceMappingAttribute), true);
            if (inheritanceInfo.Length > 0)
            {
                if (this.Discriminator == null)
                {
                    throw Error.NoDiscriminatorFound(type);
                }
                if (!MappingSystem.IsSupportedDiscriminatorType(this.Discriminator.Type))
                {
                    throw Error.DiscriminatorClrTypeNotSupported(this.Discriminator.DeclaringType.Name, this.Discriminator.Name, this.Discriminator.Type);
                }
                this.types = new Dictionary <Type, MetaType>();
                this.types.Add(type, this);                 // add self
                this.codeMap = new Dictionary <object, MetaType>();

                // initialize inheritance types
                foreach (InheritanceMappingAttribute attr in inheritanceInfo)
                {
                    if (!type.IsAssignableFrom(attr.Type))
                    {
                        throw Error.InheritanceTypeDoesNotDeriveFromRoot(attr.Type, type);
                    }
                    if (attr.Type.IsAbstract)
                    {
                        throw Error.AbstractClassAssignInheritanceDiscriminator(attr.Type);
                    }
                    AttributedMetaType mt = this.CreateInheritedType(type, attr.Type);
                    if (attr.Code == null)
                    {
                        throw Error.InheritanceCodeMayNotBeNull();
                    }
                    if (mt.inheritanceCode != null)
                    {
                        throw Error.InheritanceTypeHasMultipleDiscriminators(attr.Type);
                    }
                    object codeValue = DBConvert.ChangeType(attr.Code, this.Discriminator.Type);
                    foreach (object d in codeMap.Keys)
                    {
                        // if the keys are equal, or if they are both strings containing only spaces
                        // they are considered equal
                        if ((codeValue.GetType() == typeof(string) && ((string)codeValue).Trim().Length == 0 &&
                             d.GetType() == typeof(string) && ((string)d).Trim().Length == 0) ||
                            object.Equals(d, codeValue))
                        {
                            throw Error.InheritanceCodeUsedForMultipleTypes(codeValue);
                        }
                    }
                    mt.inheritanceCode = codeValue;
                    this.codeMap.Add(codeValue, mt);
                    if (attr.IsDefault)
                    {
                        if (this.inheritanceDefault != null)
                        {
                            throw Error.InheritanceTypeHasMultipleDefaults(type);
                        }
                        this.inheritanceDefault = mt;
                    }
                }

                if (this.inheritanceDefault == null)
                {
                    throw Error.InheritanceHierarchyDoesNotDefineDefault(type);
                }
            }

            if (this.types != null)
            {
                this.inheritanceTypes = this.types.Values.ToList().AsReadOnly();
            }
            else
            {
                this.inheritanceTypes = new MetaType[] { this }.ToList().AsReadOnly();
            }
            this.Validate();
        }
 internal AttributedMetaTable(AttributedMetaModel model, TableAttribute attr, Type rowType)
 {
     this.model     = model;
     this.tableName = string.IsNullOrEmpty(attr.Name) ? rowType.Name : attr.Name;
     this.rowType   = new AttributedRootType(model, this, rowType);
 }
 protected override MetaModel CreateModel(Type dataContextType)
 {
     var metaModel = new AttributedMetaModel(dataContextType, this);
     return metaModel;
 }
Example #8
0
        internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type)
            : base(model, table, type, null)
        {
            // check for inheritance and create all other types
            var inheritanceAttributes = GetInheritanceMappingAttributes(type, model);

            if (inheritanceAttributes.Count > 0)
            {
                if (Discriminator == null)
                {
                    throw Error.NoDiscriminatorFound(type);
                }
                if (!MappingSystem.IsSupportedDiscriminatorType(Discriminator.Type))
                {
                    throw Error.DiscriminatorClrTypeNotSupported(
                              Discriminator.DeclaringType.Name,
                              Discriminator.Name,
                              Discriminator.Type);
                }

                types = new Dictionary <Type, MetaType>();
                types.Add(type, this);                 // add self
                var codeMap = new Dictionary <object, MetaType>();

                // initialize inheritance types
                foreach (var inheritanceAttribute in inheritanceAttributes)
                {
                    if (!type.IsAssignableFrom(inheritanceAttribute.Type))
                    {
                        throw Error.InheritanceTypeDoesNotDeriveFromRoot(inheritanceAttribute.Type, type);
                    }
                    if (inheritanceAttribute.Type.IsAbstract)
                    {
                        throw Error.AbstractClassAssignInheritanceDiscriminator(inheritanceAttribute.Type);
                    }

                    var inheritedType = CreateInheritedType(type, inheritanceAttribute.Type);
                    if (inheritanceAttribute.Code == null)
                    {
                        throw Error.InheritanceCodeMayNotBeNull();
                    }
                    if (inheritedType.inheritanceCode != null)
                    {
                        throw Error.InheritanceTypeHasMultipleDiscriminators(inheritanceAttribute.Type);
                    }

                    var codeValue = DBConvert.ChangeType(inheritanceAttribute.Code, Discriminator.Type);
                    foreach (var codeMapKey in codeMap.Keys)
                    {
                        // if the keys are equal, or if they are both strings containing only spaces
                        // they are considered equal
                        if ((codeValue is string &&
                             ((string)codeValue).Trim().Length == 0 &&
                             codeMapKey is string &&
                             ((string)codeMapKey).Trim().Length == 0) ||
                            Equals(codeMapKey, codeValue))
                        {
                            throw Error.InheritanceCodeUsedForMultipleTypes(codeValue);
                        }
                    }
                    inheritedType.inheritanceCode = codeValue;
                    codeMap.Add(codeValue, inheritedType);

                    if (inheritanceAttribute.IsDefault)
                    {
                        if (inheritanceDefault != null)
                        {
                            throw Error.InheritanceTypeHasMultipleDefaults(type);
                        }
                        inheritanceDefault = inheritedType;
                    }
                }

                if (inheritanceDefault == null)
                {
                    throw Error.InheritanceHierarchyDoesNotDefineDefault(type);
                }
            }

            inheritanceTypes = types == null ?
                               new MetaType[]
            {
                this
            }
            .ToList()
            .AsReadOnly() :
            types.Values.ToList().AsReadOnly();

            Validate();
        }
Example #9
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="metaType">The parent meta type.</param>
        /// <param name="mi">The method info.</param>
        public AttributedMetaFunction(AttributedMetaModel model, MethodInfo mi)
        {
            this.model      = model;
            this.methodInfo = mi;
            this.rowTypes   = _emptyTypes;

            this.functionAttrib = Attribute.GetCustomAttribute(mi, typeof(FunctionAttribute), false) as FunctionAttribute;
            System.Diagnostics.Debug.Assert(functionAttrib != null);

            // Gather up all mapped results
            ResultTypeAttribute[] attrs = (ResultTypeAttribute[])Attribute.GetCustomAttributes(mi, typeof(ResultTypeAttribute));
            if (attrs.Length == 0 && mi.ReturnType == typeof(IMultipleResults))
            {
                throw Error.NoResultTypesDeclaredForFunction(mi.Name);
            }
            else if (attrs.Length > 1 && mi.ReturnType != typeof(IMultipleResults))
            {
                throw Error.TooManyResultTypesDeclaredForFunction(mi.Name);
            }
            else if (attrs.Length <= 1 && mi.ReturnType.IsGenericType &&
                     (mi.ReturnType.GetGenericTypeDefinition() == typeof(IEnumerable <>) ||
                      mi.ReturnType.GetGenericTypeDefinition() == typeof(ISingleResult <>) ||
                      mi.ReturnType.GetGenericTypeDefinition() == typeof(IQueryable <>)))
            {
                Type elementType = TypeSystem.GetElementType(mi.ReturnType);
                this.rowTypes = new List <MetaType>(1)
                {
                    this.GetMetaType(elementType)
                }.AsReadOnly();
            }
            else if (attrs.Length > 0)
            {
                List <MetaType> rowTypes = new List <MetaType>();
                foreach (ResultTypeAttribute rat in attrs)
                {
                    Type     type = rat.Type;
                    MetaType mt   = this.GetMetaType(type);
                    // Only add unique meta types
                    if (!rowTypes.Contains(mt))
                    {
                        rowTypes.Add(mt);
                    }
                }
                this.rowTypes = rowTypes.AsReadOnly();
            }
            else
            {
                this.returnParameter = new AttributedMetaParameter(this.methodInfo.ReturnParameter);
            }

            // gather up all meta parameter
            ParameterInfo[] pis = mi.GetParameters();
            if (pis.Length > 0)
            {
                List <MetaParameter> mps = new List <MetaParameter>(pis.Length);
                for (int i = 0, n = pis.Length; i < n; i++)
                {
                    AttributedMetaParameter metaParam = new AttributedMetaParameter(pis[i]);
                    mps.Add(metaParam);
                }
                this.parameters = mps.AsReadOnly();
            }
            else
            {
                this.parameters = _emptyParameters;
            }
        }