2011-03-04 57 views
6

我使用MOG的嘲諷與LINQ的存儲庫,以這樣的SQL:與實體框架嘲諷庫

public static IProductsRepository MockProductsRepository(params Product[] prods){ 
    // Generate an implementer of IProductsRepository at runtime using Moq 
    var mockProductsRepos = new Mock<IProductsRepository>(); 
    mockProductsRepos.Setup(x => x.Products).Returns(prods.AsQueryable()); 
    return mockProductsRepos.Object; 
} 

public interface IProductsRepository{ 
    IQueryable<Product> Products { get; } 
    void SaveProduct(Product product); 
    void DeleteProduct(Product product); 
} 

我怎麼能當我使用它像這樣改變這種功能實體框架:

public interface IProductsRepository : IEntities{ 
    EntityState GetEntryState(object entry); 
    void SetEntryState(object entry, EntityState state); 
    void Commit(); 
} 

public interface IEntities{ 
    DbSet<Product> Products { get; set; } 
} 

現在我正在使用DbSet

回答

2

好吧,既然IProductsRepository工具IEntities你應該有一個

public DbSet<Product> Products { get; set; } 

財產在那裏,但我會做的就是添加一個Fetch方法IProductRepository

public interface IProductsRepository : IEntities 
{ 
    EntityState GetEntryState(object entry); 
    void SetEntryState(object entry, EntityState state); 
    void Commit(); 

    // New method 
    IQueryable<Product> FetchAll(); 
} 

然後,在你MockProductsRepository更改設置線如下:

mockProductsRepos.Setup(x => x.FetchAll()).Returns(prods.AsQueryable());