Инжекция базы данных в проверку приписывает с ASP MVC и замок Windsor

Вместо этого создайте HTML-тег для выполнения запроса GET, Facebook не разрешает XMLHttpsRequests.

Вроде так: <a href="http://localhost:8080/auth/facebook">sign in with facebook</a>

7
задан David E 12 June 2009 в 10:29
поделиться

3 ответа

AFAIK no dependency injection container can directly manage an attribute, since it's instantiated by the runtime and there's no way to intercept that.

However, they can cheat by either:

  1. Using a static gateway to the container (example), or
  2. Using a "BuildUp" feature that injects whatever dependencies are found within an already-constructed object. This is called BuildUp in Unity or InjectProperties in Autofac.

Windsor doesn't support #2 (ref1, ref2), so you can either:

  1. Try one of the hacks to make Windsor support #2 (hack1, hack2)
  2. Use a static gateway
  3. Implement your own IValidatorBuilder and make it use Windsor to create validators. I'm sure this is implemented somewhere but I can't find it right now...
5
ответ дан 7 December 2019 в 14:37
поделиться

Hmm.

Can you test the effect of removing the (string message) ctor, and see if that at least forces Castle to use the ctor with the Repostiory ?

Otherwise we call AddComponent(name, type, type). Other than that it really should work...

Also does this hint at my first idea ? How do I use Windsor to inject dependencies into ActionFilterAttributes

-1
ответ дан 7 December 2019 в 14:37
поделиться

Не знаю, помогает ли это, но я создал подкласс ValidationAttribute , чтобы предоставить метод Resolve () следующим образом:

public abstract class IocValidationAttribute : ValidationAttribute
{
    protected T Resolve<T>()
    {
        return IocHelper.Container().Resolve<T>();
    }
}

Затем его можно использовать в любом custom ValidatorAttribute, который должен попасть в базу данных:

public class UniqueEmailAttribute : IocValidationAttribute
{
    public override bool IsValid(object value)
    {
        ICustomerRepository customerRepository = Resolve<ICustomerRepository>();

        return customerRepository.FindByEmail(value.ToString()) == null;
    }
}

Я думаю, что это вариант подхода «Статический шлюз», упомянутого Маурисио Шеффером. Не знаю, хороший это дизайн или нет. Я не большой поклонник этого, я бы предпочел, чтобы зависимость была введена более `` элегантно '', хотя я, очевидно, не могу использовать инъекцию конструктора, я бы хотел использовать инъекцию свойств, но не могу найти способ для этого подключитесь к коду фреймворка ASP.NET MVC (я даже просмотрел исходный код MVC2).

0
ответ дан 7 December 2019 в 14:37
поделиться
Другие вопросы по тегам:

Похожие вопросы: