编程语言
首页 > 编程语言> > c# – PetaPoco工作单元库模式

c# – PetaPoco工作单元库模式

作者:互联网

下面的代码似乎使用这个:

var unitOfWorkProvider = new PetaPocoUnitOfWorkProvider();
var repository = new FakeRepository();
var fake = new Fake
{     
    // etc.
};

using (var uow = unitOfWorkProvider.GetUnitOfWork("BlaConnectionString"))
{
    repository.Insert(uow, fake);
    uow.Commit();
}

最终可能会进入服务层.我将不胜感激任何改进此代码的反馈.

public interface IUnitOfWork : IDisposable
{
    void Commit();
    Database Database { get; }
}

public interface IUnitOfWorkProvider
{
    IUnitOfWork GetUnitOfWork(string connectionString);
}

public class PetaPocoUnitOfWorkProvider : IUnitOfWorkProvider
{
    public IUnitOfWork GetUnitOfWork(string connectionString)
    {
        return new PetaPocoUnitOfWork(connectionString);
    }
}

public interface IRepository<T>
{
    void Insert(IUnitOfWork unitOfWork, T entity);
    void Update(IUnitOfWork unitOfWork, T entity);
    void Delete(IUnitOfWork unitOfWork, T entity);
    T Fetch(IUnitOfWork unitOfWork, long uid);
}

public class PetaPocoUnitOfWork : IUnitOfWork
{
    private readonly Transaction _petaTransaction;
    private readonly Database _database;

    public PetaPocoUnitOfWork(string connectionString)
    {
        _database = new Database(connectionString);
        _petaTransaction = new Transaction(_database);
    }

    public void Dispose()
    {
        _petaTransaction.Dispose();
    }

    public Database Database
    {
        get { return _database; }
    }

    public void Commit()
    {
        _petaTransaction.Complete();
    }
}

public class FakeRepository : IRepository<Fake>
{
    public void Insert(IUnitOfWork unitOfWork, Fake entity)
    {
        unitOfWork.Database.Save(entity);
    }

    public void Update(IUnitOfWork unitOfWork, Fake entity)
    {
        unitOfWork.Database.Update(entity);
    }

    public void Delete(IUnitOfWork unitOfWork, Fake entity)
    {
        unitOfWork.Database.Delete(entity);
    }

    public FakeJobFact Fetch(IUnitOfWork unitOfWork, long uid)
    {
        return unitOfWork.Database.Fetch<Fake>("SELECT * FROM Fakes WHERE [FakeId] = @0", uid).FirstOrDefault();
    }
}

PS:

我根据@Plebsori当前的答案调整了代码:

public abstract class BaseRepository<T>
{
    protected IDatabase Database
    {
        get
        {
        return UnitOfWork.Current;
        }
    }

    public void Insert(T entity)
    {
        Database.Save(entity);
    }

    public void Update(T entity)
    {
        Database.Update(entity);
    }

    public void Delete(T entity)
    {
        Database.Delete(entity);
    }
}

public interface IRepository<T>
{
    void Insert(T entity);
    void Update(T entity);
    void Delete(T entity);
    T Fetch(long uid);
}

public interface IUnitOfWork : IDisposable
{
    void Commit();
    Database Database { get; }
}

public interface IUnitOfWorkProvider
{
    IUnitOfWork GetUnitOfWork(string connectionString);
}

public class PetaPocoUnitOfWork : IUnitOfWork
{
    private readonly Transaction _petaTransaction;
    private readonly Database _database;

    public PetaPocoUnitOfWork(string connectionString)
    {
        _database = new Database(connectionString);
        _petaTransaction = new Transaction(_database);
    }

    public void Dispose()
    {
        UnitOfWork.Current = null;
        _petaTransaction.Dispose();
    }

    public Database Database
    {
        get { return _database; }
    }

    public void Commit()
    {
        _petaTransaction.Complete();
    }
}

public class PetaPocoUnitOfWorkProvider : IUnitOfWorkProvider
{
    public IUnitOfWork GetUnitOfWork(string connectionString)
    {
        if (UnitOfWork.Current != null)
        {
        throw new InvalidOperationException("Existing unit of work.");
        }

        var petaPocoUnitOfWork = new PetaPocoUnitOfWork(connectionString);
        UnitOfWork.Current = petaPocoUnitOfWork.Database;
        return petaPocoUnitOfWork;
    }
}

public static class UnitOfWork
{
    [ThreadStatic] public static IDatabase Current;
}

解决方法:

您可能会或可能不会喜欢,但这是我如何从界面中删除工作单元和工作单元的传递.

var unitOfWorkProvider = new PetaPocoUnitOfWorkProvider();
var repository = new FakeRepository();
var fake = new Fake
{     
    // etc.
};

using (var uow = unitOfWorkProvider.GetUnitOfWork("BlaConnectionString"))
{
    repository.Insert(fake);
    uow.Commit();
}

public interface IUnitOfWorkProvider
{
    IUnitOfWork GetUnitOfWork(string connectionString);
}

public static class UnitOfWork
{
    [ThreadStatic]
    public static IUnitOfWork Current { get; set; }
}

public class PetaPocoUnitOfWorkProvider : IUnitOfWorkProvider
{
    public IUnitOfWork GetUnitOfWork(string connectionString)
    {
        if (UnitOfWork.Current != null) 
        {
           throw new InvalidOperationException("Existing unit of work.");
        }
        UnitOfWork.Current = new PetaPocoUnitOfWork(connectionString);
        return UnitOfWork.Current;
    }
}

public interface IRepository<T>
{
    void Insert(T entity);
    void Update(T entity);
    void Delete(T entity);
    T Fetch(long uid);
}

public class PetaPocoUnitOfWork : IUnitOfWork
{
    private readonly Transaction _petaTransaction;
    private readonly Database _database;

    public PetaPocoUnitOfWork(string connectionString)
    {
        _database = new Database(connectionString);
        _petaTransaction = new Transaction(_database);
    }

    public void Dispose()
    {
        UnitOfWork.Current = null;
        _petaTransaction.Dispose();
    }

    public Database Database
    {
        get { return _database; }
    }

    public void Commit()
    {
        _petaTransaction.Complete();
    }
}

public abstract class BaseRepository<T> : IRepository<T>
{
    protected IDatabase Db
    {
        get
        {
            return UnitOfWork.Current;
        }
    }
}

public class FakeRepository : BaseRepository<T>
{
    public void Insert(Fake entity)
    {
        Db.Save(entity);
    }

    public void Update(Fake entity)
    {
        Db.Update(entity);
    }

    public void Delete(Fake entity)
    {
        Db.Delete(entity);
    }

    public FakeJobFact Fetch(long uid)
    {
        return Db.Fetch<Fake>("SELECT * FROM Fakes WHERE [FakeId] = @0", uid).FirstOrDefault();
    }
}

标签:c,repository,unit-of-work,petapoco
来源: https://codeday.me/bug/20190516/1116094.html