Is there a good/proper way of solving the dependency injection loop problem in the ASP.NET MVC ContactsManager tutorial?

As a general consideration, circular dependencies indicate a design flaw – I think I can safely say this since you are not the original author of the code 🙂 I wouldn’t consider an Initialize method a good solution. Unless you are dealing with an add-in scenario (which you aren’t), Method Injection is not the right … Read more

String URL to RouteValueDictionary

Here is a solution that doesn’t require mocking: var request = new HttpRequest(null, “http://localhost:3333/Home/About”, “testvalue=1”); var response = new HttpResponse(new StringWriter()); var httpContext = new HttpContext(request, response); var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)); var values = routeData.Values; // The following should be true for initial version of mvc app. values[“controller”] == “Home” values[“action”] == “Index” Hope … Read more

Uploading Files into Database with ASP.NET MVC

You could use a byte[] on your model and a HttpPostedFileBase on your view model. For example: public class MyViewModel { [Required] public HttpPostedFileBase File { get; set; } } and then: public class HomeController: Controller { public ActionResult Index() { var model = new MyViewModel(); return View(model); } [HttpPost] public ActionResult Index(MyViewModel model) { … Read more

DropDownList setting selected item in asp.net MVC

I think the problem is a confusion regarding the DropDownList overloads: Html.DropDownList(string name) looks for a view model property of name and type IEnumerable<SelectListItem>. It will use the selected item (SelectListItem.Selected == true) from the list, unless there is a form post value of the same name. Html.DropDownList(string name, IEnumerable<SelectListItem> selectList) uses the items from … Read more

Mocking HttpContextBase with Moq

I’m using a version of some code Steve Sanderson included in his Pro Asp.NET MVC book… and I’m currently having a moral dilemma whether it’s okay to post the code here. How about I compromise with a highly stripped down version? 😉 So this can easily be reused, create a class similar to the one … Read more

How to remove unit of work functionality from repositories using IOC

You shouldn’t try to supply the AcmeDataContext itself to the EmployeeRepository. I would even turn the whole thing around: Define a factory that allows creating a new unit of work for the Acme domain: Create an abstract AcmeUnitOfWork that abstracts away LINQ to SQL. Create a concrete factory that can allows creating new LINQ to … Read more