Quantcast
Channel: URF - Unit of Work & (extensible/generic) Repositories Framework
Viewing all articles
Browse latest Browse all 1539

Updated Wiki: Documentation

$
0
0
For questions and support please contact Le on Twitter @LeLong37

quickstart

Additional References

v2.0
http://blog.longle.net/2013/10/09/upgrading-to-async-with-entity-framework-mvc-odata-asyncentitysetcontroller-kendo-ui-glimpse-generic-unit-of-work-repository-framework-v2-0

v1.0
http://blog.longle.net/2013/05/11/genericizing-the-unit-of-work-pattern-repository-pattern-with-entity-framework-in-mvc

Quick Samples with LINQPad
*Initializing of UnitofWork and Repositories would be facilitated by DI & IoC in real world implementation.

// Simple query for all categories
// Initialize a repository (obviously this would be injected in real world implementation)var categoryRepository = new Repository<Category>(this);

var categories1 = categoryRepository
	.Query()
	.Select();

categories1.Dump();

// Simple query, with clause and sorting
var categories2 = categoryRepository
	.Query(c => c.CategoryID < 5)
	.OrderBy(x => x.OrderBy(y => y.CategoryName)
		.ThenBy(w => w.CategoryID))
	.Select();
	
categories2.Dump();

// Simple query with deep loading graph and projection
var categories = categoryRepository
	.Query(x => x.CategoryName == "Produce")
		.Include(x => x.Products.Select(p => p.Supplier))
		.Include(x => x.Products.Select(p => p.OrderDetails))
	.Select(x => new {
			CategoryId = x.CategoryID,
			ProductCount = x.Products.Count(),
			ItemsSold = x.Products.SelectMany(p => p.OrderDetails).Sum(p => p.Quantity)
		});
	
categories.Dump();

// Accessing repositories from UnitOfWork, with async (EF6 only)
// (obviously this would be injected in real world implementation)
IUnitOfWorkAsync unitOfWork = new UnitOfWork(this);	
var product1 = await unitOfWork.RepositoryAsync<Product>().FindAsync(2);
product1.Dump();

// Built in transaction handling in UnitOfWork
var productRepository = new Repository<Product>(this);
var product2 = await productRepository.FindAsync(7);
product2.Dump();

// Begin transaction
unitOfWork.BeginTransaction();

try{
	product2.ProductName = "Chai4";
	product2.ObjectState = ObjectState.Modified;
	
	// <Do other transactions here>
	
	productRepository.Update(product2);
	
	var changes = await unitOfWork.SaveChangesAsync();
	changes.Dump("changes");
	
	// Commit Transaction
	unitOfWork.Commit();
}
catch{
	// Rollback transaction
	unitOfWork.Rollback();
}

product2 = await productRepository.FindAsync(7);
product2.Dump();

// Async hybrid deletes
var isDeleted = await productRepository.DeleteAsync(2);

Viewing all articles
Browse latest Browse all 1539

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>