I'm following on asp.net mvc 5 book by apress and I've got stuck at one point with this error:
"SportsStore.Domain.Concrete.EFProductRepository' does not implement interface member 'SportsStore.Domain.Abstract.IProductRepository.Products.set"
This is what I have in EFProductRepository:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
namespace SportsStore.Domain.Concrete
{
public class EFProductRepository : IProductRepository
{
public EFDbContext context = new EFDbContext();
public IEnumerable<Product> Products
{
get { return context.Products; }
}
}
}
And in IProductRepository I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsStore.Domain.Entities;
namespace SportsStore.Domain.Abstract
{
public interface IProductRepository
{
IEnumerable<Product> Products { get; set; }
}
}
Why am I getting the error in EFProductRepository.cs class?
Because you're not implementing a setter for the property. Look at the interface's definition:
IEnumerable<Product> Products { get; set; }
And look at your implementation:
public IEnumerable<Product> Products
{
get { return context.Products; }
}
You need to either implement a set
in the property, or remove set
from the interface definition. Given the implementation of the property, I'm guessing the latter is more appropriate.
If it helps, since it looks like you're building a repository for Entity Framework objects, here's what one of mine generally looks like:
public interface ProductRepository
{
IQueryable<Product> Products { get; }
void Add(Product model);
void Remove(Product model);
}
and the implementation:
public class ProductRepositoryImplementation : ProductRepository
{
private DbSet<Product> _products;
public IQueryable<Product> Products
{
get { return _products; }
}
public ProductRepositoryImplementation(DbSet<Product> products)
{
_products = products;
}
public void Add(Product model)
{
_products.Add(model);
}
public void Remove(Product model)
{
_products.Remove(model);
}
}
Generally this is housed within a unit of work object, which extends DbContext
and handles committing the changes to its repositories. (That's also what passes the DbSet<>
to the constructor for the repositories.) I actually just blogged about this setup today.