Esempio n. 1
0
        private static void LoadEntityMappingConfiguration()
        {
            lock (lockObject)
            {
                EAppDataConfigurationSection configuration = (EAppDataConfigurationSection)ConfigurationManager.GetSection("EAppData");

                EntityMappingConfiguration entityMappingConfig = null;

                foreach (EntityMappingElement entityMappingItem in configuration.EntityMappings)
                {
                    string mappingName = entityMappingItem.Name;

                    string mappingFile = entityMappingItem.File;

                    using (FileStream mappingFileStream = new FileStream(mappingFile, FileMode.Open, FileAccess.Read))
                    {
                        using (MemoryStream mappingMemoryStream = new MemoryStream())
                        {
                            byte[] buffers = new byte[2048];

                            int position = mappingFileStream.Read(buffers, 0, buffers.Length);

                            mappingMemoryStream.Seek(0, SeekOrigin.Begin);

                            while (position > 0)
                            {
                                mappingMemoryStream.Write(buffers, 0, position);

                                position = mappingFileStream.Read(buffers, 0, buffers.Length);
                            }

                            mappingMemoryStream.Flush();

                            entityMappingConfig =
                                ObjectSerializerFactory.GetObjectSerializer("XML").
                                Deserialize <EntityMappingConfiguration>(mappingMemoryStream.ToArray());

                            mappingMemoryStream.Close();
                        }

                        mappingFileStream.Close();
                    }

                    if (entityMappingConfig.Entities != null &&
                        entityMappingConfig.Entities.Length > 0)
                    {
                        foreach (EntityConfiguration entityConfig in entityMappingConfig.Entities)
                        {
                            if (!entityConfigs.ContainsKey(entityConfig.Name))
                            {
                                entityConfigs.Add(entityConfig.Name, entityConfig);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public void BuildEntityMappingConfigurationXml()
        {
            EntityMappingConfiguration config = new EntityMappingConfiguration();

            EntityConfiguration entity = new EntityConfiguration();

            entity.Name      = "Post";
            entity.TableName = "post";
            entity.TypeName  = typeof(Post).AssemblyQualifiedName;

            PropertyConfiguration postIdProperty = new PropertyConfiguration();

            postIdProperty.Name              = "Id";
            postIdProperty.PropertyType      = typeof(int).FullName;
            postIdProperty.ColumnName        = "post_id";
            postIdProperty.IsPrimaryKey      = true;
            postIdProperty.IsAutoIdentity    = true;
            postIdProperty.IsRelationKey     = false;
            postIdProperty.RelatedForeignKey = string.Empty;
            postIdProperty.RelatedType       = string.Empty;

            PropertyConfiguration postTopicIdProperty = new PropertyConfiguration();

            postTopicIdProperty.Name              = "TopicId";
            postTopicIdProperty.PropertyType      = typeof(int).FullName;
            postTopicIdProperty.ColumnName        = "post_topic_id";
            postTopicIdProperty.IsPrimaryKey      = false;
            postTopicIdProperty.IsAutoIdentity    = false;
            postTopicIdProperty.IsRelationKey     = true;
            postTopicIdProperty.RelatedForeignKey = "topic_id";
            postTopicIdProperty.RelatedType       = typeof(Topic).FullName;

            PropertyConfiguration postAuthorIdProperty = new PropertyConfiguration();

            postAuthorIdProperty.Name              = "AuthorId";
            postAuthorIdProperty.PropertyType      = typeof(int).FullName;
            postAuthorIdProperty.ColumnName        = "post_author_id";
            postAuthorIdProperty.IsPrimaryKey      = false;
            postAuthorIdProperty.IsAutoIdentity    = false;
            postAuthorIdProperty.IsRelationKey     = true;
            postAuthorIdProperty.RelatedForeignKey = "user_id";
            postAuthorIdProperty.RelatedType       = typeof(User).FullName;

            PropertyConfiguration postContentProperty = new PropertyConfiguration();

            postContentProperty.Name           = "Content";
            postContentProperty.PropertyType   = typeof(string).FullName;
            postContentProperty.SqlType        = "nvarchar(max)";
            postContentProperty.ColumnName     = "post_content";
            postContentProperty.IsPrimaryKey   = false;
            postContentProperty.IsAutoIdentity = false;

            PropertyConfiguration postCreationDateTimeProperty = new PropertyConfiguration();

            postCreationDateTimeProperty.Name           = "CreationDateTime";
            postCreationDateTimeProperty.PropertyType   = typeof(DateTime).FullName;
            postCreationDateTimeProperty.ColumnName     = "post_creation_datetime";
            postCreationDateTimeProperty.IsPrimaryKey   = false;
            postCreationDateTimeProperty.IsAutoIdentity = false;

            entity.Properties = new PropertyConfiguration[] { postIdProperty, postTopicIdProperty, postAuthorIdProperty, postContentProperty, postCreationDateTimeProperty };

            config.Entities = new EntityConfiguration[] { entity };

            bool serialized = SerializationManager.Serialize(config, AppDomain.CurrentDomain.BaseDirectory + "EntityMappingConfig.xml");

            using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "EntityMappingConfig.xml", FileMode.Open, FileAccess.Read))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] buffers = new byte[2048];

                    int position = fs.Read(buffers, 0, buffers.Length);

                    ms.Seek(0, SeekOrigin.Begin);
                    while (position > 0)
                    {
                        ms.Write(buffers, 0, position);

                        position = fs.Read(buffers, 0, buffers.Length);
                    }
                    ms.Flush();

                    EntityMappingConfiguration entityMappingConfig =
                        ObjectSerializerFactory.GetObjectSerializer("XML").Deserialize <EntityMappingConfiguration>(ms.ToArray());

                    Assert.AreEqual(config.Entities[0].Properties[0].SqlType, entityMappingConfig.Entities[0].Properties[0].SqlType);

                    Assert.AreEqual(config.Entities[0].Properties[2].PropertyType, entityMappingConfig.Entities[0].Properties[2].PropertyType);
                }
            }
        }