Reputation: 2293
I'm returning a null in my code: (the var cat line)
public class CategoryItemsViewModel
{
public ICategoriesRepository categoriesRepository;
public IEnumerable<Categories> GetCategories()
{
var cat = categoriesRepository.Categories;
return cat;
}
}
here is the interface:
namespace SportsStore.Domain.Abstract
{
public interface ICategoriesRepository
{
IQueryable<Categories> Categories { get; }
void SaveCategory(Categories category);
void DeleteCategory(Categories category);
}
}
here is the class:
namespace SportsStore.Domain.Entities
{
public class Categories
{
[Key]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
}
What am I doing wrong?
Upvotes: 1
Views: 1583
Reputation: 203821
You haven't set anything that's not null
to the Categories
property in the ICategoriesRepository
object, and you can't, with the given interface. You have a property with an automatic getter an no setter. This means it will only ever be able to return null
. If there is a setter (either public or privite) then you would be able to set it to something else. (You would also need to have a line of code that actually did it.) You could also use a custom getter that actually returned something other than just the backing field. (This is common for derived data.)
Upvotes: 1
Reputation: 5791
What you have so far is fine - it's just incomplete.
You don't need a setter as someone else suggested either - in fact in this case it's probably better not to have one.
You do need a concrete implementation of your interface ICategoriesRepository eg:
public class CategoriesRepository : ICategoriesRepository
{
private readonly ModelContainer db= new ModelContainer();
public IQueryable<Categories> Categories
{
get
{
return this.db.Categories;
}
}
public void SaveCategory(Categories category)
{
// TODO
}
public void DeleteCategory(Categories category)
{
// TODO
}
}
Then you will need to add a line to your viewmodel:
public class CategoryItemsViewModel
{
public ICategoriesRepository categoriesRepository;
public IEnumerable<Categories> GetCategories()
{
// This is to instantiate your repository. It may be a better idea to do this
// when it is declared, above.
categoriesRepository = new CategoriesRepository();
var cat = categoriesRepository.Categories;
return cat;
}
}
Upvotes: 2