tsframework / ts-framework

A Web Framework for Nodejs
http://tsframework.github.io
MIT License
43 stars 6 forks source link

Result.ts proposals #14

Closed Paradoxis closed 8 years ago

Paradoxis commented 8 years ago

Me and @petrck were discussing the file Results.ts, which seems to be result types for controllers.

The first thing that came up was that the name was probably not optimal, renaming it to ControllerResult or ControllerResponse would be a more describing name, as it's currently not clear what it does.

Secondly there's the issue of Application being referenced in all of the methods (and only being used twice, just for the root directory of the application). Snippet:

export interface IActionResult 
{
    execute(Application, Response): void;
}

// ... FileResult, DownloadResult
var file = path.join(app.root, this.path); 

I suggest the best way to fix this is by passing the rootDirectory to every controller, and having the controller pass it on to the actionresult like so:


// Application.ts
let controller = new Controller(this.rootDirectory);

// Controller.ts
export class Controller
{
    public constructor(private rootDirectory: string) { ... }
}

// ControllerResult.ts
export class IActionResult
{
    execute(response: Response, rootDirectory: string);
}

// SomeController.ts
export class SomeController extends Controller
{
    public someAction(request: Request, response: Response): IActionResult
    {
        return new SubClassOfActionResult(this.rootDirectory, response); // <-- this should probably be automatic, I never really used action results in the framework
    }
}