Strong Type API Usage

DatabaseMapping databaseMapping;
TableMapping tableMapping;
ColumnMapping columnMapping;
PropertyInfo[] columnProperties;
PropertyInfo associationProperty;
AssociationMapping associationMapping;

databaseMapping = new DatabaseMapping(typeof(NorthwindDatabase));
tableMapping = new TableMapping(typeof(Category)) { Name = "Categories" };
databaseMapping.Tables.Add(tableMapping);

columnProperties = new[] { 
                typeof(Category).GetProperty("CategoryID"), 
                typeof(Category).GetProperty("CategoryName"),
                typeof(Category).GetProperty("Description"), 
                typeof(Category).GetProperty("Picture"),
            };
associationProperty = typeof(Category).GetProperty("Products");
foreach (var property in columnProperties)
{
    columnMapping = new ColumnMapping(property);
    tableMapping.RowType.Columns.Add(columnMapping);
}

associationMapping = new AssociationMapping(associationProperty);
associationMapping.ThisMember = new[] { typeof(Category).GetProperty("CategoryID") };
tableMapping.RowType.Associations.Add(associationMapping);

tableMapping = new TableMapping(typeof(Product)) { Name = "Products" };
databaseMapping.Tables.Add(tableMapping);

columnProperties = new[] { 
                typeof(Product).GetProperty("ProductID"), 
                typeof(Product).GetProperty("ProductName"),
                typeof(Product).GetProperty("CategoryID"), 
            };
associationProperty = typeof(Product).GetProperty("Category");
foreach (var property in columnProperties)
{
    columnMapping = new ColumnMapping(property);
    tableMapping.RowType.Columns.Add(columnMapping);
}
associationMapping = new AssociationMapping(associationProperty);
associationMapping.ThisMember = new[] { typeof(Product).GetProperty("CategoryID") };
associationMapping.OtherMember = new[] { typeof(Category).GetProperty("CategoryID") };
associationMapping.IsForeignKey = true;
tableMapping.RowType.Associations.Add(associationMapping);
var doc = DatabaseMappingSerializer.Serialize(databaseMapping);

var mappingSource = XmlMappingSource.FromXml(doc.OuterXml);

Last edited Apr 17, 2013 at 8:44 AM by ansiboy, version 1

Comments

No comments yet.