8

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; 
    } 
} 

Trả lời

0

Bạn đang cố gắng xây dựng một phần của những gì một trình vẽ OR điển hình thực hiện. Đây là công việc. Tại sao không chỉ sử dụng ORM?

+1

đồng ý. Có rất nhiều ORM. –

+1

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

+1

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

6

Nói chung không phải là một ý tưởng hay khi có kho lưu trữ chung. Kho lưu trữ là một khái niệm miền quan trọng và bạn không muốn phát triển quá mức nó giống như bạn sẽ không muốn khái quát hóa các thực thể của bạn. Kho lưu trữ chung là CRUDy và chuyển trọng tâm ra khỏi miền của bạn. Vui lòng xem xét this article by Greg Young.

Trên một lưu ý liên quan, phơi bày IQueryable sẽ giới thiệu tight coupling ngoài làm cho mã của bạn ít miền và nhiều hơn nữa dữ liệu driven.

+0

Tôi đã cập nhật câu hỏi để giải thích vấn đề về miền/dữ liệu được điều khiển. – ca9163d9

0

Nếu bạn đang đi từ một cơ sở dữ liệu kế thừa đầy mâu thuẫn, và một tấn thủ tục được lưu trữ và bạn đang cố gắng để treo nó lên một mẫu ORM/Repository, sau đó bạn có thể thấy mình thực sự thất vọng với việc thực hiện một Mẫu Repository chung.

Tôi biết các mẫu Repository chung là một hit lớn trong phần hướng dẫn những ngày này khi nhiều ứng dụng mới hơn cho phép những thứ như Entity Framework và Active Record tạo cơ sở dữ liệu (nhớ kiểu này không ngụ ý thủ tục lưu trữ hoặc sử dụng tối thiểu) . Trong các kịch bản mới hơn này, dữ liệu có xu hướng sạch sẽ hơn một chút và làm cho nó rất dễ dàng để kết nối nó với một số mẫu kho lưu trữ chung như mọi thực thể có nhiều ID.

0

Không hẳn về chủ đề, nhưng tôi đã có vấn đề tương tự, và đây là giải pháp của tôi (bất cứ ai có thể giúp đỡ)

Tạo nhận dạng lớp:

public class Identity 
    { 
     public int Id { get; set; } 
    } 

Tạo entyty lớp:

public class Employee: Identity 
    { 
     public string Name { get; set; } 
     public string Surname { get; set; } 
     public int Age { get; set; } 
    } 

Giao diện kho lưu trữ (bạn cũng chỉ có thể sử dụng lớp trừu tượng):

interface IRepository<T> where T: Identity 
    { 
     T GetById(int id); 
     ICollection<T> GetAll(); 
     ICollection<T> GetAll(string where); 
     void Update(T entity); 
     void Insert(T entity); 
     bool Delete(T entity); 
     bool Delete(ICollection<T> entityes); 
    } 

Tạo Generic lớp trừu tượng:

public abstract class AbstractRepository<T>: IRepository<T> where T : Identity 
    { 
     protected abstract string TableName { get; } 

     protected abstract T DataRowToModel(DataRow dr); 

     protected virtual ICollection<T> DataTableToCollection(DataTable dt) 
     { 
      if (dt == null) 
      { 
       return null; 
      } 
      return dt.AsEnumerable().Select(x => DataRowToModel(x)).ToList(); 
     } 

     public virtual T GetById(int id) 
     { 
      var query = $"select * from {TableName} where id = {id}"; 
      //the data access layer is implemented elsewhere 
      DataRow dr = DAL.SelectDataRow(query); 
      return DataRowToModel(dr); 
     } 

     public virtual void Delete(T entity) 
     { 
      if (entity.Id == 0) 
      { 
       return; 
      } 
      var query = $"delete from {TableName} where id = {entity.Id}"; 
      DAL.Query(query); 
     } 

     public virtual void Delete(ICollection<T> entityes) 
     { 
      var collectionId = IdentityCollectionToSqlIdFormat(entityes); 
      if (string.IsNullOrEmpty(collectionId)) 
      { 
       return; 
      } 
      var query = $"delete from {TableName} where id in ({collectionId})"; 
      DAL.Query(query); 
     } 

     public virtual ICollection<T> GetAll() 
     { 
      var query = $"select * from {TableName}"; 
      DataTable dt = DAL.SelectDataTable(query); 
      return DataTableToCollection(dt); 
     } 

     public virtual ICollection<T> GetAll(string where) 
     { 
      var query = $"select * from {TableName} where {where}"; 
      DataTable dt = DAL.SelectDataTable(query); 
      return DataTableToCollection(dt); 
     } 

     protected virtual string IdentityCollectionToSqlIdFormat(ICollection<T> collection) 
     { 
      var array = collection.Select(x => x.Id); 
      return string.Join(",", array); 
     } 

     public abstract bool Update(T entity); 
     public abstract bool Insert(T entity); 
    } 

Và thực hiện EmployeeRepository:

public class EmployeeRepository : AbstractRepository<Employe> 
    { 
     protected sealed override string TableName 
     { 
      get 
      { 
       return "dbo.Employees"; 
      } 
     } 

     protected sealed override Employe DataRowToModel(DataRow dr) 
     { 
      if (dr == null) 
      { 
       return null; 
      } 
      return new Employe 
      { 
       Id = dr.Field<int>("id"), 
       Name = dr.Field<string>("name"), 
       Surname = dr.Field<string>("surname"), 
       Age = dr.Field<int>("age") 

      }; 
     } 


     public override void Insert(Employe entity) 
     { 
      var query = [email protected]"insert into {TableName} (name, surname, age) 
          values({entity.Name},{entity.Surname},{entity.Age})"; 
      DAL.Query(query); 
     } 

     public override bool Update(Employe entity) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

đó là tất cả.Sử dụng mã:

public class SomeService 
{ 
    public void SomeeMethod() 
    { 
    int employeeId = 10; // for example 
    EmployeeRepository repository = new EmployeeRepository(); 
    Employee employee = repository.GetById(employeeId); 
    repository.Delete(employee); 
    //... 
    } 
    } 
Các vấn đề liên quan