EF6-UnitOfWork 0.4.0

UnitOfWork pattern by EF6

Install-Package EF6-UnitOfWork -Version 0.4.0
dotnet add package EF6-UnitOfWork --version 0.4.0
<PackageReference Include="EF6-UnitOfWork" Version="0.4.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EF6-UnitOfWork --version 0.4.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

How to

  • 在外部程式 ( service, api ) 取得 QueryRepository

    透過 IUnitOfWork 介面方法 QueryRepository<TEntity>() 取得該實體的查詢用 IQueryRepository
// use factory get IUnitOfWork
IUnitOfWork uow = factory.UnitOfWork;
IBudgetRepository budgetResp = uow.QueryRepository<Budget>();
//use connection string
string connectionStr = "..."
IUnitOfWork uow = new UnitOfWorkDb(connectionStr);
IBudgetRepository budgetResp = uow.QueryRepository<Budget>();
  • 透過 Model Repository 查詢資料庫的實體

透過 IQueryRepository 介面的 Get()GetAll() 方法篩選出需要的資料。

//example:BudgetService
public Budget GetBudget(int id)
{
	//取得符合條件一筆 Budget
	return budgetRepository.Get(bu => bu.BdgtId == id);
}

public IEnumerable<Budget> GetBudgets()
{
	//取得符合條件的多筆 Budget
	return budgetRepository.GetAll().Where(bu => bu.BdgtDocVoid != "Y");
}
  • 透過 Model Repository 新增、刪除、修改資料庫的實體

透過 IRepository 介面的 Create()Update()Delete() 方法,在變更結束後呼叫 IUnitOfWork 介面方法 SaveChanges() 來儲存此次的變更。

//example:BudgetService
public void Create(int id)
{
	var newBudget = new Budget();
	budgetRepository.Create(newBudget);
	uow.SaveChanges();
}

public void Update(int id)
{
	//更新指定 id 的 Budget
	var budget = budgetRepository.Get(bd=>bd.BdgtId = id);
	/*
	...  do some change
	*/
	budgetRepository.Update(budget);
	uow.SaveChanges();
}

public void UpdateSomeValue(int id,int vender)
{
	//更新指定 id 的 Budget 的 VenderId 欄位
	budgetRepository.Update(new object[] { id }, bdgt => bdgt.VenderId , vender);
	uow.SaveChanges();
}

public void Delete(int id)
{
	//刪除指定 id 的 Budget
	var budget = budgetRepository.Get(bd=>bd.BdgtId = id);
	budgetRepository.Delete(budget);
	uow.SaveChanges();
}
  • Begin transation

使用 IUnitOfWork 介面方法 BeginTransaction()

//example:BudgetService
public void UpdateSomething(int id)
{
	using (var tr = uow.BeginTransaction())
	{
		try
		{
			/*
			...  do some change
			*/

			uow.SaveChanges();
			tr.Commit();
		}
		catch (Exception)
		{
			tr.Rollback();
			throw;
		}
	}
}

How to

  • 在外部程式 ( service, api ) 取得 QueryRepository

    透過 IUnitOfWork 介面方法 QueryRepository<TEntity>() 取得該實體的查詢用 IQueryRepository
// use factory get IUnitOfWork
IUnitOfWork uow = factory.UnitOfWork;
IBudgetRepository budgetResp = uow.QueryRepository<Budget>();
//use connection string
string connectionStr = "..."
IUnitOfWork uow = new UnitOfWorkDb(connectionStr);
IBudgetRepository budgetResp = uow.QueryRepository<Budget>();
  • 透過 Model Repository 查詢資料庫的實體

透過 IQueryRepository 介面的 Get()GetAll() 方法篩選出需要的資料。

//example:BudgetService
public Budget GetBudget(int id)
{
	//取得符合條件一筆 Budget
	return budgetRepository.Get(bu => bu.BdgtId == id);
}

public IEnumerable<Budget> GetBudgets()
{
	//取得符合條件的多筆 Budget
	return budgetRepository.GetAll().Where(bu => bu.BdgtDocVoid != "Y");
}
  • 透過 Model Repository 新增、刪除、修改資料庫的實體

透過 IRepository 介面的 Create()Update()Delete() 方法,在變更結束後呼叫 IUnitOfWork 介面方法 SaveChanges() 來儲存此次的變更。

//example:BudgetService
public void Create(int id)
{
	var newBudget = new Budget();
	budgetRepository.Create(newBudget);
	uow.SaveChanges();
}

public void Update(int id)
{
	//更新指定 id 的 Budget
	var budget = budgetRepository.Get(bd=>bd.BdgtId = id);
	/*
	...  do some change
	*/
	budgetRepository.Update(budget);
	uow.SaveChanges();
}

public void UpdateSomeValue(int id,int vender)
{
	//更新指定 id 的 Budget 的 VenderId 欄位
	budgetRepository.Update(new object[] { id }, bdgt => bdgt.VenderId , vender);
	uow.SaveChanges();
}

public void Delete(int id)
{
	//刪除指定 id 的 Budget
	var budget = budgetRepository.Get(bd=>bd.BdgtId = id);
	budgetRepository.Delete(budget);
	uow.SaveChanges();
}
  • Begin transation

使用 IUnitOfWork 介面方法 BeginTransaction()

//example:BudgetService
public void UpdateSomething(int id)
{
	using (var tr = uow.BeginTransaction())
	{
		try
		{
			/*
			...  do some change
			*/

			uow.SaveChanges();
			tr.Commit();
		}
		catch (Exception)
		{
			tr.Rollback();
			throw;
		}
	}
}

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.4.0 375 6/1/2018
0.2.0 335 6/1/2018