iris/mvc
2017-12-25 20:05:32 +02:00
..
di finish the first state of the vuejs todo mvc example, a simple rest api - todo: websocket and live updates between browser tabs with the same session id 2017-12-23 17:07:39 +02:00
ideas/1 fix all _examples to the newest mvc, add comments to those examples and add a package-level .Configure in order to make it easier for new users. Add a deprecated panic if app.Controller is used with a small tutorial and future resource link so they can re-write their mvc app's definitions 2017-12-20 08:33:53 +02:00
AUTHORS create a new package, name it as hero, I was thinking super or superb but hero is better name for what it does - the goal is to split the new 'mvc handlers' from the mvc system because they are not the same, users should know that they can use these type of rich binded handlers without controllers as well, like a normal handler and that I implemented here, the old files exist on the mvc package but will be removed at the next commit, I have to decide if we want type aliases for Result or no 2017-12-25 20:05:32 +02:00
controller_handle_test.go more checks about creating new instance of controller on each request - this time if all bindings are static then set them to the initial-devpassed controller and if the total number of lengths are equal with these static dependencies then we ignore the injector and use the initial controller on each request - maximize the performance when simple controller is used - need more cleanup before new release but I hope until Christmas iris developers will be amazed 2017-12-18 00:16:10 +02:00
controller_method_parser.go don't create a new controller instance if it doesn't have struct dependencies and the fields length is 0 - 0.4MB/s difference from the raw handlers now. 2017-12-16 17:57:20 +02:00
controller_method_result_test.go create a new package, name it as hero, I was thinking super or superb but hero is better name for what it does - the goal is to split the new 'mvc handlers' from the mvc system because they are not the same, users should know that they can use these type of rich binded handlers without controllers as well, like a normal handler and that I implemented here, the old files exist on the mvc package but will be removed at the next commit, I have to decide if we want type aliases for Result or no 2017-12-25 20:05:32 +02:00
controller_test.go fix all _examples to the newest mvc, add comments to those examples and add a package-level .Configure in order to make it easier for new users. Add a deprecated panic if app.Controller is used with a small tutorial and future resource link so they can re-write their mvc app's definitions 2017-12-20 08:33:53 +02:00
controller.go add some comment docs at the mvc/controller.go 2017-12-22 11:07:13 +02:00
engine_handler_test.go OK, my dream-idea is implemented. TODO: Some examples and doc.go is not updated yet, comments on the mvc/di subpackage, the tutorial/vuejs-todo-mvc is running but not finished yet (it's using browser's localstorage and it should be replaced by the http requests that are registered via iris mvc 2017-12-16 06:38:28 +02:00
engine.go fix all _examples to the newest mvc, add comments to those examples and add a package-level .Configure in order to make it easier for new users. Add a deprecated panic if app.Controller is used with a small tutorial and future resource link so they can re-write their mvc app's definitions 2017-12-20 08:33:53 +02:00
func_result.go ok make it cleaner, it's working well and blazing fast but I have to do a lot cleaning and commenting and docs as well before push it to master --- hope at christmas day, also thinking some internal ideas - the whole code is not ready to be readen by a third person yet. 2017-12-19 23:40:42 +02:00
handler_test.go OK, my dream-idea is implemented. TODO: Some examples and doc.go is not updated yet, comments on the mvc/di subpackage, the tutorial/vuejs-todo-mvc is running but not finished yet (it's using browser's localstorage and it should be replaced by the http requests that are registered via iris mvc 2017-12-16 06:38:28 +02:00
handler.go fix all _examples to the newest mvc, add comments to those examples and add a package-level .Configure in order to make it easier for new users. Add a deprecated panic if app.Controller is used with a small tutorial and future resource link so they can re-write their mvc app's definitions 2017-12-20 08:33:53 +02:00
LICENSE OK, my dream-idea is implemented. TODO: Some examples and doc.go is not updated yet, comments on the mvc/di subpackage, the tutorial/vuejs-todo-mvc is running but not finished yet (it's using browser's localstorage and it should be replaced by the http requests that are registered via iris mvc 2017-12-16 06:38:28 +02:00
mvc.go misspell fix 2017-12-22 11:08:16 +02:00
param_test.go fix all _examples to the newest mvc, add comments to those examples and add a package-level .Configure in order to make it easier for new users. Add a deprecated panic if app.Controller is used with a small tutorial and future resource link so they can re-write their mvc app's definitions 2017-12-20 08:33:53 +02:00
param.go don't create a new controller instance if it doesn't have struct dependencies and the fields length is 0 - 0.4MB/s difference from the raw handlers now. 2017-12-16 17:57:20 +02:00
README.md more checks about creating new instance of controller on each request - this time if all bindings are static then set them to the initial-devpassed controller and if the total number of lengths are equal with these static dependencies then we ignore the injector and use the initial controller on each request - maximize the performance when simple controller is used - need more cleanup before new release but I hope until Christmas iris developers will be amazed 2017-12-18 00:16:10 +02:00
reflect.go OK, my dream-idea is implemented. TODO: Some examples and doc.go is not updated yet, comments on the mvc/di subpackage, the tutorial/vuejs-todo-mvc is running but not finished yet (it's using browser's localstorage and it should be replaced by the http requests that are registered via iris mvc 2017-12-16 06:38:28 +02:00
session_controller.go update the documents for the new MVC API and some of its new features but not finished yet, README big examples and _examples/mvc/login is are yet updated and I'm thinking of removing the big examples on README.md, they exist on the _examples/mvc updated. 2017-12-22 10:18:31 +02:00
session.go create a new package, name it as hero, I was thinking super or superb but hero is better name for what it does - the goal is to split the new 'mvc handlers' from the mvc system because they are not the same, users should know that they can use these type of rich binded handlers without controllers as well, like a normal handler and that I implemented here, the old files exist on the mvc package but will be removed at the next commit, I have to decide if we want type aliases for Result or no 2017-12-25 20:05:32 +02:00

MVC Internals

  • MakeHandler - accepts a function which accepts any input and outputs any result, and any optional values that will be used as binders, if needed they will be converted in order to be faster at serve-time. Returns a context/iris#Handler and a non-nil error if passed function cannot be wrapped to a raw context/iris#Handler
    • Struct fields with Struct Binding
    • Methods with Dynamic Binding
  • Engine - The "manager" of the controllers and handlers, can be grouped and an Engine can have any number of children.
    • Engine#Bind Binds values to be used inside on one or more handlers and controllers
    • Engine#Handler - Creates and returns a new mvc handler, which accept any input parameters (calculated by the binders) and output any result which will be sent as a response to the HTTP Client. Calls the MakeHandler with the Engine's Dependencies.Values as the binders
    • Engine#Controller - Creates and activates a controller based on a struct which has the C as an embedded , anonymous, field and defines methods to be used as routes. Can accept any optional activator listeners in order to bind any custom routes or change the bindings, called once at startup.
  • The highest level feature of this package is the Application which contains an iris.Party as its Router and an Engine. A new Application is created with New(iris.Party) and registers a new Engine for itself, Engines can be shared via the Application#NewChild or by manually creating an &Application{ Engine: engine, Router: subRouter }. The Application is being used to build complete mvc applications through controllers, it doesn't contain any method to convert mvc handlers to raw handlers, although its Engine can do that but it's not a common scenario.

Examples can be found at: https://github.com/kataras/iris/tree/master/_examples/mvc.

Binding

First of all, they can be binded to func input arguments (custom handlers) or struct fields (controllers). We will use the term Input for both of them.

// consume the user here as func input argument.
func myHandler(user User) {}

type myController struct {
    // consume the user here, as struct field.
    user User 
}

If the input is an interface then it will check if the binding is completed this interface and it will be binded as expected.

Two types of binders are supported:

Dynamic Binding

ReturnValue, should return a single value, no pointer to, if the consumer Input (struct field or func input argument) expects User then it will be binded on each request, this is a dynamic binding based on the Context.

type User struct {
    Username string
}

myBinder := func(ctx iris.Context) User {
    return User {
        Username: ctx.Params().Get("username"),
    }
}

myHandler := func(user User) {
    // ...
}

Static Binding

Static Value (Service), this is used to bind a value instance, like a service or a database connection.

// optional but we declare interface most of the times to 
// be easier to switch from production to testing or local and visa versa.
// If input is an interface then it will check if the binding is completed this interface
// and it will be binded as expected.
type Service interface { 
    Do() string
}

type myProductionService struct {
    text string
}
func (s *myProductionService) Do() string {
    return s.text
}

myService := &myProductionService{text: "something"}

myHandler := func(service Service) {
    // ...
}

Add Dependencies

For Handlers

MakeHandler is used to create a handler based on a function which can accept any input arguments and export any output arguments, the input arguments can be dynamic path parameters or custom binders.

h, err := MakeHandler(myHandler, reflect.ValueOf(myBinder))

Values passed in Dependencies are binded to all handlers and controllers that are expected a type of the returned value, in this case the myBinder indicates a dynamic/serve-time function which returns a User, as shown above.

m := NewEngine()
m.Dependencies.Add(myBinder)

h := m.Handler(myHandler)

For Controllers

app := iris.New()
m := NewEngine()
m.Dependencies.Add(myBinder)
m.Controller(app, new(myController))
// ...
sub := app.Party("/sub")
m := NewEngine()
m.Controller(sub, &myController{service: myService})
NewEngine().Controller(sub.Party("/subsub"), new(myController), func(b mvc.BeforeActivation) {
    b.Dependencies().Add(myService)
})