在AccountController中添加存储库

时间:2015-08-13 08:25:03

标签: c# asp.net-mvc

我想在以下代码中添加locationRepository,但不确定如何执行此操作?通常我会:

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;
        }

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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)));