mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-20 21:43:33 -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
90
Marr.Data/Reflection/CachedReflectionStrategy.cs
Normal file
90
Marr.Data/Reflection/CachedReflectionStrategy.cs
Normal file
|
@ -0,0 +1,90 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
using FastReflection;
|
||||
|
||||
namespace Marr.Data.Reflection
|
||||
{
|
||||
public class CachedReflectionStrategy : IReflectionStrategy
|
||||
{
|
||||
private FastReflection.CachedReflector _reflector;
|
||||
|
||||
public CachedReflectionStrategy()
|
||||
{
|
||||
_reflector = new CachedReflector();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets an entity field value by name to the passed in 'val'.
|
||||
/// </summary>
|
||||
public void SetFieldValue<T>(T entity, string fieldName, object val)
|
||||
{
|
||||
MemberInfo member = entity.GetType().GetMember(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)[0];
|
||||
|
||||
try
|
||||
{
|
||||
// Handle DB null values
|
||||
if (val == DBNull.Value)
|
||||
{
|
||||
if (member.MemberType == MemberTypes.Field)
|
||||
_reflector.SetValue(member, entity, ReflectionHelper.GetDefaultValue((member as FieldInfo).FieldType));
|
||||
|
||||
else if (member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
var pi = (member as PropertyInfo);
|
||||
if (pi.CanWrite)
|
||||
_reflector.SetValue(member, entity, ReflectionHelper.GetDefaultValue((member as PropertyInfo).PropertyType));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (member.MemberType == MemberTypes.Field)
|
||||
_reflector.SetValue(member, entity, val);
|
||||
else if (member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
var pi = (member as PropertyInfo);
|
||||
if (pi.CanWrite)
|
||||
_reflector.SetValue(member, entity, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string msg = string.Format("The DataMapper was unable to load the following field: {0}. \nDetails: {1}", fieldName, ex.Message);
|
||||
throw new DataMappingException(msg, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an entity field value by name.
|
||||
/// </summary>
|
||||
public object GetFieldValue(object entity, string fieldName)
|
||||
{
|
||||
MemberInfo member = entity.GetType().GetMember(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)[0];
|
||||
|
||||
if (member.MemberType == MemberTypes.Field)
|
||||
{
|
||||
return _reflector.GetValue(member, entity);
|
||||
}
|
||||
else if (member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
if ((member as PropertyInfo).CanRead)
|
||||
return _reflector.GetValue(member, entity);
|
||||
}
|
||||
|
||||
throw new DataMappingException(string.Format("The DataMapper could not get the value for {0}.{1}.", entity.GetType().Name, fieldName));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiantes a type using the FastReflector library for increased speed.
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public object CreateInstance(Type type)
|
||||
{
|
||||
return _reflector.Instantiate(type);
|
||||
}
|
||||
}
|
||||
}
|
14
Marr.Data/Reflection/IReflectionStrategy.cs
Normal file
14
Marr.Data/Reflection/IReflectionStrategy.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Marr.Data.Reflection
|
||||
{
|
||||
public interface IReflectionStrategy
|
||||
{
|
||||
void SetFieldValue<T>(T entity, string fieldName, object val);
|
||||
object GetFieldValue(object entity, string fieldName);
|
||||
object CreateInstance(Type type);
|
||||
}
|
||||
}
|
80
Marr.Data/Reflection/ReflectionHelper.cs
Normal file
80
Marr.Data/Reflection/ReflectionHelper.cs
Normal file
|
@ -0,0 +1,80 @@
|
|||
/* 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;
|
||||
/* *
|
||||
* The FastReflection library was written by Renaud Bédard (renaud.bedard@gmail.com)
|
||||
* http://theinstructionlimit.com/?p=76
|
||||
* */
|
||||
using FastReflection;
|
||||
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace Marr.Data
|
||||
// ReSharper restore CheckNamespace
|
||||
{
|
||||
public class ReflectionHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a DBNull.Value to a null for a reference field,
|
||||
/// or the default value of a value field.
|
||||
/// </summary>
|
||||
/// <param name="fieldType"></param>
|
||||
/// <returns></returns>
|
||||
public static object GetDefaultValue(Type fieldType)
|
||||
{
|
||||
if (fieldType.IsGenericType)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (fieldType.IsValueType)
|
||||
{
|
||||
return Activator.CreateInstance(fieldType);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the CLR data type of a MemberInfo.
|
||||
/// If the type is nullable, returns the underlying type.
|
||||
/// </summary>
|
||||
/// <param name="member"></param>
|
||||
/// <returns></returns>
|
||||
public static Type GetMemberType(MemberInfo member)
|
||||
{
|
||||
Type memberType = null;
|
||||
if (member.MemberType == MemberTypes.Property)
|
||||
memberType = (member as PropertyInfo).PropertyType;
|
||||
else if (member.MemberType == MemberTypes.Field)
|
||||
memberType = (member as FieldInfo).FieldType;
|
||||
else
|
||||
memberType = typeof(object);
|
||||
|
||||
// Handle nullable types - get underlying type
|
||||
if (memberType.IsGenericType && memberType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||
{
|
||||
memberType = memberType.GetGenericArguments()[0];
|
||||
}
|
||||
|
||||
return memberType;
|
||||
}
|
||||
}
|
||||
}
|
85
Marr.Data/Reflection/SimpleReflectionStrategy.cs
Normal file
85
Marr.Data/Reflection/SimpleReflectionStrategy.cs
Normal file
|
@ -0,0 +1,85 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
using Marr.Data;
|
||||
|
||||
namespace Marr.Data.Reflection
|
||||
{
|
||||
public class SimpleReflectionStrategy : IReflectionStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets an entity field value by name to the passed in 'val'.
|
||||
/// </summary>
|
||||
public void SetFieldValue<T>(T entity, string fieldName, object val)
|
||||
{
|
||||
MemberInfo member = entity.GetType().GetMember(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)[0];
|
||||
|
||||
try
|
||||
{
|
||||
// Handle DB null values
|
||||
if (val == DBNull.Value)
|
||||
{
|
||||
if (member.MemberType == MemberTypes.Field)
|
||||
(member as FieldInfo).SetValue(entity, ReflectionHelper.GetDefaultValue((member as FieldInfo).FieldType));
|
||||
|
||||
else if (member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
var pi = (member as PropertyInfo);
|
||||
if (pi.CanWrite)
|
||||
(member as PropertyInfo).SetValue(entity, ReflectionHelper.GetDefaultValue((member as PropertyInfo).PropertyType), null);
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (member.MemberType == MemberTypes.Field)
|
||||
(member as FieldInfo).SetValue(entity, val);
|
||||
|
||||
else if (member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
var pi = (member as PropertyInfo);
|
||||
if (pi.CanWrite)
|
||||
(member as PropertyInfo).SetValue(entity, val, null);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string msg = string.Format("The DataMapper was unable to load the following field: {0}. \nDetails: {1}", fieldName, ex.Message);
|
||||
throw new DataMappingException(msg, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an entity field value by name.
|
||||
/// </summary>
|
||||
public object GetFieldValue(object entity, string fieldName)
|
||||
{
|
||||
MemberInfo member = entity.GetType().GetMember(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)[0];
|
||||
|
||||
if (member.MemberType == MemberTypes.Field)
|
||||
{
|
||||
return (member as FieldInfo).GetValue(entity);
|
||||
}
|
||||
else if (member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
if ((member as PropertyInfo).CanRead)
|
||||
return (member as PropertyInfo).GetValue(entity, null);
|
||||
}
|
||||
throw new DataMappingException(string.Format("The DataMapper could not get the value for {0}.{1}.", entity.GetType().Name, fieldName));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiantes a type using the FastReflector library for increased speed.
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public object CreateInstance(Type type)
|
||||
{
|
||||
return Activator.CreateInstance(type);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue