Unity IoC and MVC modelbinding

Posted by danielovich on Stack Overflow See other posts from Stack Overflow or by danielovich
Published on 2010-05-18T17:36:08Z Indexed on 2010/05/18 17:40 UTC
Read the original article Hit count: 285

Filed under:
|
|

Is it ok to have a static field in my controller for my modelbinder to call ?

Eg.

public class AuctionItemsController : Controller
{
    private IRepository<IAuctionItem> GenericAuctionItemRepository;
    private IAuctionItemRepository AuctionItemRepository;

    public AuctionItemsController(IRepository<IAuctionItem> genericAuctionItemRepository, IAuctionItemRepository auctionItemRepository) {
        GenericAuctionItemRepository = genericAuctionItemRepository;
        AuctionItemRepository = auctionItemRepository;
        StaticGenericAuctionItemRepository = genericAuctionItemRepository;
    }

    internal static IRepository<IAuctionItem> StaticGenericAuctionItemRepository;

here is the modelbinder

public class AuctionItemModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
        if (AuctionItemsController.StaticGenericAuctionItemRepository != null) {
            AuctionLogger.LogException(new Exception("controller is null"));
        }

        NameValueCollection form = controllerContext.HttpContext.Request.Form;

        var item = AuctionItemsController.StaticGenericAuctionItemRepository.GetSingle(Convert.ToInt32(controllerContext.RouteData.Values["id"]));

        item.Description = form["title"];
        item.Price = int.Parse(form["price"]);
        item.Title = form["title"];
        item.CreatedDate = DateTime.Now;
        item.AuctionId = 1;


        //TODO: Stop hardcoding this
        item.UserId = 1;

        return item;
    }}

i am using Unity as IoC and I find it weird to register my modelbinder in the IoC container.

Any other good design considerations I shold do ?

© Stack Overflow or respective owner

Related posts about mvc

Related posts about dependency-injection