Skip to content

Instantly share code, notes, and snippets.

@JeremySkinner
Created January 10, 2010 19:29
Show Gist options
  • Select an option

  • Save JeremySkinner/273709 to your computer and use it in GitHub Desktop.

Select an option

Save JeremySkinner/273709 to your computer and use it in GitHub Desktop.
public class Customer {
public int Id { get; set; } //assumes public property named "Id" is PK
public string Name { get; set; } //all public read/write properties auto-mapped to db cols
}
//config:
ActiveRecordConfiguration.Configure(cfg => {
cfg.ConnectToSqlServer("(local)", "mydb", "user", "pass");
cfg.MapTypesFromAssemblyContaining<Customer>();
//lots of other options...
});
//usage:
using(ContextScope.Begin()) { //in a web app, this can be transparent using a scope per request
var customer = new Customer { Name = "Jeremy" };
customer.Save();
} //changes committed on scope disposal
using(ContextScope.Begin()) {
var cust = Customer.FindById(1); //normal AR-style operations
}
//testability:
var fakeData = new[] { new Customer { Id = 1 }, new Customer { Id = 2 } };
using(Customer.Fake(fakeData)) { //replaces underlying data access with in-memory collection
var cust = Customer.FindById(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment