Major changes in the alpha 2 release
After releasing MVC Forum alpha 1 a little over 2 weeks ago, we've been hard at work changing a lot of fundamental things. Alpha 1 has been all about getting a basic forum up and running, and telling the world that the project exists and that we're hard at work getting a forum solution ready for ASP.NET MVC solutions and sites. Getting alpha 1 released meant removing the additional data providers we've been working on (EF4.1 and NHibernate) to save time.
Now we're getting ready for another alpha release, this time the main focus is back on the architecture of the solution. We always knew that it wasn't enough to release a forum that could only run off my own ORM/DAL, not in 2011. So I've been working on finishing the data provider model, and in the process I've removed the stORM data provider (the only one included in alpha 1) and implemented an Entity Framework 4.1 data provider. This has already been committed to Codeplex. Next up is a NHibernate data provider, and part of implementing that, is using a Unit of Work pattern. The Unit of Work pattern is almost done, and with that, all the controllers of the forum and forum admin areas will be changed to inherit from ForumBaseController instead of directly from the Controller base class.
This has the added benefit of making it a lot easier to get the rest of your solution working just like the forum - the correct timezone and the correct language/region when displaying dates, getting texts, etc. All you have to do, is inherit from the ForumBaseController class.
Here's a little preview on what to expect:
[Themed]
public abstract class ForumBaseController : Controller {
protected ForumBaseController(IUserProvider userProvider, IUnitOfWork uow)
: base() {
// Some code left out here!!
}
protected Boolean Authenticated { get { return this.authenticated; } }
protected ForumUser ActiveUser { get { return this.user; } }
protected IUnitOfWork UnitOfWork { get { return this.uow; } }
protected IRepository<ForumUser> ForumUserRepository { get { return this.userRepository; } }
protected IRepository<TEntity> GetRepository<TEntity>() where TEntity : class {
return this.UnitOfWork.GetRepository<TEntity>();
}
}
So whenever you need to get the current user (if one is present), you can get it off the base class, or if you need a repository, instead of adding it to the growing list of contructor parameters, all you need to do is call the GetRepository method. Time for an example:
public class BasicInstallController : ForumBaseController {
private readonly IConfiguration config;
public BasicInstallController(IConfiguration config, IUnitOfWork uow, IUserProvider userProvider)
: base(userProvider, uow) {
this.config = config;
}
[HttpPost]
public ActionResult Index(BasicInstallViewModel model) {
IRepository<ForumSettings> settingsRepo = this.UnitOfWork.GetRepository<ForumSettings>();
// Lots of code left out here!!!
this.UnitOfWork.Commit();
return RedirectToAction("Index", "Home", new { area = "Forum" });
}
}
This example is taken from the BasicInstall controller, as you can see. Important things to notice here is the this.UnitOfWork.Commit(). Whenever you've changed any objects (create, delete, update), you need to commit these changes, or they'll just be lost whenever the request is done!
The downside of this approach is that all constructors will have to include at least 2 parameters (IUserRepository and IUnitOfWork). Instead of having these 2 parameters, I've been thinking about doing property injection, but I'm not really sure yet. This of course would mean that the DI implementations used with MVC Forum must support property injection. I have no idea how common this is.
Categories: MVC Forum - ASP.NET MVC - Alpha 2
Posted by Steen F. Tøttrup on 28 September 2011 10:52. There are 0 comments.
Write new comment
