I have a Repository Class and a Services Class as below :
public class DinnerRepository
{
DinnerDataContext db = new DinnerDataContext();
public Dinner GetDinner(int id)
{
return db.Dinners.SingleOrDefault(d => d.DinnerID == id);
}
// Others Code
}
public class Service
{
DinnerRepository repo = new DinnerRepository();
Dinner dinner = repo.GetDinner(5);
// Other Code
}
This throws error:
A field initializer cannot reference the non-static field, method, or property.
Even though I have intatiated the DinnerRepository Class to expose its method GetDinner() in the Service Class. This works fine with below code. Is there any alternative to it or is it a standard practice? I cannot use static methods here..
public class Service
{
public Service()
{
DinnerRepository repo = new DinnerRepository();
Dinner dinner = repo.GetDinner(5);
}
}