是否使用依赖注入模式来创建专业代码


Is use of the dependency injection pattern necessary to create professional code?

我最近更新了我的PHP库以使用依赖注入模式。 我没有在我的类中实例化对象,而是在一个名为 ObjectMaker 的类中实例化它们,并通过对象"getter"函数传递对象。

我能看到这样做的唯一一点是隔离由实例化引起的故障。

但是由于我只是一个人,而且我没有做任何单元测试,我认为更新我的 JavaScript 以使用依赖注入模式没有任何意义。

这些依赖项不会给我带来任何问题。

我可以不使用它,仍然打电话给我的JavaScript专业人士吗?

我不会看到使用它的任何性能提升,只是更抽象的代码(对象创建对象) - 我更喜欢一个简单的对象模型,没有太多的"模式"使用。 这是样式偏好正确吗?

问题?

我可以不使用依赖注入模式,仍然称我的 JavaScript 专业水平吗?

/**
 *Control
 */
var Control = ( function () 
{
    var Control = function ( type )
    {
        this.TIME = 4000;
        this.form_element = document.getElementById( type ), 
        this.response_element = document.getElementById( type + '_response' );
        this.text_object = new Text( this.form_element ),
        this.message_object = new Message( this.response_element ),
        this.effects_object= new Effects( this.response_element );
    };
    Control.prototype.invoke = function( ) 
    {
        if( Global.validate_input_on === 1 )
        {
            if( !this.text_object.checkEmpty() ) 
            {
                this.message_object.display( 'empty' );
                this.effects_object.fade( 'down', this.TIME );
                return false;
            }
            if( type === 'signup' && !this.text_object.checkPattern( 'name' ) ) 
            {
                this.message_object.display( 'name' );
                this.effects_object.fade( 'down', this.TIME );
                return false;
            }
            if( !this.text_object.checkPattern( 'email' ) ) 
            {
                this.message_object.display( 'email' );
                this.effects_object.fade( 'down', this.TIME );
                return false;
            }
            if( !this.text_object.checkPattern( 'pass' ) ) 
            {
                this.message_object.display( 'pass' );
                this.effects_object.fade( 'down', this.TIME );
                return false;
           }
        }
        var response_element = this.response_element;
        AjaxNew.repeatUse( ajaxSerialize( this.form_element ) + '&ajax_type=' + type + '_control', function( server_response_text ) { ajaxType( server_response_text, this.response_element, 'respond' ); } );
    };
    Control.in = function()
    {
        new Control( 'signin' ).invoke();
    };
    Control.up = function()
    {
        new Control( 'signup' ).invoke();
    };
    Control.out = function()
    {
        AjaxNew.repeatUse( '&ajax_type=ControlSignOut', function( server_response_text ) { ajaxType( server_response_text, 0, 'simple' ); } );
    };
    Control.try = function()
    {
        AjaxNew.repeatUse( '&ajax_type=ControlTryIt', function( server_response_text ) { ajaxType( server_response_text, 0, 'simple' ); } );
    };
    return Control;
} () );

你可能会冒着与其他开发人员一起不是一个很酷的孩子的风险,但这并不意味着你的代码不那么专业。 使用最适合您项目的方法。 如果它对你想要做的事情来说是矫枉过正的,不要使用它。

我能看到这样做的唯一一点是隔离由实例化引起的故障。

依赖注入的目的是促进松散耦合。 无论您在JS代码中是否需要它,我想说很大程度上取决于您的JS代码的范围;即您是在编写JS框架,还是在操作一些div?

网络开发公司会找到您的工作专业人士吗? 这要看情况。 您是无端地将 DI 添加到您的代码中,以便看起来很酷,还是它有一些真正的理由存在? 专业的网络公司会知道其中的区别。