forked from JeremySkinner/FluentLinqToSql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
readme.txt
62 lines (47 loc) · 2.16 KB
/
readme.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Fluent Linq to Sql allows you to define mappings for Linq to Sql entities using a fluent interface rather than attributes or XML.
For discussions, documentation and binary releases please visit the CodePlex site:
http://fluentlinqtosql.codeplex.com
This project was inspired by Fluent NHibernate (http://fluentnhibernate.org).
A simple example:
public class CustomerMapping : Mapping<Customer> {
public CustomerMapping() {
Identity(customer => customer.Id);
Map(customer => customer.Name);
HasMany(customer => customer.Orders).OtherKey(order => order.CustomerId);
}
}
var mappingSource = new FluentMappingSource("Mydatabase");
mappingSource.AddFromAssemblyContaining<CustomerMapping>();
var dataContext = new DataContext("connection-string", mappingSource);
var customers = from c in dataContext.GetTable<Customer>() select c;
Also included in this project is a Linq to Sql based ActiveRecord implementation:
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);
}
Note that if you want to run the unit tests for this project,
you'll fist need to create a local SQL Server database called "FluentLinqToSql".
This can be achieved by running BuildDatabase.cmd
Copyright Jeremy Skinner
http://www.jeremyskinner.co.uk
Licensed under the Apache License 2
http://www.apache.org/licenses/LICENSE-2.0.html