Golang Dependency Control inversion (IoC)

Source: Internet
Author: User
Tags net domain

The mainstream development language, in order to achieve low coupling between projects, will be implemented by the IOC framework. Abstraction and implementation separation, using the abstraction layer, do not care about the concrete implementation of these layers of abstraction, the implementation of the abstraction layer can be implemented independently. Now the more popular domain-driven design (DDD), in order to achieve the domain layer as the most core, also needs to rely on the IOC.

Looking back, let's look at the Golang implementation of the IOC framework, with a Golang-style framework, and a heavier framework that moves from other mainstream languages. I think the current realization of the most lightweight, when the Martini framework of the IOC relies on the library github.com/codegangsta/inject. The number of lines of code, providing type registration, interface registration, type resolution, function injection, struct injection method, it can be said that the basic has been compared completely. From the beginning of the article it should be guessed that I am now studying DDD, currently in. NET domain began to learn in real-world projects by using edges. In practice, it is found that the IOC, in addition to the singleton mode (Singleton) support, should also have a temporary instance (Transient) support. That's why I wrote the IOC framework under Golang.

My purpose is simple, and I hope that IOC will not only support Singleton, but also support transient. The initial idea is to write an abstraction layer that supports the injection of both patterns. The transition part of the self-realization, and Singleton, the use of ready-made github.com/codegangsta/inject framework, and add a layer of adaptation. The implementation of transient is characterized by the need to create a new object for each parsing type (Resolve), which is independent from the previously created object. Here I use dependency injection to create new objects by type. There are no constructors in Golang, and I introduce the concept of constructors in order to initialize the object after it is created and before it is used. The interface of this constructor is actually very simple.

Initializer is to init a struct.type Initializer interface {    initfunc () interface{}}

Here I spit down the blog park, how to insert code, still do not support Golang ah?

This interface is simple, just a function that returns interface{}. In fact, the return should be another function, that is, the constructor. For example:

Func (Container *ioccontainer) Initfunc () interface{} {    return func () {        if !  container.isinitialized {            = &sync. rwmutex{}            = &Singletoncontainer{valuemapper:make (map[reflect. Type]reflect. Value)}            = &Transientcontainer{typemapper:make (map[reflect. Type]reflect. Type)}            true        }     }

When initialized, a constructor is called once, which completes an initialization operation. In fact, the same is true for Singleton, but also need to be initialized, but this initialization requires only the first time, and here will not be called only once (because the IOC framework does not know when you will be called first, here needs to be determined by the implementation of the constructor itself, Here you can use a field isinitialized to check whether it has been initialized).

All say Golang reflection, performance is very poor, I think part of the reflection of the function will be poor performance, but some should be OK. Now that the IOC framework is finished, test performance. Since the performance data was not saved before the adjustment, it was not shown. In short, before the revision, found inject package, in the resolve performance is very poor. After careful investigation, it was found that one of the implementations is intelligent, that is, when the Resolve interface type does not exist in the injected type, it attempts to turn the existing type into an interface, and returns if it can be converted. Because of the idea of Golang, there is no type tree. It is considered that the interface method is realized and the interface is realized, so the judgment itself becomes time consuming. And for that reason, I rewrote the singleton section, in Resolve, just judging by the type of incoming. If this type is singleton at the time of registration, it is singleton, and the original interface or type is taken out as is, without any conversion. It turns out that performance has improved.

The following are performance data:

1 routine, 3 times resolve Singleton and 1 times resolve transient per code invoke, invoke 1,000,000 times.

Result:

[commandprocessor] 2016/07/17 11:31:29 [info] requestContext.Invoke for 1000000 times with 1 routines execute in 4971.1971ms.[commandprocessor] 2016/07/17 11:31:34 [info] requestContext.Invoke for 1000000 times with 1 routines execute in 4951.494214ms.[commandprocessor] 2016/07/17 11:31:39 [info] requestContext.Invoke for 1000000 times with 1 routines execute in 4954.376794ms.

2 routine, 3 times resolve Singleton and 1 times resolve transient per code invoke, invoke 1,000,000 times.

Result:

[commandprocessor] 2016/07/17 11:23:50 [info] requestContext.Invoke for 1000000 times with 2 routines execute in 2779.720723ms.[commandprocessor] 2016/07/17 11:23:53 [info] requestContext.Invoke for 1000000 times with 2 routines execute in 2719.810844ms.[commandprocessor] 2016/07/17 11:23:56 [info] requestContext.Invoke for 1000000 times with 2 routines execute in 2734.028326ms.

Estimates down, almost 2 routine, 4 resolve action, 350,000/sec performance data.

I was doing a test on my laptop (i5 dual core), enabling 2 concurrent routine to test the resolve, one execution per test code, including Resolve4 calls. Test the code execution 35w times per second. This performance, I think in the business system development, do not need to consider the problem of performance loss.

---------------------------------Split Line-------------------------------------------------------------

My IOC project, already hanging on GitHub, is interested in getting to know the next. Http://https://github.com/Berkaroad/ioc

Install IOC package via go: Go get github.com/berkaroad/ioc

What's the problem in using, welcome to issue on GitHub, thank you!

Golang Dependency Control inversion (IoC)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.