Based on the reply of this questionquestion I have created the following code. I need to check whether it's good or not.
Here is my entity class:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Designation { get; set; }
}
This is my db context implementation:
public class MyDataContext<T> : DbContext where T:class
{
private IDbSet<T> _dbSet;
public MyDataContext() : base("name=DefaultConnectionString")
{
_dbSet = this.Set<T>();
}
public MyDataContext(IDbSet<T> dbSet )
: base("name=DefaultConnectionString")
{
this._dbSet = dbSet;
}
public IDbSet<T> DbSetOjbect
{
get { return _dbSet; }
}
}
Now I have implemented the EmployeeService business logic class and the IEmployee service class:
public interface IEmployeeService
{
List<Employee> GetEmployees();
}
Here is the implementation:
public class EmployeeService : IEmployeeService
{
private IDbSet<Employee> employee;
public EmployeeService()
{
var employeeContext = new MyDataContext<Employee>();
employee = employeeContext.DbSetOjbect;
}
public EmployeeService(IDbSet<Employee> employee)
{
this.employee = employee;
}
public List<Employee> GetEmployees()
{
return employee.ToList();
}
}
The following is my controller code in ASP.NET MVC controller.
public class EmployeeController : Controller
{
private readonly IEmployeeService _employeeService;
public EmployeeController()
{
_employeeService = new EmployeeService();
}
public EmployeeController(IEmployeeService employeeService)
{
_employeeService = employeeService;
}
public ActionResult Index()
{
return View(_employeeService.GetEmployees());
}
}
I want to check whether it is a good approach for TDD Test Driven Development or not.