Configuring MVC 5 project with service layer and DI and UOW In the past I have always called the repositories directly from the controller, but that is a bad practice and now I am implementing a "Business Layer" to my project.

Would I have two UnitOfWorks?  One for the Service and then one for the repositories?

BaseController:

public class BaseController : Controller
{
    protected IUnitOfWorkService UnitOfWorkService;
    private readonly IEmployeeService _employeeService;

    protected BaseController(IUnitOfWorkService unitOfWorkService)
    {
        UnitOfWorkService = unitOfWorkService;
        _employeeService = unitOfWorkService.EmployeeService;
    }
}


HomeController:

public class HomeController : BaseController
{
    private readonly IEmployeeService _employeeService;

    public HomeController(IUnitOfWorkService unitOfWorkService) : base(unitOfWorkService)
    {
        _employeeService = unitOfWorkService.EmployeeService;
    }

    public ActionResult Index()
    {
        var emp = _employeeService.GetEmployee(employeeId);
        ...
    }

    ...
}


EmployeeService:

public class EmployeeService : IEmployeeService
{
    private IEmployeeRepository _employeeRepo;

    public EmployeeService(IUnitOfWork unitOfWork)
    {
        _employeeRepo = unitOfWork.EmployeeRepository;
    }

    public Employee GetEmployee(int employeeId)
    {
        return _employeeRepo.GetEmployee(employeeId);
    }

    ...
}


EmployeeRepository

public class EfEmployeeRepository : EfRepository, IEmployeeRepository
{
    public EfEmployeeRepository(SqlContext context) : base(context) { }

    public Employee GetEmployee(int employeeId)
    {
        var employee = Context.Employees
                    .SingleOrDefault(e =&gt; e.EmployeeId == employeeId);


        return employee != null ? employee.ToDomain() : null;
    }
}


Unit of Work

IUnitOfWorkService:

public interface IUnitOfWorkService : IDisposable
{
    IEmployeeService EmployeeService { get; }
}


UnitOfWorkService:

public class UnitOfWorkService : IUnitOfWorkService
{
    private readonly IUnitOfWork _unitOfWork;

    private EmployeeService _employeeService;

    public UnitOfWorkService()
    {
        _unitOfWork = new EfUnitOfWork();
    }


    public IEmployeeService EmployeeService
    {
        get { return _employeeService ?? (_employeeService = new EmployeeService(_unitOfWork)); }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
            _unitOfWork.Dispose();
    }
}


IUnitOfWork:

public interface IUnitOfWork : IDisposable
{
    IEmployeeRepository EmployeeRepository { get; }
}


UnitOfWork:

public class EfUnitOfWork : IUnitOfWork
{
    private readonly SqlContext _context;

    private EfEmployeeRepository _employeeRepository;

    public IEmployeeRepository EmployeeRepository
    {
        get { return _employeeRepository ?? (_employeeRepository = new EfEmployeeRepository(_context)); }
    }

    public EfUnitOfWork()
    {
        _context = new SqlContext();
    }

    public void Save()
    {
        _context.SaveChanges();
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
            _context.Dispose();
    }
}


Register Dependencies

builder.RegisterType&lt;EfUnitOfWork&gt;().As&lt;IUnitOfWork&gt;().InstancePerRequest();
builder.RegisterType&lt;UnitOfWorkService&gt;().As&lt;IUnitOfWorkService&gt;().InstancePerRequest();
public class BaseController : Controller
{
    protected IUnitOfWorkService UnitOfWorkService;
    private readonly IEmployeeService _employeeService;

    protected BaseController(IUnitOfWorkService unitOfWorkService)
    {
        UnitOfWorkService = unitOfWorkService;
        _employeeService = unitOfWorkService.EmployeeService;
    }
}
public class HomeController : BaseController
{
    private readonly IEmployeeService _employeeService;

    public HomeController(IUnitOfWorkService unitOfWorkService) : base(unitOfWorkService)
    {
        _employeeService = unitOfWorkService.EmployeeService;
    }

    public ActionResult Index()
    {
        var emp = _employeeService.GetEmployee(employeeId);
        ...
    }

    ...
}  




I don't see a reason here to have private readonly IEmployeeService _employeeService; in the HomeController. It would be better to make the _employeeService variable of the BaseController protected so it can be used by the objects which inherits the BaseController.  
Why is the protected IUnitOfWorkService UnitOfWorkService; not readonly ?  




Style  


Using abbreviations or naming variables is bad practice. Does it hurt to name the variable IEmployeeRepository _employeeRepo like IEmployeeRepository _employeeRepository ?  
It does not hurt to use braces {} for single if statements but will make the code less error prone. I would like to encourage you to always use them.