Skip to content

Instantly share code, notes, and snippets.

@fitzchak
Forked from jesuslpm/LocalizableEntities.cs
Created October 10, 2012 08:05
Show Gist options
  • Save fitzchak/3863944 to your computer and use it in GitHub Desktop.
Save fitzchak/3863944 to your computer and use it in GitHub Desktop.
Proposed model for localizable entities
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Client.Document;
using Raven.Client.Embedded;
using Xunit;
using Raven.Json.Linq;
using System.Diagnostics;
using Raven.Client;
namespace Raven.Tests
{
public interface ILocalizableEntity<TLocalizedProperties>
{
Dictionary<string, TLocalizedProperties> LocalizedProperties { get; set; }
}
public class Product : ILocalizableEntity<Product.Localized>
{
public class Localized
{
public string Name { get; set; }
public string Description { get; set; }
}
public string Id { get; set; }
public decimal UnitPrice { get; set; }
public Dictionary<string, Localized> LocalizedProperties { get; set; }
}
public class ProductViewModel
{
public string Id { get; set; }
public decimal UnitPrice { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ProductViewModel();
public ProductViewModel(Product product, string languageCode)
{
this.Id = product.Id;
this.UnitPrice = product.UnitPrice;
this.Name = product.LocalizedProperties[languageCode].Name;
this.Description = product.LocalizedProperties[languageCode].Description;
}
}
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()
{
using (var session = store.OpenSession())
{
var product1 = new Product
{
Id = "Products/1",
UnitPrice = 10m,
LocalizedProperties = new Dictionary<string, Product.Localized>
{
{"en", new Product.Localized { Name = "Cheese" }},
{"es", new Product.Localized { Name = "Queso" }}
}
};
var product2 = new Product
{
Id = "Products/2",
UnitPrice = 1.2m,
LocalizedProperties = new Dictionary<string, Product.Localized>
{
{"en", new Product.Localized { Name = "Milk" }},
{"es", new Product.Localized { Name = "Leche" }}
}
};
var product3 = new Product
{
Id = "Products/3",
UnitPrice = 0.8m,
LocalizedProperties = new Dictionary<string, Product.Localized>
{
{"en", new Product.Localized { Name = "Bread" }},
{"es", new Product.Localized { Name = "Pan" }}
}
};
session.Store(product1);
session.Store(product2);
session.Store(product3);
session.SaveChanges();
var json = RavenJObject.FromObject(product1).ToString();
}
using (var session = store.OpenSession())
{
var query = session.Query<Product>()
.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
.Customize(x => ((IDocumentQuery<Product>)x).OrderBy("LocalizedProperties.es.Name"));
Debug.WriteLine(query.ToString());
var products = query.ToList();
var productsViewModel = products.Select(p => new ProductViewModel(p, "es"));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment