Two models, one document

19 views
Skip to first unread message

Luke Eccleston

unread,
Oct 21, 2016, 6:31:54 PM10/21/16
to RavenDB - 2nd generation document database

Hi 

Trying to achieve Tell dont ask and using proper encapsulation

 public class TestEntity
   
{
       
public string Id { get; set; }

       
private string _description;


       
public TestEntity(string description)
       
{
            _description
= description;
       
}
   
}

Here I am storing TestEntity within my database

I use a custom json converter to persists/load private fields

 public class Custom : DefaultRavenContractResolver
   
{
       
protected override List<MemberInfo> GetSerializableMembers(Type type)
       
{
           
var members = new List<MemberInfo>();


            members
.AddRange(type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance));
            members
.AddRange(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance));


           
return members;
       
}


       
public Custom(bool shareCache) : base(shareCache)
       
{
       
}
   
}

This works fine and I can save/load and have the correct Description on my test entity which helps my achieve encapsulation. Is there a performance issue on using reflection here to do this?


Now this is where it gets tricky, with my testing. I want to use a similar entity but with public properties (These special entities only exist in test project)

 public class TestModel
   
{
       
public string Description { get; set; }
   
}



public void Test()
       
{
           
var id = "fs";

           
using (var session = Store.OpenSession())
           
{
               
var t = new TestEntity("Lol");


                session
.Store(t);


                session
.SaveChanges();


                id
= t.Id;
           
}


           
using (var session = Store.OpenSession())
           
{
               
var t = session.Load<TestModel>(id);
           
}
       
}


Now, I want to be able to load my entity using a different class so that I can test again the public state. Any ideas how?

Oren Eini (Ayende Rahien)

unread,
Oct 21, 2016, 6:59:45 PM10/21/16
to ravendb
Your class is saved with Raven-Clr-Type, so it will fail with cast exception.
In your tests, you can tell ravendb to ignore it by setting 
DocumentCoventions.FindClrType = (_,__,___) => null;

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luke Eccleston

unread,
Oct 21, 2016, 8:13:29 PM10/21/16
to RavenDB - 2nd generation document database
Thanks :)

Any idea how I can query/index against a model with private fields?
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
Oct 21, 2016, 8:18:30 PM10/21/16
to ravendb
Using the 2nd entity
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages