I just wanted to solve the most common things. Validation, logging, permissions are the obvious ones, I find the implementation of these in microsofts MVC framework to be pretty terrible, it's almost always one of the first things I replace. I like the command/query model, so I wanted that in place. I want to be able to offer desktop/mobile integration further down the line. I want easy integration with knockoutjs. Most importantly, I don't want to have to set all this up every single time.
So as I'm sporadically putting various parts together I noticed one thing. MS MVC is getting in my way and stopping me from creating this framework, It must go! So what was once going to be an application framework is now going to be MVC framework as well.
A touch of arrogance
I think if your going to replace something then you need to replace it with something better, which requires at least a modest level of arrogance. You have to know how what you want to do better and hopefully have a rough idea on how to get there.
As I got further through my exploratory and brainstorming phase I started to get more confident that yes, I can make a better MVC.
Controllers
The first thing I started looking at is what I wanted my controllers to look like. I came up with this:
- public abstract class Controller {
- [Route("/product/view/*/{Id}")]
- public abstract object View(ProductDetailsQuery query);
- public abstract object List(ProductListQuery query);
- [Roles("ProductManager")]
- [HttpPost]
- public object Create(ProductCreateCommand command) {
- System.Execute(command);
- User.Message(MessageType.Success, "Product has been created");
- return User.Redirect("/Product/List");
- }
- }
There are a number of interesting things here, First of all, the controller is responsible for defining a public API, so routes and permissions are defined by attributes. MS MVC has recently added attribute based routing as well.
A permissions system will be provided by the framework that will allow role based and finer grained permissions.
The controller is abstract. For many common actions like view and list in the above sample, all the controller does is delegate responsibility. Abstract actions will have the ability to be routed by default. Saving those couple of lines of code was one of the main drivers for my framework, yes I'm that anal.
The HttpPost may seem fairly standard, but by convention this will enlist a transaction.
All action return objects. There are no view results, json results or anything like that. It's one object in, one object out. The format of the result is determined by the caller.
We don't have to check if ModelState.IsValid on *every single function*. That is done for us.
It's just called Controller. I will mention the project layout at a later date.
There are a couple of big interfaces in there, User and System. These are basically wrappers for real objects. I really like this abstraction because it makes it explicit who where interacting with. Where not interacting with NotificationSubSystem, where interacting with the user. We aren't interacting with the command executor, we are interacting with the system.
Fingers Crossed
This is the current plan any way. Some ideas may turn out to be bad ones, some might be brilliant and there could be many more additions. Finding out which is which should be a fun journey.
No comments:
Post a Comment