Skip to content

Instantly share code, notes, and snippets.

@jesuslpm
Created October 8, 2012 11:03
Show Gist options
  • Save jesuslpm/3851961 to your computer and use it in GitHub Desktop.
Save jesuslpm/3851961 to your computer and use it in GitHub Desktop.
Modeling localization in RavenDB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Client.Document;
using Raven.Client.Embedded;
using Xunit;
namespace Raven.Tests.Localization
{
public interface ILocalizableProperties
{
string Id { get; }
string MainEntityId { get; set; }
string LanguageCode { get; set; }
}
public abstract class LocalizableProperties : ILocalizableProperties
{
public string Id
{
get { return this.MainEntityId + "/" + this.LanguageCode; }
}
public string MainEntityId { get; set; }
public string LanguageCode { get; set; }
}
public class Product
{
public class LocalizedProperties : LocalizableProperties
{
public string Name { get; set; }
public string Description { get; set; }
}
public string Id { get; set; }
public decimal UnitPrice { get; set; }
}
public class LocalizedProduct : Product
{
public string Id { get; set; }
public decimal? UnitPrice { get; set; }
public string LanguageCode { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class LocalizedProducts_Search : Raven.Client.Indexes.AbstractMultiMapIndexCreationTask<LocalizedProduct>
{
public LocalizedProducts_Search()
{
this.AddMap<Product>(products => from p in products
select new
{
Id = p.Id,
UnitPrice = (decimal?) p.UnitPrice,
Name = (string)null,
Description = (string)null,
LanguageCode = (string)null
});
this.AddMap<Product.LocalizedProperties>(properties => from p in properties
select new
{
Id = p.MainEntityId,
UnitPrice = (decimal?) null,
Name = p.Name,
Description = p.Description,
LanguageCode = p.LanguageCode
});
this.Reduce = results => from result in results
group result by result.Id into g
let product = new { Id = g.Key, UnitPrice = g.Select(x => x.UnitPrice).FirstOrDefault(x => x != null), }
from l in g
where l.LanguageCode != null
select new
{
Id = product.Id,
UnitPrice = product.UnitPrice,
Name = l.Name,
Description = l.Description,
LanguageCode = l.LanguageCode
};
}
}
public class LocalizedTest : IDisposable
{
DocumentStore store;
public LocalizedTest()
{
store = new EmbeddableDocumentStore { RunInMemory = true };
store.Initialize();
}
public void Dispose()
{
if (store != null) store.Dispose();
}
[Fact]
public void Test()
{
new LocalizedProducts_Search().Execute(this.store);
using (var session = store.OpenSession())
{
var product1 = new Product
{
Id = "Products/1",
UnitPrice = 10m
};
var localized1_1 = new Product.LocalizedProperties
{
MainEntityId = "Products/1",
LanguageCode = "en",
Name = "Cheese"
};
var localized1_2 = new Product.LocalizedProperties
{
MainEntityId = "Products/1",
LanguageCode = "es",
Name = "Queso"
};
var product2 = new Product
{
Id = "Products/2",
UnitPrice = 1.2m,
};
var localized2_1 = new Product.LocalizedProperties
{
MainEntityId = "Products/2",
LanguageCode = "en",
Name = "Milk"
};
var localized2_2 = new Product.LocalizedProperties
{
MainEntityId = "Products/2",
LanguageCode = "es",
Name = "Leche"
};
var product3 = new Product
{
Id = "Products/3",
UnitPrice = 0.8m
};
var localized3_1 = new Product.LocalizedProperties
{
MainEntityId = "Products/3",
LanguageCode = "en",
Name = "Bread"
};
var localized3_2 = new Product.LocalizedProperties
{
MainEntityId = "Products/3",
LanguageCode = "es",
Name = "Pan"
};
session.Store(product1);
session.Store(product2);
session.Store(product3);
session.Store(localized1_1);
session.Store(localized1_2);
session.Store(localized2_1);
session.Store(localized2_2);
session.Store(localized3_1);
session.Store(localized3_2);
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var query = session.Query<LocalizedProduct, LocalizedProducts_Search>()
.Customize(x => x.WaitForNonStaleResults())
.Where(x => x.LanguageCode == "es")
.OrderBy(x => x.Name);
var products = query.ToList();
var statistics = this.store.DatabaseCommands.GetStatistics();
products = query.ToList();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment