Can someone explain dependency injection with a basic .NET example and provide a few links to .NET resources to extend on
Create DB layer project(class library) and add below code in it.
public class UserData : IUserData
{
public string getUserDetails()
{
return "Asif";
}
}
public interface IUserData
{
string getUserDetails();
}
Add bussiness logic project of type class library and add below code in it. public class UserDetailLogic : IUserDetailLogic { private IUserData _userData = null;
public UserDetailLogic(IUserData userData)
{
_userData = userData;
}
public string getUserDetails()
{
return _userData.getUserDetails();
}
}
public interface IUserDetailLogic
{
string getUserDetails();
}
In you main project add below code in home controller.
public class HomeController : Controller { private readonly IUserDetailLogic _userDetailLogic;
public HomeController(IUserDetailLogic userDetailLogic)
{
_userDetailLogic = userDetailLogic;
}
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
string str = _userDetailLogic.getUserDetails();
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}