Linq 2 Sql - Repository isn't working unless query is done inside controller
I'm really struggling to understand an issue I'm facing. I have a very simple repository with the following code:
public class ProductRepository : IProductRepository
{
private Table<Product> productTable;
public Product GetProductById(int id)
{
return productTable.Where(p => p.ProductId == id).Single();
}
public IQueryable<Product> GetProducts()
{
return productTable;
}
}
I have a controller called AddToCart(). The repository class above is passed into the controller via DI. I pass a productId into this controller, and the repository method is called. The problem is that a null value is returned when a Product does exist.
public void AddToCart(int productId)
{
//Returns Null value
Product product = productRepository.GetProductById(productId);
...
}
I thought the issue may be that I need the IQueryable<>
around the class that I'm returning. I also modified my AddToCart method in various ways to try to get it to work.
public IQueryable<Product> GetProductById(int id)
{
return productTable.Where(p => p.ProductId == id);
}
public void AddToCart(int productId)
{
//Throws error / Returns Null value
Product product = productRepository.GetProductById(productId).Single();
//Returns Null value
Product product = productRepository.GetProductById(productId).FirstOrDefault();
}
However, the following works and I have no reason why! It's basically the same code, just b开发者_如何转开发eing done in the controller. Anyone have any ideas? :/
public void AddToCart(int productId)
{
Product product = productRepository.GetProducts().FirstOrDefault(p => p.ProductId == productId);
}
I wonder if you may be having an issue with your Dependency Injection. You are using .Single() which, according to the docs, will throw an exception if there is not exactly one item in the collection that satisfies your condition.
I would try implementing this without using Dependency Injection and see if you get the same results. You can also try instantiating and returning a product item directly from the GetProductById method (instead of pulling from the database) to rule out anything weird happening with your DI.
It's hard to see what the problem is without more information.
- How is GetProductById() returning null? Single() does not allow nulls to be returned and will throw an InvalidOperationException if there are no items or more than one item returned.
- What is GetProducts() doing differently? Looking at your code, there should not be any problem.
What happens when you try this?
public class ProductRepository : IProductRepository
{
private Table<Product> productTable;
public Product GetProductById(int id) {
return GetProducts().Where(p => p.ProductId == id).Single();
}
public IQueryable<Product> GetProducts() {
return productTable;
}
}
As a side note, you should avoid using Single() if your are positive that only one item exists in your collection. Single will always itterate through your entire collection. First() is more efficient in that regard.
精彩评论