mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-14 10:47:08 -07:00
added marr.datamapper source code for easy debugging.
This commit is contained in:
parent
58a05fcef8
commit
3cdff3bb71
96 changed files with 9198 additions and 363 deletions
75
Marr.Data/Mapping/Strategies/AttributeMapStrategy.cs
Normal file
75
Marr.Data/Mapping/Strategies/AttributeMapStrategy.cs
Normal file
|
@ -0,0 +1,75 @@
|
|||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
using Marr.Data.Converters;
|
||||
using Marr.Data.Parameters;
|
||||
|
||||
namespace Marr.Data.Mapping.Strategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps fields or properties that are marked with the ColumnAttribute.
|
||||
/// </summary>
|
||||
public class AttributeMapStrategy : ReflectionMapStrategyBase
|
||||
{
|
||||
public AttributeMapStrategy()
|
||||
: base()
|
||||
{ }
|
||||
|
||||
public AttributeMapStrategy(bool publicOnly)
|
||||
: base(publicOnly)
|
||||
{ }
|
||||
|
||||
public AttributeMapStrategy(BindingFlags flags)
|
||||
: base(flags)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Registers any member with a ColumnAttribute as a ColumnMap.
|
||||
/// <param name="entityType">The entity that is being mapped.</param>
|
||||
/// <param name="member">The current member that is being inspected.</param>
|
||||
/// <param name="columnAtt">A ColumnAttribute (is null of one does not exist).</param>
|
||||
/// <param name="columnMaps">A list of ColumnMaps.</param>
|
||||
/// </summary>
|
||||
protected override void CreateColumnMap(Type entityType, MemberInfo member, ColumnAttribute columnAtt, ColumnMapCollection columnMaps)
|
||||
{
|
||||
if (columnAtt != null)
|
||||
{
|
||||
ColumnMap columnMap = new ColumnMap(member, columnAtt);
|
||||
columnMaps.Add(columnMap);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers any member with a RelationshipAttribute as a relationship.
|
||||
/// </summary>
|
||||
/// <param name="entityType">The entity that is being mapped.</param>
|
||||
/// <param name="member">The current member that is being inspected.</param>
|
||||
/// <param name="relationshipAtt">A RelationshipAttribute (is null if one does not exist).</param>
|
||||
/// <param name="relationships">A list of Relationships.</param>
|
||||
protected override void CreateRelationship(Type entityType, MemberInfo member, RelationshipAttribute relationshipAtt, RelationshipCollection relationships)
|
||||
{
|
||||
if (relationshipAtt != null)
|
||||
{
|
||||
Relationship relationship = new Relationship(member, relationshipAtt);
|
||||
relationships.Add(relationship);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
51
Marr.Data/Mapping/Strategies/ConventionMapStrategy.cs
Normal file
51
Marr.Data/Mapping/Strategies/ConventionMapStrategy.cs
Normal file
|
@ -0,0 +1,51 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
using System.Collections;
|
||||
|
||||
namespace Marr.Data.Mapping.Strategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Allows you to specify a member based filter by defining predicates that filter the members that are mapped.
|
||||
/// </summary>
|
||||
public class ConventionMapStrategy : ReflectionMapStrategyBase
|
||||
{
|
||||
public ConventionMapStrategy(bool publicOnly)
|
||||
: base(publicOnly)
|
||||
{
|
||||
// Default: Only map members that are properties
|
||||
ColumnPredicate = m => m.MemberType == MemberTypes.Property;
|
||||
|
||||
// Default: Only map members that are properties and that are ICollection types
|
||||
RelationshipPredicate = m =>
|
||||
{
|
||||
return m.MemberType == MemberTypes.Property && typeof(ICollection).IsAssignableFrom((m as PropertyInfo).PropertyType);
|
||||
};
|
||||
}
|
||||
|
||||
public Func<MemberInfo, bool> ColumnPredicate;
|
||||
public Func<MemberInfo, bool> RelationshipPredicate;
|
||||
|
||||
|
||||
|
||||
protected override void CreateColumnMap(Type entityType, System.Reflection.MemberInfo member, ColumnAttribute columnAtt, ColumnMapCollection columnMaps)
|
||||
{
|
||||
if (ColumnPredicate(member))
|
||||
{
|
||||
// Map public property to DB column
|
||||
columnMaps.Add(new ColumnMap(member));
|
||||
}
|
||||
}
|
||||
|
||||
protected override void CreateRelationship(Type entityType, System.Reflection.MemberInfo member, RelationshipAttribute relationshipAtt, RelationshipCollection relationships)
|
||||
{
|
||||
if (RelationshipPredicate(member))
|
||||
{
|
||||
relationships.Add(new Relationship(member));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
48
Marr.Data/Mapping/Strategies/IMapStrategy.cs
Normal file
48
Marr.Data/Mapping/Strategies/IMapStrategy.cs
Normal file
|
@ -0,0 +1,48 @@
|
|||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Marr.Data.Mapping.Strategies
|
||||
{
|
||||
/// <summary>
|
||||
/// A strategy for creating mappings for a given entity.
|
||||
/// </summary>
|
||||
public interface IMapStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a table map for a given entity type.
|
||||
/// </summary>
|
||||
/// <param name="entityType"></param>
|
||||
/// <returns></returns>
|
||||
string MapTable(Type entityType);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a ColumnMapCollection for a given entity type.
|
||||
/// </summary>
|
||||
/// <param name="entityType">The entity that is being mapped.</param>
|
||||
ColumnMapCollection MapColumns(Type entityType);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a RelationshpCollection for a given entity type.
|
||||
/// </summary>
|
||||
/// <param name="entityType">The entity that is being mapped.</param>
|
||||
/// <returns></returns>
|
||||
RelationshipCollection MapRelationships(Type entityType);
|
||||
}
|
||||
}
|
85
Marr.Data/Mapping/Strategies/PropertyMapStrategy.cs
Normal file
85
Marr.Data/Mapping/Strategies/PropertyMapStrategy.cs
Normal file
|
@ -0,0 +1,85 @@
|
|||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Marr.Data.Mapping.Strategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps all public properties to DB columns.
|
||||
/// </summary>
|
||||
public class PropertyMapStrategy : AttributeMapStrategy
|
||||
{
|
||||
public PropertyMapStrategy(bool publicOnly)
|
||||
: base(publicOnly)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Maps properties to DB columns if a ColumnAttribute is not present.
|
||||
/// <param name="entityType">The entity that is being mapped.</param>
|
||||
/// <param name="member">The current member that is being inspected.</param>
|
||||
/// <param name="columnAtt">A ColumnAttribute (is null of one does not exist).</param>
|
||||
/// <param name="columnMaps">A list of ColumnMaps.</param>
|
||||
/// </summary>
|
||||
protected override void CreateColumnMap(Type entityType, MemberInfo member, ColumnAttribute columnAtt, ColumnMapCollection columnMaps)
|
||||
{
|
||||
if (columnAtt != null)
|
||||
{
|
||||
// Add columns with ColumnAttribute
|
||||
base.CreateColumnMap(entityType, member, columnAtt, columnMaps);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
// Map public property to DB column
|
||||
columnMaps.Add(new ColumnMap(member));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a relationship if a RelationshipAttribute is present.
|
||||
/// </summary>
|
||||
/// <param name="entityType">The entity that is being mapped.</param>
|
||||
/// <param name="member">The current member that is being inspected.</param>
|
||||
/// <param name="relationshipAtt">A RelationshipAttribute (is null if one does not exist).</param>
|
||||
/// <param name="relationships">A list of Relationships.</param>
|
||||
protected override void CreateRelationship(Type entityType, MemberInfo member, RelationshipAttribute relationshipAtt, RelationshipCollection relationships)
|
||||
{
|
||||
if (relationshipAtt != null)
|
||||
{
|
||||
// Add relationships by RelationshipAttribute
|
||||
base.CreateRelationship(entityType, member, relationshipAtt, relationships);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
PropertyInfo propertyInfo = member as PropertyInfo;
|
||||
if (typeof(System.Collections.ICollection).IsAssignableFrom(propertyInfo.PropertyType))
|
||||
{
|
||||
Relationship relationship = new Relationship(member);
|
||||
relationships.Add(relationship);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
151
Marr.Data/Mapping/Strategies/ReflectionMapStrategyBase.cs
Normal file
151
Marr.Data/Mapping/Strategies/ReflectionMapStrategyBase.cs
Normal file
|
@ -0,0 +1,151 @@
|
|||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Marr.Data.Mapping.Strategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Iterates through the members of an entity based on the BindingFlags, and provides an abstract method for adding ColumnMaps for each member.
|
||||
/// </summary>
|
||||
public abstract class ReflectionMapStrategyBase : IMapStrategy
|
||||
{
|
||||
private BindingFlags _bindingFlags;
|
||||
|
||||
/// <summary>
|
||||
/// Loops through members with the following BindingFlags:
|
||||
/// Instance | NonPublic | Public | FlattenHierarchy
|
||||
/// </summary>
|
||||
public ReflectionMapStrategyBase()
|
||||
{
|
||||
_bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loops through members with the following BindingFlags:
|
||||
/// Instance | Public | FlattenHierarchy | NonPublic (optional)
|
||||
/// </summary>
|
||||
/// <param name="publicOnly"></param>
|
||||
public ReflectionMapStrategyBase(bool publicOnly)
|
||||
{
|
||||
if (publicOnly)
|
||||
{
|
||||
_bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy;
|
||||
}
|
||||
else
|
||||
{
|
||||
_bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loops through members based on the passed in BindingFlags.
|
||||
/// </summary>
|
||||
/// <param name="bindingFlags"></param>
|
||||
public ReflectionMapStrategyBase(BindingFlags bindingFlags)
|
||||
{
|
||||
_bindingFlags = bindingFlags;
|
||||
}
|
||||
|
||||
public string MapTable(Type entityType)
|
||||
{
|
||||
object[] atts = entityType.GetCustomAttributes(typeof(TableAttribute), true);
|
||||
if (atts.Length > 0)
|
||||
{
|
||||
return (atts[0] as TableAttribute).Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
return entityType.Name;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements IMapStrategy.
|
||||
/// Loops through filtered members and calls the virtual "CreateColumnMap" void for each member.
|
||||
/// Subclasses can override CreateColumnMap to customize adding ColumnMaps.
|
||||
/// </summary>
|
||||
/// <param name="entityType"></param>
|
||||
/// <returns></returns>
|
||||
public ColumnMapCollection MapColumns(Type entityType)
|
||||
{
|
||||
ColumnMapCollection columnMaps = new ColumnMapCollection();
|
||||
|
||||
MemberInfo[] members = entityType.GetMembers(_bindingFlags);
|
||||
foreach (var member in members)
|
||||
{
|
||||
ColumnAttribute columnAtt = GetColumnAttribute(member);
|
||||
CreateColumnMap(entityType, member, columnAtt, columnMaps);
|
||||
}
|
||||
|
||||
return columnMaps;
|
||||
}
|
||||
|
||||
public RelationshipCollection MapRelationships(Type entityType)
|
||||
{
|
||||
RelationshipCollection relationships = new RelationshipCollection();
|
||||
|
||||
MemberInfo[] members = entityType.GetMembers(_bindingFlags);
|
||||
foreach (MemberInfo member in members)
|
||||
{
|
||||
RelationshipAttribute relationshipAtt = GetRelationshipAttribute(member);
|
||||
CreateRelationship(entityType, member, relationshipAtt, relationships);
|
||||
}
|
||||
|
||||
return relationships;
|
||||
}
|
||||
|
||||
protected ColumnAttribute GetColumnAttribute(MemberInfo member)
|
||||
{
|
||||
if (member.IsDefined(typeof(ColumnAttribute), false))
|
||||
{
|
||||
return (ColumnAttribute)member.GetCustomAttributes(typeof(ColumnAttribute), false)[0];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected RelationshipAttribute GetRelationshipAttribute(MemberInfo member)
|
||||
{
|
||||
if (member.IsDefined(typeof(RelationshipAttribute), false))
|
||||
{
|
||||
return (RelationshipAttribute)member.GetCustomAttributes(typeof(RelationshipAttribute), false)[0];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inspect a member and optionally add a ColumnMap.
|
||||
/// </summary>
|
||||
/// <param name="entityType">The entity type that is being mapped.</param>
|
||||
/// <param name="member">The member that is being mapped.</param>
|
||||
/// <param name="columnMaps">The ColumnMapCollection that is being created.</param>
|
||||
protected abstract void CreateColumnMap(Type entityType, MemberInfo member, ColumnAttribute columnAtt, ColumnMapCollection columnMaps);
|
||||
|
||||
/// <summary>
|
||||
/// Inspect a member and optionally add a Relationship.
|
||||
/// </summary>
|
||||
/// <param name="entityType">The entity that is being mapped.</param>
|
||||
/// <param name="member">The current member that is being inspected.</param>
|
||||
/// <param name="relationshipAtt">A RelationshipAttribute (is null if one does not exist).</param>
|
||||
/// <param name="relationships">A list of Relationships.</param>
|
||||
protected abstract void CreateRelationship(Type entityType, MemberInfo member, RelationshipAttribute relationshipAtt, RelationshipCollection relationships);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue