3. UserController

publicclassUserController : Controller

    {

 

        IUserRepository _userrepos;

 

        public UserController( IUserRepository _repository)

        {

            _userrepos = _repository;

        }

 

        // GET: User

        publicActionResult Index()

        {

            var data = _userrepos.GetUserList();

            return View(data);

        }

 

        // GET: User/Details/5

        publicActionResult Details(int id)

        {

            var data = _userrepos.GetUser(id);

            return View(data);

        }

 

        // GET: User/Create

        publicActionResult Create()

        {

            return View();

        }

 

        // POST: User/Create

        [HttpPost]

        publicActionResult Create(User user)

        {

            try

            {

                _userrepos.Add(user);

                _userrepos.SaveChanges();

                return RedirectToAction("Index");

            }

            catch

            {

 

            }

            return View();

        }

 

        // GET: User/Edit/5

        publicActionResult Edit(int id)

        {

            return View();

        }

 

        // POST: User/Edit/5

        [HttpPost]

        //public ActionResult Edit(int id, FormCollection collection)

        publicActionResult Edit(int id, User usr)

        {

            try

            {

                usr.UserId = id;

                // TODO: Add update logic here

                _userrepos.Update(usr);

                _userrepos.SaveChanges();

                return RedirectToAction("Index");

            }

            catch

            {

                return View();

            }

        }

 

        // GET: User/Delete/5

        publicActionResult Delete(int id)

        {

            return View();

        }

 

        // POST: User/Delete/5

        [HttpPost]

        publicActionResult Delete(int id, FormCollection collection)

        {

            try

            {

                // TODO: Add delete logic here

                return RedirectToAction("Index");

            }

            catch

            {

                return View();

            }

        }

    }

4. Autofac install (autofac + autofacMVC)

5. Global.asax.

  protectedvoid Application_Start()

        {

            AreaRegistration.RegisterAllAreas();

            GlobalConfiguration.Configure(WebApiConfig.Register);

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            RouteConfig.RegisterRoutes(RouteTable.Routes);

            BundleConfig.RegisterBundles(BundleTable.Bundles);

 

            var builder = newContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            builder.RegisterType(typeof(UserRepository)).AsImplementedInterfaces();

            //builder.RegisterType(typeof(CustomerRepository)).AsImplementedInterfaces();

            var container = builder.Build();

            DependencyResolver.SetResolver(newAutofacDependencyResolver(container));

 

 

 

        }