示例#1
0
        public IEntityInfo GetEntityInfo(string entityName)
        {
            if (!Entities.Contains(entityName))
            {
                DiscoverDynamicEntity(entityName);
            }

            return(Entities[entityName]);
        }
示例#2
0
        private void AddType(Type entityType, bool verifyInterface, bool ensureCompatibility)
        {
            var attr = (from a in entityType.GetCustomAttributes(true)
                        where a.GetType().Equals(typeof(EntityAttribute))
                        select a).FirstOrDefault() as EntityAttribute;

            if (verifyInterface)
            {
                if (attr == null)
                {
                    throw new ArgumentException(
                              string.Format("Type '{0}' does not have an EntityAttribute", entityType.Name));
                }
            }

            var map = new TEntityInfo();

            // store the NameInStore if  not explicitly set
            if (attr.NameInStore == null)
            {
                attr.NameInStore = entityType.Name;
            }

            //TODO: validate NameInStore

            map.Initialize(attr, entityType);

            // see if we have any entity
            // get all field definitions
            foreach (var prop in entityType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
            {
                var attribute = prop.GetCustomAttributes(true)
                                .Where(a =>
                                       (a.GetType().Equals(typeof(FieldAttribute)))
                                       ).FirstOrDefault() as FieldAttribute;

                if (attribute != null)
                {
                    attribute.PropertyInfo = prop;

                    // construct the true FieldAttribute by merging the propertyinfo and the fileldattribute overrides
                    if (attribute.FieldName == null)
                    {
                        attribute.FieldName = prop.Name;
                    }

                    if (!attribute.DataTypeIsValid)
                    {
                        // first call any custom type converter
                        var dt = PropertyTypeToDbType(prop.PropertyType);

                        if (dt.HasValue)
                        {
                            attribute.DataType = dt.Value;
                        }
                        else
                        {
                            // no custom override, use the default
                            attribute.DataType = prop.PropertyType.ToDbType();
                        }
                    }

                    if (!map.Fields.ContainsField(attribute.FieldName))
                    {
                        map.Fields.Add(attribute);
                    }

                    if (m_entities.Contains(map.EntityName))
                    {
                        if (m_entities[map.EntityName].Fields.ContainsField(attribute.FieldName))
                        { // make sure the PropertyInfo is set (dynamic discovery can leave this null)
                            if (m_entities[map.EntityName].Fields[attribute.FieldName].PropertyInfo == null)
                            {
                                m_entities[map.EntityName].Fields[attribute.FieldName].PropertyInfo = prop;
                            }
                        }
                        else
                        { // the entity doesn't contain this named field, but should.
                            if (ensureCompatibility)
                            {
                                m_entities[map.EntityName].Fields.Add(attribute);
                            }
                            else
                            {
                                throw new FieldNotFoundException(string.Format("Field '{0}' not found in destination Entity '{1}'. Consider calling with ensureCompatibility parameter set to 'true'.",
                                                                               attribute.FieldName, map.EntityName));
                            }
                        }
                    }
                }
                else
                {
                    var reference = prop.GetCustomAttributes(true).Where(a => a.GetType().Equals(typeof(ReferenceAttribute))).FirstOrDefault() as ReferenceAttribute;

                    if (reference != null)
                    {
                        reference.PropertyInfo = prop;
                        map.References.Add(reference);
                    }
                }
            }

//            if (m_entities.Contains(map.EntityName))
//            {
//                // this will ensure that the m_entities type to name map is properly filled out
//                m_entities.Add(map);
//                return;
//            }

            if (map.Fields.Count == 0)
            {
                throw new OpenNETCF.ORM.EntityDefinitionException(map.EntityName, string.Format("Entity '{0}' Contains no Field definitions.", map.EntityName));
            }

            // store a creator proxy delegate if the entity supports it (*way* faster for Selects)
            var methodInfo = entityType.GetMethod("ORM_CreateProxy", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

            if (methodInfo != null)
            {
                map.CreateProxy = (EntityCreatorDelegate)Delegate.CreateDelegate(typeof(EntityCreatorDelegate), null, methodInfo);
            }

            m_entities.Add(map);

            AfterAddEntityType(entityType, ensureCompatibility);

            var handler = EntityTypeAdded;

            if (handler != null)
            {
                var info = this.Entities[attr.NameInStore];
                var args = new EntityTypeAddedArgs(info);
                handler(this, args);
            }
        }