iris/mvc2
2017-12-15 20:28:06 +02:00
..
di remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
ideas/1 remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
bind.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
controller_handle_test.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
controller_method_parser.go split the controller's method lexer and parser to another file to simplify the code, we need more steps to simplify it enough before pushing to master 2017-12-11 06:51:52 +02:00
controller_test.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
controller.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
engine_handler_test.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
engine.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
func_result_test.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
func_result.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
handler_test.go add test for binding a whole function as an input argument on the handler's function - worked 2017-12-14 03:30:39 +02:00
handler.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
mvc.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
path_param_binder_test.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
path_param_binder.go ok the new mvc system works great, all tests done and almost x3 smaller LOC used plus new three awesome features:) - next commit will be commenting out and replace the mvc package with the new mvc2 2017-12-10 07:00:51 +02:00
README.md add an overview of the new semantics at the mvc2/README.md 2017-12-11 06:24:55 +02:00
reflect.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
session_controller.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +02:00
session.go remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers 2017-12-15 20:28:06 +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
  • 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 Input 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
  • C
    • Struct fields with Struct Binding
    • Methods with Dynamic Binding

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 {
    C

    // 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

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) {
    // ...
}

Bind

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 Bind 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 := New().Bind(myBinder)

h := m.Handler(myHandler)

For Controllers

app := iris.New()
New().Bind(myBinder).Controller(app, new(myController))
// ...
sub := app.Party("/sub")
New().Controller(sub, &myController{service: myService})
New().Controller(sub.Party("/subsub"), new(myController), func(ca *ControllerActivator) {
    ca.Bind(myService)
})