Skip to content

Instantly share code, notes, and snippets.

@jalchr
Created April 1, 2013 15:13
Show Gist options
  • Save jalchr/5285484 to your computer and use it in GitHub Desktop.
Save jalchr/5285484 to your computer and use it in GitHub Desktop.
RavenDb v.2544 A bug in the deserialization model ... somehow related to the way we name objects....
using System;
using System.Collections.Generic;
namespace RavenApp
{
using System.Linq;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Imports.Newtonsoft.Json;
class Program
{
static void Main(string[] args)
{
var store = new DocumentStore() { ConnectionStringName = "RavenServer" };
store.Initialize();
store.Conventions.CustomizeJsonSerializer = serializer =>
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
store.Conventions.FindFullDocumentKeyFromNonStringIdentifier = (id, type, allowNull) => id.ToString();
IndexCreation.CreateIndexes(typeof(Program).Assembly, store);
var code = "code";
using (var session = store.OpenSession())
{
var agency = new AgencyDb();
agency.Id = Guid.NewGuid();
agency.Name = "my-name";
agency.Code = code;
var country = new AgencyCountryDb();
country.AgencyId = agency.Id;
country.Country = "The-Country";
agency.Countries = new AgencyCountryDb[] { country };
session.Store(agency);
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var query = session.Query<AgencyDb, Agency_Entity>().Customize(x => x.WaitForNonStaleResultsAsOfLastWrite());
var agency = query.Where(x => x.Code == code).As<Agency>().SingleOrDefault();
System.Diagnostics.Debug.Assert(agency != null);
}
}
}
class AgencyDb
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public AgencyCountryDb[] Countries { get; set; }
}
class AgencyCountryDb
{
public string Country { get; set; }
public Guid AgencyId { get; set; }
}
class Agency
{
private List<AgencyCountry> _countries;
public Guid Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public AgencyCountry[] Countries
{
get { return _countries.ToArray(); }
set
{
_countries = new List<AgencyCountry>(value);
}
}
}
class AgencyCountry
{
public string Country { get; set; }
public Agency Agency { get; set; }
}
class Agency_Entity : AbstractIndexCreationTask<AgencyDb>
{
public Agency_Entity()
{
Map = agencies => from agency in agencies
select new
{
agency.Code,
};
TransformResults = (database, agencies) => from agency in agencies
select new // AgencyDb
{
agency.Id,
agency.Name,
agency.Code,
Countries = from com in agency.Countries
select new // AgencyCountry
{
com.Country,
Agency = agency
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment