Skip to content
Johnwii.Chang edited this page Jun 28, 2019 · 3 revisions

Model Validator

For developers writing business code, it is essential to check the data. But these data are not too much related to the actual business logic, but it is necessary to make the code look like a bunch of wool balls, and lose confidence in maintenance over time.

To solve this question, WebAPI provide you an interface to move your defensive coding to one place. Add Check() error function to your model, and it will be automatically invoked before request arriving your actual logics. You can also do some data format transformation when your method is ptr based.

Serializer

Automatic serialization and deserialization were mentioned earlier. If you do not manually change the settings for serialization and deserialization, the default JSON implementation is used by default. Of course, you can also introduce other standards such as MsgPack externally.

Defining a serializer only requires an implementation of two methods:

//Serializer Serializer
Serializer interface {
	Marshal(interface{}) ([]byte, error)
	Unmarshal([]byte, interface{}) error
}

then set the context with the middleware.

If you are trying to use this extension, you might get to know that the latest serialiser design is changing to separate serialiser and deserialiser in the context. The serialiser is working for reply and another is body limited.

And you need to know that the request header will let WebAPI select the right deserialiser, and there are JSON and URLEncode support build-in. So, if you are trying to send some information with url-encode format, WebAPI can indicate it without any extra supports. However, you ought to notice that if there is an unknown Content-Type, the body object is a default one and you need to read the body manually(you can get it with Controller.Context().Body() or if you need a reader, you can still use Controller.Context().GetRequest().Body).

CryptoService

The encryption and decryption security service is used in the same way as the serializer. The required implementation methods are:

//CryptoService Cryptography service
CryptoService interface {
	Encrypt([]byte) []byte
	Decrypt([]byte) ([]byte, error)
}

Responsive Structure

The replyable structure is the default response behavior of the system when the value is returned. Any structure that conforms to the replyable structure can be recognized and responded correctly by WebAPI.

//Replyable Responsive for request reply
Replyable interface {
	StatusCode() int
	Data() interface{}
}

Alias of Controller

Different controllers can be properly isolated during writing to achieve different controllers to focus on different fields, and it is also convenient for multi-person collaborative development. It's very easy to assign a name to the controller. You only need to implement a method called RouteAlias() string.

Clone this wiki locally