Tôi đang cố gắng triển khai mẫu lưu trữ bằng ado.net do giới hạn nền tảng.Thực hiện mẫu lưu trữ chung bằng cách sử dụng ado.net
public interface IGenericRepository<T> : IDisposable where T : class
{
IQueryable<T> GetAll();
IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Delete(T entity);
void Edit(T entity);
void Save();
}
Làm thế nào để hoàn tất lớp trừu tượng sau đây ...?
public abstract class GenericRepository<C, T> :
IGenericRepository<T>
where T : class
where C : IDbDataAdapter, new()
{
private C dbDataAdapter = new C();
protected C DB
{
get { return dbDataAdapter; }
set { dbDataAdapter = value; }
}
public virtual IQueryable<T> GetAll()
{
DataTable dt;
dbDataAdapter.fill(dt);
IQueryable<T> query = dt....?;
return query;
}
public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
IQueryable<T> query = dbDataAdapter.???Set<T>???().Where(predicate);
return query;
}
Cập nhật:
tôi sẽ thực hiện các kho lưu trữ miền quy định sau đó bằng cách cố hữu những giao diện hai/lớp.
public class FooRepository :
GenericRepository<FooBarEntities, Foo>, IFooRepository {
public Foo GetSingle(int fooId) {
var query = GetAll().FirstOrDefault(x => x.FooId == fooId);
return query;
}
}
đồng ý. Có rất nhiều ORM. –
Tôi đang thêm các tính năng vào ứng dụng cũ và không muốn mang bất kỳ tệp DLL mới nào. – ca9163d9
Ok, sau đó bạn có một tháng làm việc để thực hiện phương pháp FindBy. Bạn cần tạo chuỗi SQL từ các biểu thức. Đây không phải là tầm thường * chút nào *. Tôi đề nghị bạn thay đổi thiết kế giao diện của bạn để không hỗ trợ các biểu thức tùy ý. Có thể bạn có thể hỗ trợ trình tự-chọn-trật tự-chọn-đầu-hàng Có lẽ thế là đủ. – usr