Re: Ravendb IReadOnlyCollection<> and private backing field

135 views
Skip to first unread message

Kijana Woodard

unread,
Apr 17, 2013, 4:21:46 PM4/17/13
to rav...@googlegroups.com
- You can change _tires to a private property and it will be serialized automatically (private fields are not) and initialize in the constructor.
- You can add [Raven.Imports.Newtonsoft.Json] to the public property and it won't be serialized.

Assuming the first option won't work for you.
- You could try adding [Raven.Imports.Newtonsoft.JsonProprty] to _tires.

You could customize the contract resolver
http://ravendb.net/docs/2.0/client-api/advanced/custom-serialization

On Wednesday, April 17, 2013 12:27:04 PM UTC-5, Daan Le Duc wrote:
Hi,

I have a question related to IReadOnlyCollection<> and backing fields. Say i have the following situation:

    public class Car
    {
        private ICollection<Tire> _tires = new HashSet<Tire>();

        public IReadOnlyCollection<Tire> Tires
        {
            get { return new ReadOnlyCollection<Tire>(_tires.ToList()); }
        }

        public void AddTire(Tire tire)
        {
            _tires.Add(tire);
        }
    }

What i want is to be able to use indexes on Tires field and serialize and deserialize the value from and into _tires.

With NHibernate i am able to do this by specifying the access, for example in Fluent NHibernate it will be:

HasMany(x => x.Tires).Access.CamelCaseField(Prefix.Underscore);

I have looked into JsonContractResolver but lack the knowledge to find it out.

Does some have suggestions how to tackle this situation?
Reply all
Reply to author
Forward
0 new messages