Exemplo n.º 1
0
 protected void RegisterEntityInfo(IEntityInfo info)
 {
     lock (m_entities)
     {
         m_entities.Add(info);
     }
 }
Exemplo n.º 2
0
        public EntityInfoCollection <EntityInfo> GetEntities()
        {
            var coll = new EntityInfoCollection <EntityInfo>();

            foreach (var entity in m_entities)
            {
                coll.Add(entity);
            }
            return(coll);
        }
Exemplo n.º 3
0
        private void AddType(Type entityType, bool verifyInterface)
        {
            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))
            {
                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;
                    }
                    attribute.FieldName = attribute.FieldName.Replace(" ", "_");

                    if (!attribute.DataTypeIsValid)
                    {
                        // TODO: add custom converter support here
                        attribute.DataType = prop.PropertyType.ToDbType();
                    }
                    // Ensures the IsIdentity is set on the field, otherwise it could have side effects at table creation
                    if (attribute.IsPrimaryKey & map.EntityAttribute.KeyScheme == KeyScheme.Identity)
                    {
                        attribute.IsIdentity = true;
                    }
                    map.Fields.Add(attribute);
                    if (attribute.SortOrder != FieldOrder.None)
                    {
                        map.SortingFields.Add(attribute);
                    }
                }
                else
                {
                    var reference = prop.GetCustomAttributes(true).Where(a => a.GetType().Equals(typeof(ReferenceAttribute))).FirstOrDefault() as ReferenceAttribute;

                    if (reference != null)
                    {
                        //if (!prop.PropertyType.IsArray)
                        //{
                        //    throw new InvalidReferenceTypeException(reference.ReferenceEntityType, reference.ReferenceField,
                        //        "Reference fields must be arrays");
                        //}

                        reference.PropertyInfo = prop;
                        reference.IsArray      = prop.PropertyType.IsArray;
                        var interfaces = prop.PropertyType.GetInterfaces();
                        reference.IsList = (from el in prop.PropertyType.GetInterfaces()
                                            where el.Name.Equals("IList")
                                            select el).FirstOrDefault() != null;
                        map.References.Add(reference);
                    }
                }
            }
            if (map.Fields.Count == 0)
            {
                throw new OpenNETCF.ORM.EntityDefinitionException(map.EntityName, string.Format("Entity '{0}' Contains no Field definitions.", map.EntityName));
            }
            if (map.References != null && map.References.Count > 0 && map.Fields.KeyFields.Count != 1)
            {
                throw new OpenNETCF.ORM.EntityDefinitionException(map.EntityName, string.Format("Entity '{0}' Contains references but does have an invalid Primary Key count ({1}).", map.EntityName, map.Fields.KeyFields.Count));
            }
            var createProxyMethod = entityType.GetMethod("ORM_CreateProxy", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);

            if (createProxyMethod != null)
            {
                var parameters = createProxyMethod.GetParameters();
                if (parameters.Length >= 2 && parameters[1].ParameterType.Equals(typeof(System.Collections.Generic.IDictionary <string, int>)))
                {
                    map.CreateProxy = (EntityInfo.CreateProxyDelegate)Delegate.CreateDelegate(typeof(EntityInfo.CreateProxyDelegate), null, createProxyMethod);
                }
            }

            var ctor = entityType.GetConstructor(new Type[] {});

            if (ctor != null)
            {
                map.DefaultConstructor = ctor;
            }
            else // We want a default constructor because we want the ability to instantiate new empty objects to fill in.
            {
                throw new EntityDefinitionException(entityType.ToString(), "The Type doesn't have a parameterless default constructor");
            }

            var serializerMethod = entityType.GetMethod("Serialize", BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);

            if (serializerMethod != null)
            {
                var serParameters = serializerMethod.GetParameters();
                if (serParameters.Length >= 2 &&
                    serParameters[0].ParameterType.Equals(typeof(object)) &&
                    serParameters[1].ParameterType.Equals(typeof(string)))
                {
                    var deserializerMethod = entityType.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
                    if (deserializerMethod != null)
                    {
                        var desParameters = deserializerMethod.GetParameters();
                        if (desParameters.Length >= 3 &&
                            desParameters[0].ParameterType.Equals(typeof(object)) &&
                            desParameters[1].ParameterType.Equals(typeof(string)) &&
                            desParameters[2].ParameterType.Equals(typeof(object)))
                        {
                            map.Serializer   = (EntityInfo.SerializerDelegate)Delegate.CreateDelegate(typeof(EntityInfo.SerializerDelegate), null, serializerMethod);
                            map.Deserializer = (EntityInfo.DeserializerDelegate)Delegate.CreateDelegate(typeof(EntityInfo.DeserializerDelegate), null, deserializerMethod);
                        }
                    }
                }
            }

            m_entities.Add(map);
            OnTypeAdded(map);
        }