I want to use dependency injection with Unity in my application. I am not following repository pattern and unit-of-work (and don't want to). I also have ViewModel in my controller action method. Please review my code and add your comments.
public class ProductCatagoryController : Controller
{
private dataBaseContext _context;
public ProductCatagoryController(IdataBaseContext context)
{
this._context = context as dataBaseContext;
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult addNewCategory([Bind(Include = "catName,desc")]RegisterNewCatagoryViewModel RegisterNewCatagoryViewModel)
{
if (ModelState.IsValid)
{
try
{
Mapper.CreateMap<RegisterNewCatagoryViewModel, Category>();
Category category = Mapper.Map<Category>(RegisterNewCatagoryViewModel);
category.catID = Guid.NewGuid().ToString();
category.date_from = DateTime.Now;
category.active = true;
_context.Categories.Add(category);
_context.SaveChanges();
}
catch (Exception e)
{
loggerElmah.logElmah(e, "Adding New Category Failed");
}
}
return View();
}
[HttpGet]
public ActionResult addNewCategory()
{
return View();
}
// GET: ProductCatagory
public ActionResult Index()
{
return View();
}
}
In addNewCategory
I am passing an object of a concrete class object, but I want to pass an interface of the viewmodel
and bind it with actual implementation in the run-time through unity. How can I do this?
Also, can someone guide me on writing unit-tests for the above code?