1

Is there any MVC framework around that applies POCO concept to controllers ?

Example:
Suppose I don't want to make my controller dependent on any web framework, in the way that I don't need to inherit from framework Controller class anymore.
Instead, I want to just specify routes with some rules, describing how to map requests to methods and parameters of my POCO controller class instance.

Maybe little example below can explain it better.
Imagine we can have routing like this:

routes.MapPOCOController<MyPOCO>("MyWebAppName/{method}/{*parameters}", () => _container.Resolve<MyPOCO>());
routes.IfMethodReturns<MyCustomRedirectResult>().PerformRedirect(result => result.Url);

and then your controller class is just a POCO object:

public class MyPOCO {
    public User     GetUser(Guid userId) {...}
    public string   Home() { 
        return _razorEngine.Render("~/Views/Home/Index.cshtml", new HomeModel { Message = "Hello world!"});
    }
    public object   RenderSomePageOrRedirect()
    {
        if(blabla)
            return new MyCustomRedirectResult("/profile");
        else
            return _razorEngine.Render( ... );
    }
    ...
}
  • In first method "GetUser" JSON serialization could happen automagically like in mvc4.
  • In second method "Home" we need to render our views manually as a string, so this would give you more flexibility, like combining different engines or writing a custom one in much more natural way than it happens in current asp.mvc implementation.
  • In third method "RenderSomePageOrRedirect" we are not tight to current RedirectResult implementation and can use whatever class that works, and then map it properly in routes.

Is there any framework around that already implemented at least one of the mentioned concepts ? ... maybe some "contrib" library?

The reasons why I'm looking for such functionality are:

  • I want to follow DRY principle and don't want to mix request/response mapping code with the business logic in the controller (you can't do both with current mvc implementations that I'm aware of)
  • I want to make my controller classes usable in multiple contexts, so, it's easier for unit-testing, for reusing and so on. (code becomes more DRY when you reusing same controllers in different web applications: you simply need to adjust routes a bit)
  • It's just a gut feeling that this way of doing things is better, sorry, my words renderer gives up (

PS: my appreciation to people (StriplingWarrior especially) who commented below and helped me to form this question properly!

tereško
  • 58,060
  • 25
  • 98
  • 150
IlliakaillI
  • 1,510
  • 13
  • 25
  • How would you host your controllers, etc with no framework to interact with the web server? – Maess May 15 '12 at 19:16
  • 3
    So you want to create your own MVC framework? – jrummell May 15 '12 at 19:16
  • It seems to me that the controller is inherently framework (and language)-specific. How would you plan on taking advantage of that abstraction? – Jeremy Holovacs May 15 '12 at 19:16
  • @jrummell I want to have more options to map web requests to methods and parameters of the class. The way how ASP.MVC does that is not the best way in some situations. – IlliakaillI May 15 '12 at 20:06
  • @jeremy-holovacs imagine we have a service class that does some useful stuff. I want to expose that class as a web-service. With current implementations I need to create a controller class that does that mapping. As alternative - I want to have an option to say: map all requests to "MyServiceClass" instance using specific factory. Does it make sense? – IlliakaillI May 15 '12 at 20:12
  • >>It seems to me that the controller is inherently framework (and language)-specific. looks like it inherently framework-specific only in current inplementations (because they force you to *inherit* from their base class, haha) – IlliakaillI May 15 '12 at 20:15
  • 2
    @ILICH: Well I would say that a properly abstracted business layer referenced by the framework controllers and some judicious routing would solve your problems without reinventing a reasonably reliable wheel. – Jeremy Holovacs May 15 '12 at 20:25
  • @jeremy-holovacs so, you want me to duplicate all methods of my service in controller? create an wrapper that just delegates calls? It violates DRY principle and it's definitely a smell! – IlliakaillI May 15 '12 at 20:34
  • @ILICH: It does not violate DRY principal to invoke methods from multiple user interfaces; that's exactly the purpose of DRY, so you can use the same business logic in different ways. Besides, I think you missed the part about judicious routing. Properly done, your URL can provide the metadata required for you to write a single service call, or at the most a small group of them. Abstracting the controller would definitely require you rewriting all the base class code, now that violates DRY. – Jeremy Holovacs May 15 '12 at 20:50
  • 1
    @ILICH: Could you provide a code sample to show how you would expect such a controller to work? For example, if you want your controller to double as a service, what type of object would its actions return? How would those objects get rendered when in MVC mode versus Service mode? I think the idea behind this question has some merit, but as it stands you haven't explained your intent with enough detail to yield a decent answer. – StriplingWarrior May 15 '12 at 20:52
  • @JeremyHolovacs >>Properly done, your URL can provide the metadata required for you to write a single service call ... now it smells like "hidden contract" antipattern – IlliakaillI May 15 '12 at 21:48
  • @JeremyHolovacs >>Abstracting the controller would definitely require you rewriting all the base class code ... - I don't think so – IlliakaillI May 15 '12 at 21:49
  • @StriplingWarrior thanks for suggestions! I just need "Service mode", but in "MVC mode" it could behave as the current asp.mvc framework does, but maybe with use of some static helpers to generate different action results – IlliakaillI May 15 '12 at 22:07
  • @ILICH: I didn't say it was good idea, I just said you could do what you seem to want to do that way without reinventing the wheel. How would you use a controller class without rewriting the interfaces required for the system to use it? The framework depends on controllers implementing interfaces to link into. Plugging your controllers into some other framework would require a "meta" framework that there does not seem to be any reason to build. – Jeremy Holovacs May 15 '12 at 22:53
  • 2
    @ILICH: That's not enough code to flesh out your question. What would `MyService` look like? What kind of results would its actions return? You want `MyService` to effectively double as both the controller and the service, right? If so, what kind of transformation would be performed on the return types of its methods to make them renderable in views? If you want anybody else to take your question seriously, you need to show that you've given it serious thought. – StriplingWarrior May 15 '12 at 22:54
  • @StriplingWarrior I updated the question, please let me know if it's full enough or I can add something more in. Thanks for your help! – IlliakaillI May 16 '12 at 16:39

2 Answers2

2

AFAIK there's isn't such framework. ASP.NET MVC controllers must derive from the Controller, AsyncController or the ApiController class. So I guess you will have to write your own MVC framework if you want to implement such concepts.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

Actually there is FubuMvc . It seems to be pretty much what you're looking for. The only problem is scarce documentation.

MikeSW
  • 16,140
  • 3
  • 39
  • 53