Reputation: 1548
Hi I'm used to have the following entity
public class Foo { private IList<Bar> _bars; public IEnumerable<Bar> Bars { get { return bars; } } public void Add(Bar bar) { /* Validation logic here */ _bars.Add(bar); } }
I'm suspecting this won't work with RavenDb or am I'm wrong? Can I keep my collection with Bars protected from outside manipulating (in other words not allowing foo.Bars.Add(bar);)
Upvotes: 1
Views: 81
Reputation: 241525
A private setter on an automatic property is the easiest and most readable way without doing anything special.
public class Foo
{
public IEnumerable<Bar> Bars { get; private set; }
public void Add(Bar bar)
{
Bars.Add(bar);
}
}
Another way would be with attributes:
// pick one or the other
using Newtonsoft.Json // on 1.0
using Raven.Imports.Newtonsoft.Json // on 2.0
...
public class Foo
{
[JsonProperty(PropertyName = "Bars")]
private IList<Bar> _bars;
[JsonIgnore]
public IEnumerable<Bar> Bars { get { return bars; } }
public void Add(Bar bar)
{
_bars.Add(bar);
}
}
Upvotes: 0
Reputation: 1548
I found the solution to use two properties.
public IEnumerable Bars { get { return InnerBars; } }
private List InnerBars { get; set; }}
Upvotes: 1