0
votes

I'm using ASP.NET MVC 3 and have my custom model binder.

public class NewsModelBinder : DefaultModelBinder
{
    private readonly INewsRepository newsRepository;
    private readonly ICategoryRepository categoryRepository;

    public NewsModelBinder()
    {

    }

    public NewsModelBinder(INewsRepository newsRepository, ICategoryRepository categoryRepository)
    {
        this.newsRepository = newsRepository;
        this.categoryRepository = categoryRepository;
    }

    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
         //Some code here
    }
}

And i want to resolve dependencies with Castle Windsor. But which way i can do this if i want to both register my binder and i want Windsor to give it required parametres? Because when i register binder I can't just give it needed parametres.

UPD 0 Or, maybe, I can get suitable objects from container in code? Because i register these repositories as singletones.

1

1 Answers

0
votes

I've solved problem in this way: set my dependencies as properties and used property injection

public class NewsModelBinder : DefaultModelBinder
{
    public INewsRepository NewsRepository { get; set; }
    public ICategoryRepository CategoryRepository { get; set; }

    public NewsModelBinder()
    {}

    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
         //Some code here
    }
}

And then in Global.asax i added my binder like this

ModelBinders.Binders.Add(typeof(News), Container.Resolve<NewsModelBinder>());

Windsor resolves dependencies by himself in this way