Skip to content

Instantly share code, notes, and snippets.

@DerAlbertCom
Created July 22, 2011 14:34
Show Gist options
  • Save DerAlbertCom/1099574 to your computer and use it in GitHub Desktop.
Save DerAlbertCom/1099574 to your computer and use it in GitHub Desktop.
Private Property Setter Serialization with RavenDb.
using Raven.Client;
using Raven.Client.Document;
namespace Aperea.Infrastructure.Data
{
public class DocumentSessionFactory : IDocumentSessionFactory
{
readonly DocumentStore _documentStore;
public DocumentSessionFactory()
{
_documentStore = new DocumentStore();
_documentStore.ConnectionStringName = "RavenDb";
_documentStore.Conventions.JsonContractResolver = new PrivateSetterContractResolver(true);
_documentStore.Initialize();
}
public IDocumentSession CreateDocumentSession()
{
return _documentStore.OpenSession("Foobar");
}
}
}
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Serialization;
namespace Aperea.Infrastructure.Data
{
public class PrivateSetterContractResolver : DefaultContractResolver
{
public PrivateSetterContractResolver(bool shareCache):base(shareCache)
{
DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
}
protected override System.Collections.Generic.List<MemberInfo> GetSerializableMembers(System.Type objectType)
{
return base.GetSerializableMembers(objectType).Where(m => m.MemberType == MemberTypes.Property).ToList();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment