Asp.Net MVC - 所有控制器通用数据 [英] Asp.Net MVC - Common Data Across All Controllers

查看:185
本文介绍了Asp.Net MVC - 所有控制器通用数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的设置:(使用Asp.Net MVC 2 RC,实体框架时,SQL Server,VS2008)

我的朋友和我的一个项目,将在其具有不同的域名指向的工作。我们希望得到的域名(网站)出请求,并用它来驱动数据。该网站数据需要的所有控制器的组成部分。


  

防爆。数据domain1.website.com会
  比用于数据不同
  domain2.website.com,和那些将
  比website2.com数据不同。
  该网站的外观是对同一
  所有这些,但数据是不同的。


我成立了一个BaseController,我的所有其他控制器继承。但我不喜欢它。

BaseController:

 公共类BaseController:控制器
{
    私人网站_website;
    私人只读IWebsiteRepository _websiteRepository;    公共BaseController(IWebsiteRepository websiteRepository)
    {
        _websiteRepository = websiteRepository;
    }    公共网站CurrentWebsite {搞定; }
}

这样做的问题是,我现在需要一个IWebsiteRepository传递给我的每个控制器的基础:

 公共类BioController:BaseController
{
    私人只读IBiographyRepository _bioRepository;    公共BioController(IBiographyRepository bioRepository,IWebsiteRepository websiteRepository)
        :基地(websiteRepository)
    {
        _bioRepository = bioRepository;
    }
}

这里是我的问题


  1. 有没有更好的方式来处理被指向多个域的一个项目和过滤数据?

  2. 有没有更好的方式来在每个控制器网站对象?

更新

对不起,我忘了补充一点,我已经使用IOC(结构图)。我的问题是线沿线的更多:


  1. 我应该替换别的东西BaseController? ActionFilter?

  2. 有没有一种方法来设定,让我没有给IWebsiteRepository传递给基类?

  3. 有没有更好的方式来处理用于数据域?


解决方案

其实我喜欢通过构造函数注入注入储备库的想法。它将使测试控制器,你可以简单地通过一个模拟库要容易得多。这另一种方法是使用一个静态工厂类获得的请求资源库,但使用静态类做单元测试更难了。

改进之一,我会提出的是,对你的控制器默认的构造函数调用与空值参数的构造。在参数构造函数,实例化正确的存储库,如果提供的参数为null。这样,您就不需要实现一个控制器工厂建立与它们的参数控制器;默认的控制器工厂可以使用参数构造函数,你仍然可以得到构造函数注入的好处。

 公共BaseController():这个(NULL){} 公共BaseController(IWebsiteRepository websiteRepository)
 {
     this._websiteRepository = websiteRepository?新WebsiteRepository();
 }

The setup: (using Asp.Net MVC 2 RC, Entity Framework, SQL Server, VS2008)

My buddy and I are working on a project that will have different domains pointing at it. We want to get the domain (website) out of the Request and use that to drive data. This website data needs to be part of all the controllers.

Ex. Data for domain1.website.com will be different than data for domain2.website.com, and those will be different than data for website2.com. The look of the site is the same for all these, but the data is different.

I set up a BaseController that all my other controllers inherit from. But I don't like it.

BaseController:

public class BaseController : Controller
{
    private Website _website;
    private readonly IWebsiteRepository _websiteRepository;

    public BaseController(IWebsiteRepository websiteRepository)
    {
        _websiteRepository = websiteRepository;
    }

    public Website CurrentWebsite { get; }
}

The problem with this is that I now need to pass an IWebsiteRepository to the base of each of my controllers:

public class BioController : BaseController
{
    private readonly IBiographyRepository _bioRepository;

    public BioController(IBiographyRepository bioRepository, IWebsiteRepository websiteRepository) 
        : base(websiteRepository)
    {
        _bioRepository = bioRepository;
    }
}

HERE ARE MY QUESTIONS

  1. Is there a better way to handle multiple domains being pointed at one project and filtering the data?
  2. Is there a better way to have the Website object in every controller?

UPDATE

Sorry, I forgot to add that in. I am already using IoC (structure map). My question is more along the lines of:

  1. Should I replace the BaseController with something else? ActionFilter?
  2. Is there a way to set it up so I didn't have to pass the IWebsiteRepository to the base class?
  3. Is there a better way to handle the domains used for data?

解决方案

I actually like the idea of injecting the repository via constructor injection. It will make testing the controllers much easier as you can simply pass in a mock repository. An alternative to this would be to use a static factory class to get the repository from a request, but using static classes makes unit testing harder.

One improvement that I would make is to have a default constructor for your controllers that calls the constructor with parameters with null values. In your constructor with parameters, instantiate the correct repository if the supplied parameter is null. This way you don't need to implement a controller factory to build the controllers with their parameters; the default controller factory can use the parameterless constructors and you still get the benefit of constructor injection.

 public BaseController() : this(null) { }

 public BaseController( IWebsiteRepository websiteRepository )
 {
     this._websiteRepository = websiteRepository ?? new WebsiteRepository();
 }

这篇关于Asp.Net MVC - 所有控制器通用数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆