简体   繁体   English

在存储库中实例化的上下文(ASP.NET MVC)

[英]Context to be instantiated within the repository (ASP.NET MVC)

I have Mobile area and Account controller in it 我有移动区域和帐户控制器

Here is code 这是代码

public class AccountController : BaseController
{
    private AccountRepositoryEntities repoEntities; //The desktop trackerweb repository
    private Domain.Repository.Mobile.AccountRepository mobileRepo;
    private IdentityUserManager userManager;
    private IAuthenticationManager authenticationManager;
    private Dictionary dictionary;

    public AccountController(TraxgoDB context) : base()
    {
        repoEntities = new AccountRepositoryEntities();
        mobileRepo = new Domain.Repository.Mobile.AccountRepository(context);
    }
}

Context is passed to controller. 上下文被传递给控制器​​。

I need context to be instantiated within the repository 我需要在存储库中实例化上下文

So controller will be like 所以控制器会像

public AccountController() : base()
{
    repoEntities = new AccountRepositoryEntities();
    mobileRepo = new Domain.Repository.Mobile.AccountRepository();
}

Here is code of Domain.Repository.Mobile.AccountRepository 这是Domain.Repository.Mobile.AccountRepository代码

public sealed class AccountRepository : BaseRepository
{
    public AccountRepository(TraxgoDB context) : base(context) { }

    public Site TryGetCustomSite(string hostName)
    {
        var site = context.Sites.FirstOrDefault(s => hostName.Contains(s.HostName));
        if (site != null && site.HostName != "traxgo")
            return site;
        return null;
    }

    public CultureInfo UpdateCustomerLanguage(int customerID, CultureInfo culture)
    {
        using (var ctx = new TraxgoDB(TraxgoDBRights.ReadWrite))
        {
            var customer = ctx.Customers.FirstOrDefault(c => c.ID == customerID);
            var currentCultureInfo = customer.Language.GetCultureInfo();

            if (currentCultureInfo != culture)
            {
                customer.Language = culture.GetLanguage();
                ctx.SaveChanges();
            }
            return culture;
        }
    }
}

And here is BaseRepository 这是BaseRepository

 public abstract class BaseRepository
{
    protected TraxgoDB context { get; }

    protected BaseRepository(TraxgoDB context)
    {
        this.context = context;
    }
}

Without arguments. 没有参数。 How I can do this? 我该怎么做?

UPDATE 更新

In task was said 在任务中说

each controller constructor with arguments, should have its argumentts removed for example: public AccountController(TraxgoDB context) -> public AccountController() mobileRepo = new Domain.Repository.Mobile.AccountRepository(context); 每个带有参数的控制器构造函数,都应删除其参数,例如:public AccountController(TraxgoDB context)-> public AccountController()mobileRepo = new Domain.Repository.Mobile.AccountRepository(context); -> mobileRepo = new Domain.Repository.Mobile.AccountRepository(); -> mobileRepo =新的Domain.Repository.Mobile.AccountRepository(); because we want the context to be instantiated within the repository 因为我们希望在存储库中实例化上下文

I think it needed to write it like this in controller? 我认为需要在控制器中这样写吗?

public AccountController() : base()
    {
        using (TraxgoDB ctx = new TraxgoDB())
        {   
             repoEntities = new AccountRepositoryEntities();
             mobileRepo = new Domain.Repository.Mobile.AccountRepository(ctx);

        }
    }

I think you are probably looking for something like this, which also introduces dependency injection. 我认为您可能正在寻找类似的东西,这也引入了依赖注入。 But then we are not instantiating, rather we are injecting. 但是然后我们没有实例化,而是在注入。

public class AccountController : BaseController
{
    private readonly IAccountRepository _accountRepository;
    public AccountController(IAccountRepository accountRepository)
    {
        _accountRepository = accountRepository;
    }
}

public interface IAccountRepository
{
    Site TryGetCustomSite(string hostName);
     CultureInfo UpdateCustomerLanguage(int customerID, CultureInfo culture);
}

public class AccountRepository : IAccountRepository, BaseRepository
{
    private readonly ITraxgoDB _context;

    public AccountRepository(ITraxgoDB context) {
        _context = context;
    }

    public Site TryGetCustomSite(string hostName)
    {
        var site = _context.Sites.FirstOrDefault(s => hostName.Contains(s.HostName));
        if (site != null && site.HostName != "traxgo")
            return site;
        return null;
    }

    public CultureInfo UpdateCustomerLanguage(int customerID, CultureInfo culture)
    {
        using (var ctx = new TraxgoDB(TraxgoDBRights.ReadWrite))
        {
            ...
        }
    }
}

If you are using .net core, then you can use the depeneny injection that is provided. 如果您使用的是.net core,则可以使用所提供的水杨醛注射。 Otherwise, if you use .netframeworks 4.x then you could consider something like autofac. 否则,如果您使用.netframeworks 4.x,则可以考虑使用autofac之类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM