Skip to content

Instantly share code, notes, and snippets.

@markrendle
Created October 12, 2012 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markrendle/3879256 to your computer and use it in GitHub Desktop.
Save markrendle/3879256 to your computer and use it in GitHub Desktop.
Simple.Data + Impromptu-Interface interfaces
static class DatabaseFactory
{
public static IDatabase Create()
{
Database.UseMockAdapter(CreateMockAdapter());
// This single line of code applies all these interfaces
return Impromptu.ActLike<IDatabase>(Database.Open());
}
private static InMemoryAdapter CreateMockAdapter()
{
var inmem = new InMemoryAdapter(StringComparer.OrdinalIgnoreCase);
inmem.SetAutoIncrementKeyColumn("Customers", "Id");
inmem.SetAutoIncrementKeyColumn("Employees", "Id");
inmem.Join.Master("Customers", "Id", "Employees").Detail("Employees", "EmployerId", "Employer");
return inmem;
}
}
public interface IDatabase
{
ICustomers Customers { get; }
IEmployees Employees { get; }
}
public interface ICustomers
{
ICustomer Get(int id);
[UseNamedArgument]
ICustomer Insert(string name = null, string location = null);
}
public interface IEmployees
{
[UseNamedArgument]
IEmployee Insert(string name = null, int employerId = 0);
}
public interface ICustomer
{
int Id { get; set; }
string Name { get; set; }
string Location { get; set; }
IList<IEmployee> Employees { get; }
}
public interface IEmployee
{
int Id { get; set; }
int EmployerId { get; set; }
ICustomer Employer { get; }
string Name { get; set; }
}
[TestFixture]
public class CustomerTest
{
[Test]
public void TestInsert()
{
var db = DatabaseFactory.Create();
var customer = db.Customers.Insert(name: "Megadodo", location: "Ursa Minor Beta");
Assert.AreEqual(1, customer.Id);
Assert.AreEqual("Megadodo", customer.Name);
Assert.AreEqual("Ursa Minor Beta", customer.Location);
}
[Test]
public void TestGet()
{
var db = DatabaseFactory.Create();
db.Customers.Insert(name: "Megadodo", location: "Ursa Minor Beta");
var customer = db.Customers.Get(1);
Assert.AreEqual(1, customer.Id);
Assert.AreEqual("Megadodo", customer.Name);
Assert.AreEqual("Ursa Minor Beta", customer.Location);
}
[Test]
public void TestLazyLoadDetail()
{
var db = DatabaseFactory.Create();
var customer = db.Customers.Insert(name: "Megadodo", location: "Ursa Minor Beta");
db.Employees.Insert(name: "Ford Prefect", employerId: customer.Id);
db.Employees.Insert(name: "Zarniwoop", employerId: customer.Id);
customer = db.Customers.Get(customer.Id);
Assert.AreEqual(2, customer.Employees.Count);
// Make sure we get the same list every time we access the property
customer.Employees.RemoveAt(0);
Assert.AreEqual(1, customer.Employees.Count);
}
[Test]
public void TestLazyLoadMaster()
{
var db = DatabaseFactory.Create();
var customer = db.Customers.Insert(name: "Megadodo", location: "Ursa Minor Beta");
var employee = db.Employees.Insert(name: "Ford Prefect", employerId: customer.Id);
Assert.AreEqual("Megadodo", employee.Employer.Name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment