저는이 훌륭한 마이크로 도구 (petapoco)를 처음 접했고 petapoco를 사용하여 UoW 및 저장소 패턴을 구현하는 방법을 알고 있습니다. 웹 프로젝트에서. 나는 몇몇 기사를 읽었지 만, 좋은 아이디어를 디자인하거나 구현하는 방법이 없습니다. 어떤 사람이 약간의 제작 예제를 제공하거나 이것을 달성하기 위해 지시 할 수 있습니까?petapoco를 사용하여 Unit of Work 및 저장소 패턴 (트랜잭션 포함)을 잘못 구현했거나 잘못 구현 했습니까
여기 내 생각과 pattial 구현 코드입니다, 제발 내가 잘못하면 조언이나 의견을 주시기 바랍니다.
public interface IUnitOfWork
{
void StartNew();
void Commit();
void Rollback();
}
public class PetaPocoUnitOfWork : IUnitOfWork
{
private PetaPoco.Database _db = null;
public PetaPocoUnitOfWork(PetaPoco.Database db)
{
_db = db;
}
public void StartNew()
{
_db.BeginTransaction();
}
public void Commit()
{
_db.CompleteTransaction();
}
public void Rollback()
{
_db.AbortTransaction();
}
}
public class UnitOfWorkFactory
{
public static IUnitOfWork GetInstance()
{
return new PetaPocoUnitOfWork(Project.Core.Domain.ProjectDb.GetInstance());
}
}
interface IRepository<T>
{
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
List<T> FetchAll();
List<T> FetchAll(int startIndex, int endIndex, int count);
T Fetch(int uid);
void SaveChanges();
}
public class TireRepository : IRepository<Tire>
{
private IUnitOfWork _uow = null;
public TireRepository(IUnitOfWork uow)
{
_uow = uow;
}
public void Insert(Tire entity)
{
var db = ProjectDb.GetInstance();
db.Save(entity);
}
public void Update(Tire entity)
{
throw new NotImplementedException();
}
public void Delete(Tire entity)
{
var db = ProjectDb.GetInstance();
}
public List<Tire> FetchAll()
{
throw new NotImplementedException();
}
public List<Tire> FetchAll(int startIndex, int endIndex, int count)
{
throw new NotImplementedException();
}
public Tire Fetch(int id)
{
throw new NotImplementedException();
}
public void SaveChanges()
{
_uow.Commit();
}
}
다음은 간단한 테스트 케이스이며, 그것은 아마 서비스 계층 전화의 사용이다.
[TestMethod()]
public void InsertTest()
{
IUnitOfWork uow = Xyz.Core.UnitOfWorkFactory.GetInstance();
TireRepository target = new TireRepository(uow);
Tire entity = new Tire();
entity.Description = "ABCD";
entity.Manufacturer = 1;
entity.Spec = "18R/V";
try
{
target.Insert(entity);
target.SaveChanges();
}
catch
{
uow.Rollback();
}
}
autoFac을 Ioc 솔루션으로 사용하고 http 요청 당 인스턴스를 리포지토리 개체에 주입합니다.
이 코드가 잘못되었거나 나쁘면 약간의 의견이나 조언을 보내주십시오. 많은 감사합니다.