A better DI with Autofac Repositories

I have most recently been using Structure map DI but I would have to say Autofac would be the DI choice for licensing, ease of setup & performance.

Both achieve keeping Data Access out of the controller but the difference is in the instantiation and the ability to Unit Test & mock.

Autofac is free and doesn’t have these shortcomings. So it’s an easy choice for MVC.

Setting up is easy:

___________________________________________
For Interfaces: (From Interfaces Project or Folder)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClientRepository.Data;

namespace ClientRepository.Interfaces
{
public interface ICustomerRepository
{
IEnumerable SelectAll();
Customer SelectByID(string id);
void Insert(Customer obj);
void Delete(string id);
void Save();
}
}

___________________________________________

For Data Access: (From Data Project)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using ClientRepository.Interfaces;

namespace ClientRepository.Data
{

public class CustomerRepository : ICustomerRepository
{
IClientDB_DBEntities ClientDBContext;
public CustomerRepository(IClientDB_DBEntities db)
{
ClientDBContext = db;
}
public IEnumerable SelectAll()
{
return ClientDBContext.Customers.ToList();
}

public Customer SelectByID(string id)
{
return ClientDBContext.Customers.Find(id);
}

public void Insert(Customer obj)
{
ClientDBContext.Customers.Add(obj);
}
public void Delete(string id)
{
var value = ClientDBContext.Customers.Where(i => i.CustomerID == id).FirstOrDefault();
ClientDBContext.Customers.Remove(value);
}
public void Save()
{
ClientDBContext.SaveChanges();
}
}
}

___________________________________________
For Data Access: (Register Repositories)

namespace ClientRepository.Data
{
public static class AutofacConfig
{
public static void RegisterComponents()
{
var builder = new ContainerBuilder();
builder.RegisterType().As<ICustomerRepository>();
builder.RegisterType<CustomerController>();
builder.RegisterType<ClientDB_DBEntities>().As<IClientDB_DBEntities>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

}
}

___________________________________________

for the Web Project : Clean implementation in the controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ClientRepository.Interfaces;
using ClientRepository.Data;

namespace RepositoryPattern.Controllers
{
public class CustomerController : Controller
{
ICustomerRepository _CustomerRepo;

public CustomerController(ICustomerRepository customerRepo)
{
_CustomerRepo = customerRepo;
}

//
// GET: /Customer/
public ActionResult Index()
{
List<Customer> model = (List<Customer>)_CustomerRepo.SelectAll();
return View(model);
}

public ActionResult Insert(Customer obj)
{
_CustomerRepo.Insert(obj);
_CustomerRepo.Save();
return View();
}

public ActionResult Edit(string id)
{
Customer existing = _CustomerRepo.SelectByID(id);
return View(existing);
}

public ActionResult ConfirmDelete(string id)
{
Customer existing = _CustomerRepo.SelectByID(id);
return View(existing);
}

public ActionResult Delete(string id)
{
_CustomerRepo.Delete(id);
_CustomerRepo.Save();
return View();
}

}
}

and finally your Models are decluttered by accessing from the Data Access Layer

@model List<ClientRepository.Data.Customer>

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s