将新项目动态添加到 IQueryable 硬编码假存储库

发布于 2024-09-19 19:32:23 字数 592 浏览 11 评论 0原文

在使用真正的数据库之前构建应用程序,为了让事情正常工作,我可以首先使用硬编码列表作为假的内存存储库:

public class FakeProductsRepository 
{
    private static IQueryable<Product> fakeProducts = new List<Product> {
        new Product{ ProductID = "xxx", Description = "xxx", Price = 1000},
        new Product{ ProductID = "yyy", Description = "xxx", Price = 2000},
        new Product{ ProductID = "zzz", Description = "xxx", Price = 3000}
    }.AsQueryable();

    public IQueryable<Product> Products
    {
        get { return fakeProducts; }
    }
}

如何向此类添加方法以添加新的非硬编码项目动态地在此列表中?

Building an application, before using a real database, just to get things work I can first use a hard-coded list as a fake, in-memory repository:

public class FakeProductsRepository 
{
    private static IQueryable<Product> fakeProducts = new List<Product> {
        new Product{ ProductID = "xxx", Description = "xxx", Price = 1000},
        new Product{ ProductID = "yyy", Description = "xxx", Price = 2000},
        new Product{ ProductID = "zzz", Description = "xxx", Price = 3000}
    }.AsQueryable();

    public IQueryable<Product> Products
    {
        get { return fakeProducts; }
    }
}

How to add a method to this class for adding new, not hard-coded items in this list dynamically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦太阳 2024-09-26 19:32:23

只需保留列表<产品>即可在 List类型的字段中而不是 IQueryable

public class FakeProductsRepository 
{
    private readonly List<Product> fakeProducts = new List<Product>
    {
        new Product { ProductID = "xxx", Description = "xxx", Price = 1000 },
        new Product { ProductID = "yyy", Description = "xxx", Price = 2000 },
        new Product { ProductID = "zzz", Description = "xxx", Price = 3000 },
    };

    public void AddProduct(string productID, string description, int price)
    {
        fakeProducts.Add(new Product
        {
            ProductID = productID,
            Description = description,
            Price = price,
        });
    }

    public IQueryable<Product> Products
    {
        get { return fakeProducts.AsQueryable(); }
    }
}

Just keep the List<Product> in a field of type List<Product> instead of IQueryable<Product>:

public class FakeProductsRepository 
{
    private readonly List<Product> fakeProducts = new List<Product>
    {
        new Product { ProductID = "xxx", Description = "xxx", Price = 1000 },
        new Product { ProductID = "yyy", Description = "xxx", Price = 2000 },
        new Product { ProductID = "zzz", Description = "xxx", Price = 3000 },
    };

    public void AddProduct(string productID, string description, int price)
    {
        fakeProducts.Add(new Product
        {
            ProductID = productID,
            Description = description,
            Price = price,
        });
    }

    public IQueryable<Product> Products
    {
        get { return fakeProducts.AsQueryable(); }
    }
}
乞讨 2024-09-26 19:32:23

如果您打算模拟您的存储库以进行测试,那么我建议您首先声明一个包含您期望从存储库中获得的功能的接口。然后构建真实的和“假的”存储库来实现该接口,否则您将无法轻松地用一个存储库替换另一个存储库。

一旦有了一致的接口,你就会发现这很容易,函数已经被声明了,即

public interface IRepository {
    IQueryable<Products> GetAllProducts();
    Product AddProduct(Product Product);
}

public class FakeRepository : IRepository {
    private static IList<Product> fakeProducts = new List<Product> {
        new Product{ ProductID = "xxx", Description = "xxx", Price = 1000},
        new Product{ ProductID = "yyy", Description = "xxx", Price = 2000},
        new Product{ ProductID = "zzz", Description = "xxx", Price = 3000}
     };

     public IQueryable<Product> GetAllProducts() {
         return fakeProducts.AsQueryable();
     }

     public Product Add(Product Product) {
         fakeProducts.Add(Product);
         return Product;
     }
 }

If you are going to Mock your repository for testing purposes then I'd suggest that you start by declaring an interface that encompasses the functions your expect from your repository. Then build your real and your 'fake' repository to implement that interface, otherwise you won't be able to easily substitute one for the other.

You'll find it pretty easy once you have that consistent interface, the functions will already be declared, i.e.

public interface IRepository {
    IQueryable<Products> GetAllProducts();
    Product AddProduct(Product Product);
}

public class FakeRepository : IRepository {
    private static IList<Product> fakeProducts = new List<Product> {
        new Product{ ProductID = "xxx", Description = "xxx", Price = 1000},
        new Product{ ProductID = "yyy", Description = "xxx", Price = 2000},
        new Product{ ProductID = "zzz", Description = "xxx", Price = 3000}
     };

     public IQueryable<Product> GetAllProducts() {
         return fakeProducts.AsQueryable();
     }

     public Product Add(Product Product) {
         fakeProducts.Add(Product);
         return Product;
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文