EF6-UnitOfWork 0.4.0

dotnet add package EF6-UnitOfWork --version 0.4.0
NuGet\Install-Package EF6-UnitOfWork -Version 0.4.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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
#r "nuget: EF6-UnitOfWork, 0.4.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EF6-UnitOfWork as a Cake Addin
#addin nuget:?package=EF6-UnitOfWork&version=0.4.0

// Install EF6-UnitOfWork as a Cake Tool
#tool nuget:?package=EF6-UnitOfWork&version=0.4.0

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;
		}
	}
}
Product Compatible and additional computed target framework versions.
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.0 1,092 6/1/2018
0.2.0 940 6/1/2018