Asp.Net MVC - Common Data Across All Controllers
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)
{
_bioRepo开发者_JS百科sitory = bioRepository;
}
}
HERE ARE MY QUESTIONS
- Is there a better way to handle multiple domains being pointed at one project and filtering the data?
- 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:
- Should I replace the BaseController with something else? ActionFilter?
- Is there a way to set it up so I didn't have to pass the IWebsiteRepository to the base class?
- 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();
}
IoC will help you.
http://weblogs.asp.net/sfeldman/archive/2008/02/14/understanding-ioc-container.aspx
Tool: http://structuremap.sourceforge.net/Default.htm - for example
精彩评论