Created
September 13, 2011 14:34
-
-
Save mstarkman/1213945 to your computer and use it in GitHub Desktop.
Mongo Protected Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using Fishidy.Domain.Attributes; | |
using Fishidy.Domain.Models.Base; | |
using MongoDB.Bson.Serialization; | |
using MongoDB.Bson.Serialization.Conventions; | |
using MongoDB.Driver; | |
namespace Fishidy.Domain.Infrastructure.Mongo | |
{ | |
public static class FishidyMongo | |
{ | |
#region Private Properties | |
private static MongoDatabase Database | |
{ | |
get | |
{ | |
return MongoDatabase.Create(ConfigurationManager.ConnectionStrings["FishidyMongoDatabase"].ConnectionString); | |
} | |
} | |
#endregion | |
#region Public Methods | |
public static MongoCollection<T> GetCollection<T>() | |
where T : FishidyCollectionModel | |
{ | |
var collectionAttribute = Attribute.GetCustomAttribute(typeof(T), typeof(MongoCollectionAttribute)); | |
return Database.GetCollection<T>(collectionAttribute == null ? typeof(T).Name : ((MongoCollectionAttribute) collectionAttribute).CollectionName); | |
} | |
public static MongoCollection GetCollection(Type modelType) | |
{ | |
var collectionAttribute = Attribute.GetCustomAttribute(modelType, typeof(MongoCollectionAttribute)); | |
return Database.GetCollection(modelType, (collectionAttribute == null ? modelType.Name : ((MongoCollectionAttribute)collectionAttribute).CollectionName)); | |
} | |
public static void Initialize() | |
{ | |
SetConventionProfile(); | |
} | |
#endregion | |
#region Private Methods | |
private static void SetConventionProfile() | |
{ | |
var typesToProcess = new List<Type>(); | |
var convention = new ConventionProfile().SetMemberFinderConvention(new FishidyMongoMemberFinderConvention()); | |
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) | |
{ | |
typesToProcess.AddRange(from t in assembly.GetTypes() | |
where (t.IsSubclassOf(typeof(FishidyModel)) || t == typeof(FishidyModel)) | |
&& t.IsClass | |
select t); | |
} | |
typesToProcess.ForEach(type => BsonClassMap.RegisterConventions(convention, t => t == type)); | |
} | |
#endregion | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Reflection; | |
using MongoDB.Bson.Serialization.Conventions; | |
namespace Fishidy.Domain.Infrastructure.Mongo | |
{ | |
public class FishidyMongoMemberFinderConvention : IMemberFinderConvention | |
{ | |
// This code is copied from the CSharp-Driver project and was modified to allow for NonPublic types | |
public IEnumerable<MemberInfo> FindMembers(Type type) | |
{ | |
foreach (var fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) | |
{ | |
if (fieldInfo.IsInitOnly || fieldInfo.IsLiteral) | |
{ // we can't write | |
continue; | |
} | |
yield return fieldInfo; | |
} | |
foreach (var propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) | |
{ | |
if (!propertyInfo.CanRead || (!propertyInfo.CanWrite && type.Namespace != null)) | |
{ // we can't write or it is anonymous... | |
continue; | |
} | |
// skip indexers | |
if (propertyInfo.GetIndexParameters().Length != 0) | |
{ | |
continue; | |
} | |
// skip overridden properties (they are already included by the base class) | |
var getMethodInfo = propertyInfo.GetGetMethod(true); | |
if (getMethodInfo.IsVirtual && getMethodInfo.GetBaseDefinition().DeclaringType != type) | |
{ | |
continue; | |
} | |
yield return propertyInfo; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment