I am using repository pattern in mvc 5 I have created the ICountry Interface and clsCountry class when run the project and put a break point then _ICountry.listofCountry() throw the error "Object reference not send to an Instance". Any expert can tell me where i am wrong . I am using Database first approch and my db extension is .edmx
Model Countries
- public int CountryID { get; set; }
- public string CountryName { get; set; }
IcountryRepository
- public interface ICountry {
- List<Countries> listofCountry();
- }
clsCountryRepository
- public class clsCountry: ICountry {
- ECommercedbEntities _db;
- public clsCountry(ECommercedbEntities db) {
- _db = db;
- }
- public List < Countries > listofCountry() {
- return _db.Countries.ToList();
- }
- }
Controller
- public class AccountController: Controller {
- ICountry _ICountry;
- public AccountController(ICountry ICountry) {
- _ICountry = ICountry;
- }
- [HttpGet] public ActionResult Registration() {
- try {
- List < Countries > listCountry = _ICountry.listofCountry();
- ViewBag.CountryList = new SelectList(listCountry, "CountryID", "CountryName");
- return View();
- } catch (Exception ex) {
- throw ex;
- }
- }
- }
AppStart
- public static class UnityConfig {
- public static void RegisterComponents() {
- var container = new UnityContainer();
- container.RegisterType < ILogin, clsLogin > ();
- container.RegisterType < ICountry, clsCountry > ();
- DependencyResolver.SetResolver(new UnityDependencyResolver(container));
- }
- }
Global.asax
- protected void Application_Start()
- {
- UnityConfig.RegisterComponents();
- }