using System;
using System.Linq;
using Marr.Data.Mapping.Strategies;
using System.Reflection;
using System.Collections;
namespace Marr.Data.Mapping
{
[Obsolete("This class is obsolete. Please use the 'Mappings' class.")]
public class MapBuilder
{
private bool _publicOnly;
public MapBuilder()
: this(true)
{ }
public MapBuilder(bool publicOnly)
{
_publicOnly = publicOnly;
}
#region - Columns -
///
/// Creates column mappings for the given type.
/// Maps all properties except ICollection properties.
///
/// The type that is being built.
///
public ColumnMapBuilder BuildColumns()
{
return BuildColumns(m => m.MemberType == MemberTypes.Property &&
!typeof(ICollection).IsAssignableFrom((m as PropertyInfo).PropertyType));
}
///
/// Creates column mappings for the given type.
/// Maps all properties that are simple types (int, string, DateTime, etc).
/// ICollection properties are not included.
///
/// The type that is being built.
///
public ColumnMapBuilder BuildColumnsFromSimpleTypes()
{
return BuildColumns(m => m.MemberType == MemberTypes.Property &&
DataHelper.IsSimpleType((m as PropertyInfo).PropertyType) &&
!typeof(ICollection).IsAssignableFrom((m as PropertyInfo).PropertyType));
}
///
/// Creates column mappings for the given type.
/// Maps properties that are included in the include list.
///
/// The type that is being built.
///
///
public ColumnMapBuilder BuildColumns(params string[] propertiesToInclude)
{
return BuildColumns(m =>
m.MemberType == MemberTypes.Property &&
propertiesToInclude.Contains(m.Name));
}
///
/// Creates column mappings for the given type.
/// Maps all properties except the ones in the exclusion list.
///
/// The type that is being built.
///
///
public ColumnMapBuilder BuildColumnsExcept(params string[] propertiesToExclude)
{
return BuildColumns(m =>
m.MemberType == MemberTypes.Property &&
!propertiesToExclude.Contains(m.Name));
}
///
/// Creates column mappings for the given type if they match the predicate.
///
/// The type that is being built.
/// Determines whether a mapping should be created based on the member info.
///
public ColumnMapBuilder BuildColumns(Func predicate)
{
Type entityType = typeof(T);
ConventionMapStrategy strategy = new ConventionMapStrategy(_publicOnly);
strategy.ColumnPredicate = predicate;
ColumnMapCollection columns = strategy.MapColumns(entityType);
MapRepository.Instance.Columns[entityType] = columns;
return new ColumnMapBuilder(null, columns);
}
///
/// Creates a ColumnMapBuilder that starts out with no pre-populated columns.
/// All columns must be added manually using the builder.
///
///
///
public ColumnMapBuilder Columns()
{
Type entityType = typeof(T);
ColumnMapCollection columns = new ColumnMapCollection();
MapRepository.Instance.Columns[entityType] = columns;
return new ColumnMapBuilder(null, columns);
}
#endregion
#region - Relationships -
///
/// Creates relationship mappings for the given type.
/// Maps all properties that implement ICollection.
///
/// The type that is being built.
///
public RelationshipBuilder BuildRelationships()
{
return BuildRelationships(m =>
m.MemberType == MemberTypes.Property &&
typeof(ICollection).IsAssignableFrom((m as PropertyInfo).PropertyType));
}
///
/// Creates relationship mappings for the given type.
/// Maps all properties that are listed in the include list.
///
/// The type that is being built.
///
///
public RelationshipBuilder BuildRelationships(params string[] propertiesToInclude)
{
Func predicate = m =>
(
// ICollection properties
m.MemberType == MemberTypes.Property &&
typeof(ICollection).IsAssignableFrom((m as PropertyInfo).PropertyType) &&
propertiesToInclude.Contains(m.Name)
) || ( // Single entity properties
m.MemberType == MemberTypes.Property &&
!typeof(ICollection).IsAssignableFrom((m as PropertyInfo).PropertyType) &&
propertiesToInclude.Contains(m.Name)
);
return BuildRelationships(predicate);
}
///
/// Creates relationship mappings for the given type if they match the predicate.
///
/// The type that is being built.
/// Determines whether a mapping should be created based on the member info.
///
public RelationshipBuilder BuildRelationships(Func predicate)
{
Type entityType = typeof(T);
ConventionMapStrategy strategy = new ConventionMapStrategy(_publicOnly);
strategy.RelationshipPredicate = predicate;
RelationshipCollection relationships = strategy.MapRelationships(entityType);
MapRepository.Instance.Relationships[entityType] = relationships;
return new RelationshipBuilder(null, relationships);
}
///
/// Creates a RelationshipBuilder that starts out with no pre-populated relationships.
/// All relationships must be added manually using the builder.
///
///
///
public RelationshipBuilder Relationships()
{
Type entityType = typeof(T);
RelationshipCollection relationships = new RelationshipCollection();
MapRepository.Instance.Relationships[entityType] = relationships;
return new RelationshipBuilder(null, relationships);
}
#endregion
#region - Tables -
///
/// Provides a fluent table mapping interface.
///
///
///
public TableBuilder BuildTable()
{
return new TableBuilder(null);
}
///
/// Sets the table name for a given type.
///
///
///
public TableBuilder BuildTable(string tableName)
{
return new TableBuilder(null).SetTableName(tableName);
}
#endregion
}
}