2015-08-13 3 views
0

Я хотел бы добавить locationRepository к следующему коду, но не знаю, как это сделать? Обычно у меня было бы:Добавить репозиторий в AccountController

private ILocationRepository locationRepository; 

public AccountController(ILocationRepository locationRepository) 
     { 
      this.locationRepository= locationRepository; 
     } 

Как добавить вышеуказанный код к существующему контроллеру учетной записи, указанному ниже?

public AccountController() 
      : this(null, null) 
     { 
     } 

public AccountController(IFormsAuthentication formsAuth, IMembershipService service) 
{ 
    FormsAuth = formsAuth ?? new FormsAuthenticationService(); 
    MembershipService = service ?? new AccountMembershipService(); 
} 

public IFormsAuthentication FormsAuth 
     { 
      get; 
      private set; 
     } 

     public IMembershipService MembershipService 
     { 
      get; 
      private set; 
     } 
+1

Добавьте его в другую зависимость от конструктора? – janhartmann

+0

@CallumLinington Это не подведет - он инициализирует значения по умолчанию. – BartoszKP

+0

Я прокомментировал публикацию AccountController(): this (null, null) {}, и я получаю ошибку. Невозможно создать компонент - IFormsAuthentication, которая не была зарегистрирована. - IMembershipService, который не был зарегистрирован – Ros

ответ

0

С помощью этого link я получил контроллер работать.

Контроллер

private IMembershipService membershipService; 
private ILocationRepository locationRepository; 


public AccountController(IMembershipService membershipService, 
       ILocationRepository locationRepository) 
      { 
       this.membershipService = membershipService; 
       this.localRepository = locationRepository; 
      } 

Компонент Регистратор

container.Register(Component.For(typeof(IMembershipService)).ImplementedBy(typeof(AccountMembershipService))); 
0

Просто передать его в качестве аргумента другого конструктора:

public AccountController(
    IFormsAuthentication formsAuth, 
    IMembershipService service, 
    ILocationRepository locationRepository) 
{ 
    FormsAuth = formsAuth ?? new FormsAuthenticationService(); 
    MembershipService = service ?? new AccountMembershipService(); 
    LocationRepository = locationRepository ?? new LocationRepository(); 
} 

public IFormsAuthentication FormsAuth 
{ 
    get; 
    private set; 
} 

public IMembershipService MembershipService 
{ 
    get; 
    private set; 
} 

public ILocationRepository LocationRepository 
{ 
    get; 
    private set; 
} 
+0

спасибо @BartoszKP Я пробовал это, и теперь я получаю ошибку. Ссылка на объект не установлена ​​в экземпляр объекта. – Ros

+0

@Ros Если у вас есть другой вопрос, отправьте его в качестве другого вопроса, если он соответствует рекомендациям SO. В этом случае сначала попробуйте отладить. – BartoszKP