-
Notifications
You must be signed in to change notification settings - Fork 0
Data Base
Arsenty Politov edited this page Mar 15, 2019
·
1 revision
Contains following abstractions that are used to organize access to database:
- IEntityStore<T> - provides querying and modification capabilities over entities of type T.
- IRepository - provides ability to get entity stores and to save changes in the database.
- IRepositoryFactory - provides ability to construct new instances of IRepository.
Also, provides a set of extensions that allow to execute querying and modification methods on repository directly.
// Query all
public async Task Example1(IRepository repository)
{
var store = repository.GetEntityStore<Product>();
var products = await store.Query().ToListAsync();
}
// Query some
public async Task Example2(IRepository repository)
{
var store = repository.GetEntityStore<Product>();
var expensiveProducts = await store.Query().Where(x => x.Price > 1000).ToListAsync();
}
// Query single
public async Task Example3(IRepository repository)
{
var store = repository.GetEntityStore<Product>();
var product = await store.Query().SingleOrDefaultAsync(x => x.Id == 42);
}
// Eager-loading related property
public async Task Example4(IRepository repository)
{
var store = repository.GetEntityStore<Product>();
var products = await store.Query("Category").ToListAsync();
}
// Eager-loading related properties
public async Task Example5(IRepository repository)
{
var store = repository.GetEntityStore<Order>();
var orders = await store.Query("Customer", "Items", "Items.Category").ToListAsync();
}
Inserting data
public async Task Example6(IRepository repository)
{
var store = repository.GetEntityStore<Product>();
var product = new Product();
// Initialze and fill product
await store.InsertAsync(product);
await repository.SaveChangesAsync();
}
Updating data
public async Task Example7(IRepository repository)
{
var store = repository.GetEntityStore<Product>();
var product = await store.Query().SingleOrDefaultAsync(x => x.Id == 42);
// Modify product
await store.UpdateAsync(product);
await repository.SaveChangesAsync();
}
Deleting data
public async Task Example8(IRepository repository)
{
var store = repository.GetEntityStore<Product>();
var product = await store.Query().SingleOrDefaultAsync(x => x.Id == 42);
await store.DeleteAsync(product);
await repository.SaveChangesAsync();
}
Using extensions
public async Task Example9(IRepository repository)
{
var newProduct = new Product();
await repository.InsertAsync(newProduct);
await repository.SaveChangesAsync();
var updatedProduct = await repository.Query<Product>("Category").SingleOrDefaultAsync(x => x.Id == 42);
await repository.UpdateAsync(updatedProduct);
await repository.SaveChangesAsync();
var deletedProduct = await repository.Query<Product>().SingleOrDefaultAsync(x => x.Id == 42);
await repository.DeleteAsync(deletedProduct);
await repository.SaveChangesAsync();
}