Reputation: 7144
I am using RavenDB (NoSQL Database), and I have this class:
public class VirtualDirectory
{
public string Id { get; set; }
public string HostAlias { get; set; }
public string VirtualPath { get; set; }
public string IdentificationMethod { get; set; }
public int IsCancelled { get; set; }
}
I'm initiating a new instance of this class by:
VirtualDirectory vd = new VirtualDirectory() { HostAlias = "bla", IsCancelled = 0 };
And saving the document by:
using (var session = DbConnection.Instance.DocumentStore.OpenSession(DbName))
{
session.Store(vd);
session.SaveChanges();
}
From what i've learned about NoSQL DB documents, the documents are not bound to schema, so their structure is flexible.
My questions are: How can I save the data to my RavenDB database, without getting null values in the fields I didn't set ? After the last piece of code, my db looks like:
{
HostAlias : "bla",
VirtualPath : null,
IdentificationMethod : null,
IsCancelled : 0
}
When I get a C# object (VirtualDirectory type) that is missing some fields like:
{
HostAlias : "bla",
IsCancelled : 0
}
will it be with null values ?
Upvotes: 2
Views: 1691
Reputation: 86967
Personally, I'm not sure why you would want to avoid null properties in the DB.
I love using RavenDb and having NULL properties is totally great and helpful to me. I believe that if the property exists in the DB but not in the class (because you class 'schema' has changed) then it will just ignore that db property/value.
That said, you can tell RavenDb what to do at the time it saves something into the DB. You can customize the serialization/deserialization process. Here, you would want to create a custom ContractResolver
.
In this case you would need to check the current value to see if it's null. If so, you ignore this property.
So, this is 150% untested and just written up here after doing a quick google search. Use it as a reference.
public class IgnoreNullValuesContractResolver : DefaultRavenContractResolver
{
public IgnoreNullValuesContractResolver(bool shareCache)
: base(shareCache)
{
}
protected override JsonProperty CreateProperty(MemberInfo member,
MemberSerialization memberSerialization)
{
// Grab the property.
var property = base.CreateProperty(member, memberSerialization);
// Only write this property if the value is NOT null.
// NOTE: I have no idea if this is the correct way to retrieve
// the reflected property and it's value.
property.Writable = ((PropertyInfo) member).GetValue(member, null) != null;
return property;
}
}
Upvotes: 1
Reputation: 31
Use this to save in ravendb:
store.Conventions.CustomizeJsonSerializer = serializer => serializer.NullValueHandling=NullValueHandling.Ignore;
To fetch and return data:
var config = GlobalConfiguration.Configuration;
var settings = config.Formatters.JsonFormatter.SerializerSettings;
settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
Upvotes: 3
Reputation: 2325
I believe raven will map your object ( and all its properties ) to a Json object and save it. Hence all your objects properties will be persisted.
I would make another object - that has just the properties i need- and save that to the DB.
If you save the VirtualDirectory class to raven - you will get it back with null values as raven will instantiate your object - and then fill in the properties it has values for.
does that help ?
EDIT:
Following your comment i did a little experiment and found that using 'dynamic' you can quite easily achieve what you need. Raven will store it just fine however they are not aggregated into collections automatically.
how does that sound ? :P
Upvotes: 0