Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed Jun 29, 2010
1 parent 1b55794 commit faa64ed
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 81 deletions.
12 changes: 5 additions & 7 deletions source/MongoDB/Configuration/Mapping/Auto/AutoMappingProfile.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MongoDB.Attributes;
using MongoDB.Configuration.CollectionAdapters;
Expand Down Expand Up @@ -104,13 +105,10 @@ public MemberInfo FindIdMember(Type classType)
/// <returns></returns>
public IEnumerable<MemberInfo> FindMembers(Type classType)
{
foreach (MemberInfo memberInfo in _memberFinder.FindMembers(classType))
{
var doMap = memberInfo.GetCustomAttribute<MongoIgnoreAttribute>(true) == null;

if (doMap)
yield return memberInfo;
}
return from memberInfo in _memberFinder.FindMembers(classType)
let doMap = memberInfo.GetCustomAttribute<MongoIgnoreAttribute>(true) == null
where doMap
select memberInfo;
}

/// <summary>
Expand Down
@@ -1,109 +1,97 @@
using System;
using System.Collections;
using System.Collections.Generic;

using MongoDB.Configuration.CollectionAdapters;

namespace MongoDB.Configuration.Mapping.Conventions
{
/// <summary>
///
/// </summary>
public class DefaultCollectionAdapterConvention : ICollectionAdapterConvention
{
/// <summary>
///
/// </summary>
public static readonly DefaultCollectionAdapterConvention Instance = new DefaultCollectionAdapterConvention();

/// <summary>
///
/// </summary>
private delegate ICollectionAdapter CollectionTypeFactoryDelegate();

/// <summary>
///
/// </summary>
private static readonly Dictionary<Type, CollectionTypeFactoryDelegate> CollectionTypes = new Dictionary<Type, CollectionTypeFactoryDelegate>
{
{ typeof(ArrayList), CreateArrayListCollectionType },
{ typeof(IList), CreateArrayListCollectionType },
{ typeof(ICollection), CreateArrayListCollectionType },
{ typeof(IEnumerable), CreateArrayListCollectionType },
{ typeof(HashSet<>), CreateGenericSetCollectionType },
{ typeof(List<>), CreateGenericListCollectionType },
{ typeof(IList<>), CreateGenericListCollectionType },
{ typeof(ICollection<>), CreateGenericListCollectionType },
{ typeof(IEnumerable<>), CreateGenericListCollectionType }
{typeof(ArrayList), CreateArrayListCollectionType},
{typeof(IList), CreateArrayListCollectionType},
{typeof(ICollection), CreateArrayListCollectionType},
{typeof(IEnumerable), CreateArrayListCollectionType},
{typeof(HashSet<>), CreateGenericSetCollectionType},
{typeof(List<>), CreateGenericListCollectionType},
{typeof(IList<>), CreateGenericListCollectionType},
{typeof(ICollection<>), CreateGenericListCollectionType},
{typeof(IEnumerable<>), CreateGenericListCollectionType}
};

private delegate Type ElementTypeFactoryDelegate(Type type);

/// <summary>
///
/// </summary>
private static readonly Dictionary<Type, ElementTypeFactoryDelegate> ElementTypes = new Dictionary<Type, ElementTypeFactoryDelegate>
{
{ typeof(ArrayList), GetArrayListElementType },
{ typeof(IList), GetArrayListElementType },
{ typeof(ICollection), GetArrayListElementType },
{ typeof(IEnumerable), GetArrayListElementType },
{ typeof(HashSet<>), GetGenericSetElementType },
{ typeof(List<>), GetGenericListElementType },
{ typeof(IList<>), GetGenericListElementType },
{ typeof(ICollection<>), GetGenericListElementType },
{ typeof(IEnumerable<>), GetGenericListElementType }
{typeof(ArrayList), GetArrayListElementType},
{typeof(IList), GetArrayListElementType},
{typeof(ICollection), GetArrayListElementType},
{typeof(IEnumerable), GetArrayListElementType},
{typeof(HashSet<>), GetGenericSetElementType},
{typeof(List<>), GetGenericListElementType},
{typeof(IList<>), GetGenericListElementType},
{typeof(ICollection<>), GetGenericListElementType},
{typeof(IEnumerable<>), GetGenericListElementType}
};

/// <summary>
/// </summary>
public static readonly DefaultCollectionAdapterConvention Instance = new DefaultCollectionAdapterConvention();

/// <summary>
/// Initializes a new instance of the <see cref="DefaultCollectionAdapterConvention"/> class.
/// </summary>
private DefaultCollectionAdapterConvention()
{ }
{
}

/// <summary>
/// Gets the type of the collection.
/// Gets the type of the collection.
/// </summary>
/// <param name="type">The type.</param>
/// <param name = "type">The type.</param>
/// <returns></returns>
public ICollectionAdapter GetCollectionAdapter(Type type)
{
CollectionTypeFactoryDelegate factory;
if (CollectionTypes.TryGetValue(type, out factory))
if(CollectionTypes.TryGetValue(type, out factory))
return factory();

if(type.IsArray && type != typeof(byte[]))
{
return new ArrayCollectionAdapter();
}

if (type.IsGenericType && !type.IsGenericTypeDefinition)
if(type.IsGenericType && !type.IsGenericTypeDefinition)
{
Type genericType = type.GetGenericTypeDefinition();
if (CollectionTypes.TryGetValue(genericType, out factory))
var genericType = type.GetGenericTypeDefinition();
if(CollectionTypes.TryGetValue(genericType, out factory))
return factory();
}

return null;
}

/// <summary>
/// Gets the type of the element.
/// Gets the type of the element.
/// </summary>
/// <param name="type">The type.</param>
/// <param name = "type">The type.</param>
/// <returns></returns>
public Type GetElementType(Type type)
{
ElementTypeFactoryDelegate factory;
if (ElementTypes.TryGetValue(type, out factory))
if(ElementTypes.TryGetValue(type, out factory))
return factory(type);

if (type.IsArray)
{
if(type.IsArray)
return type.GetElementType();
}

if (type.IsGenericType && !type.IsGenericTypeDefinition)
if(type.IsGenericType && !type.IsGenericTypeDefinition)
{
var genericType = type.GetGenericTypeDefinition();
if (ElementTypes.TryGetValue(genericType, out factory))
if(ElementTypes.TryGetValue(genericType, out factory))
return factory(type);
}

Expand Down Expand Up @@ -139,5 +127,11 @@ private static Type GetGenericSetElementType(Type type)
{
return type.GetGenericArguments()[0];
}

/// <summary>
/// </summary>
private delegate ICollectionAdapter CollectionTypeFactoryDelegate();

private delegate Type ElementTypeFactoryDelegate(Type type);
}
}
Expand Up @@ -6,46 +6,39 @@
namespace MongoDB.Configuration.Mapping.Conventions
{
/// <summary>
///
/// </summary>
public class DefaultDictionaryAdapterConvention : IDictionarynAdapterConvention
{
/// <summary>
///
/// </summary>
public static readonly DefaultDictionaryAdapterConvention Instance = new DefaultDictionaryAdapterConvention();

/// <summary>
///
/// </summary>
private delegate IDictionaryAdapter DictionaryTypeFactoryDelegate();

/// <summary>
///
/// </summary>
private static readonly Dictionary<Type, DictionaryTypeFactoryDelegate> DictionaryTypes = new Dictionary<Type, DictionaryTypeFactoryDelegate>
{
{ typeof(Document), CreateDocumentType },
{ typeof(IDictionary), CreateHashtableType },
{ typeof(Hashtable), CreateHashtableType },
{ typeof(IEnumerable<DictionaryEntry>), CreateHashtableType }
{typeof(Document), CreateDocumentType},
{typeof(IDictionary), CreateHashtableType},
{typeof(Hashtable), CreateHashtableType},
{typeof(IEnumerable<DictionaryEntry>), CreateHashtableType}
};

/// <summary>
/// </summary>
public static readonly DefaultDictionaryAdapterConvention Instance = new DefaultDictionaryAdapterConvention();

private DefaultDictionaryAdapterConvention()
{ }
{
}

/// <summary>
/// Gets the type of the dictionary.
/// Gets the type of the dictionary.
/// </summary>
/// <param name="type">The type.</param>
/// <param name = "type">The type.</param>
/// <returns></returns>
public IDictionaryAdapter GetDictionaryAdapter(Type type)
{
DictionaryTypeFactoryDelegate factory;
if (DictionaryTypes.TryGetValue(type, out factory))
if(DictionaryTypes.TryGetValue(type, out factory))
return factory();

if (type.IsGenericType && !type.IsGenericTypeDefinition)
if(type.IsGenericType && !type.IsGenericTypeDefinition)
{
var genericType = type.GetGenericTypeDefinition();

Expand All @@ -54,9 +47,8 @@ public IDictionaryAdapter GetDictionaryAdapter(Type type)
var genericArgs = type.GetGenericArguments();
var adapterType = typeof(GenericSortedListDictionaryAdapter<,>).MakeGenericType(genericArgs[0], genericArgs[1]);
return (IDictionaryAdapter)Activator.CreateInstance(adapterType);

}

if(genericType == typeof(IDictionary<,>) ||
genericType == typeof(Dictionary<,>))
{
Expand All @@ -78,5 +70,9 @@ private static IDictionaryAdapter CreateHashtableType()
{
return new HashtableDictionaryAdapter();
}

/// <summary>
/// </summary>
private delegate IDictionaryAdapter DictionaryTypeFactoryDelegate();
}
}

0 comments on commit faa64ed

Please sign in to comment.