Skip to content

Instantly share code, notes, and snippets.

@PureKrome
Created May 5, 2015 11:43
Show Gist options
  • Save PureKrome/61f54b3061fccdfd4c61 to your computer and use it in GitHub Desktop.
Save PureKrome/61f54b3061fccdfd4c61 to your computer and use it in GitHub Desktop.
Class is persisted without the $type property
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Raven.Client.Document;
namespace ConsoleApplication5
{
class Program
{
private static IList<Person> FakePeople
{
get
{
var people = new List<Person>
{
new Soldier
{
Name = "Jane",
Equipment = new ReconEquipment
{
CamoColour = "red",
FlareCount = 3
}
},
new Medic
{
Name = "Joe",
Equipment = new HeavyWeaponsEquipment
{
CamoColour = "blue",
SandvichCount = 2
}
}
};
return people;
}
}
static void Main(string[] args)
{
var store = new DocumentStore
{
Url = "http://localhost:8080/",
DefaultDatabase = "RhinoNinjas"
};
store.Initialize();
using (var session = store.OpenSession())
{
foreach (var person in FakePeople)
{
session.Store(person);
}
session.SaveChanges();
}
}
}
public class Soldier : BasePerson<ReconEquipment>
{
}
public class Medic : BasePerson<HeavyWeaponsEquipment>
{
}
public abstract class BasePerson<TEquipment> : Person where TEquipment : IEquipment
{
public TEquipment Equipment
{
get { return (TEquipment)base.GetEquipment(); }
set { base.SetEquipment(value); }
}
}
public abstract class Person
{
private IEquipment _equipment;
public string Name { get; set; }
public IEquipment GetEquipment() { return _equipment; }
public void SetEquipment(IEquipment value) { _equipment = value; }
}
public interface IEquipment
{
string Name { get; }
string CamoColour { get; set; }
}
public class ReconEquipment : IEquipment
{
public string Name { get { return "Recon"; } }
public string CamoColour { get; set; }
public int FlareCount { get; set; }
}
public class HeavyWeaponsEquipment : IEquipment
{
public string Name { get { return "Heavy Metal"; } }
public string CamoColour { get; set; }
public int SandvichCount { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment