I am trying to implement Repository and UnitOfWork (UOW) patterns with petaPOCO micro ORM. I have completed implementing Repository pattern for the "select" queries. I have decided to follow this post to implement the UOW pattern: PetaPoco + unit of work + repository pattern . The problem I am facing is that the db object in RepositoryBase object always comes as null
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 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;
}
public abstract class RepositoryBase<T> where T : class
{
//public Database db = new Database("Northwind");
protected IDatabase db
{
get
{
return UnitOfWork.Current;
}
}
public void Delete(T entity)
{
db.Delete(entity);
}
public void DeleteById(int id)
{
db.Delete<T>(id);
}
public IQueryable<T> GetAll()
{
return db.Query<T>("").AsQueryable();
}
public T GetById(int id)
{
return db.SingleOrDefault<T>("WHERE CustomerId = @0", id);
}
public T GetById(string id)
{
return db.SingleOrDefault<T>("WHERE CustomerId = @0", id);
}
public void Insert(T entity)
{
}
public IQueryable<T> SearchFor(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
throw new NotImplementedException();
}
}
public interface IRepository<T> where T: class
{
void Insert(T entity);
void Delete(T entity);
IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
IQueryable<T> GetAll();
T GetById(string id);
}
public interface ICustomerRepository : IRepository<Customers>
{
CustomerAndOrders GetOrdersAndCustomersByCustomerId(string id);
List<CustomerOrderHistory> CustomerOrderHistories(string customerId);
}
public class CustomerRepository : RepositoryBase<Customers>, ICustomerRepository
{
public CustomerAndOrders GetOrdersAndCustomersByCustomerId(string id)
{
var customer = this.db.SingleOrDefault<Customers>("WHERE CustomerId = @0", id);
var orders = this.db.Query<Orders>("WHERE CustomerId = @0", id).ToList();
//dynamic cust = new Customers();
var config = new MapperConfiguration(cfg => cfg.CreateMap<Customers, CustomerAndOrders>());
var mapper = config.CreateMapper();
CustomerAndOrders customerAndOrders = mapper.Map<CustomerAndOrders>(customer);
//CustomerAndOrders customerAndOrders = Mapper.Map<Customers, CustomerAndOrders>(customer);
//cust.Orders = new List<Order>();
if (customerAndOrders != null && orders != null)
{
customerAndOrders.Order = new List<Orders>();
customerAndOrders.Order.AddRange(orders);
}
return customerAndOrders;
}
public List<CustomerOrderHistory> CustomerOrderHistories(string customerId)
{
var history = new List<CustomerOrderHistory>();
//http://ift.tt/29r1n3X
//Make sure you have the ; before the execute statement
var result = db.Fetch<dynamic>(";EXEC CustOrderHist @@CustomerID = @0", customerId);
if (result != null)
{
var customerOrderHistory = new CustomerOrderHistory();
foreach (var item in result)
{
customerOrderHistory.ProductName = item.ProductName;
customerOrderHistory.Quantity = item.Total;
history.Add(customerOrderHistory);
}
}
return history;
}
}
Aucun commentaire:
Enregistrer un commentaire