Discuss dependency injection again

Source: Internet
Author: User

In the design systemProgramDuring development, it is often mentioned that this module depends on another module. If this module depends on another one, how do we deal with the dependency? Everyone has their own opinions on this issue.

Summarize and explore the commonly used dependency methods and their advantages and disadvantages in the project based on your understanding.

Before exploring this problem, assume a running scenario. Let's say that an interface provides the Time Service and the client gets the time. This is only a scenario where you can describe some problems. The initial design of this problem is as follows:Code, A class that provides services, obtained at the client call time.

 Class  Sqltimeprovider
{
Public String Currenttime
{
Get
{
// SQL select omitted
Return " Select getdate () " ;
}
}
}
Class Dbclient
{
Public String Gettime ()
{
Return New Sqltimeprovider (). currenttime;
}
}
 
In the preceding example, the time service for obtaining the SQL Server is provided by defining classes separately. What should we do if the server needs to provide the time service from other sources? interface, therefore, an interface is provided based on the time service,
 
Determine the service to be called Based on the Interface instantiation. refer to the following code:
    Interface  Itimerprovider
{
String Currenttime { Get ;}
}
Class Sqltimeprovider: itimerprovider
{
Public String Currenttime
{
Get
{
// SQL select omitted
Return " Select getdate () " ;
}
}
}
Class Sqlitetimeprovider: itimerprovider
{
Public String Currenttime
{
Get
{
// SQLite select omitted
Return " Select date () " ;
}
}
}
Class Dbclient
{
Public String Gettime ()
{
Itimerprovider itime = New Sqltimeprovider ();
// Itime = new sqlitetimeprovider ();
Return Itime. currenttime;
}
}

In this way, the client depends on the interface when calling, and the server can write it freely. The interface must be called and processed on the interface in a unified manner. But this is still unpleasant, when calling the client, I must know that there is a sqlitetimeprovider to instantiate the interface. To make the process more dynamic, I also need to add an additional class to analyze the data to be processed and the client obtains the data, I will directly currenttime, regardless of the time you read from SQLite or the time you read from SQL. This is not required for the client, you only need to know that you need to display a suitable time format. Add an analysis object for dynamic parsing. The Code is as follows:

 Public     Class  Analysis
{
Dictionary < Type, type > Timedictionary = New Dictionary < Type, type > ();
Public Analysis ()
{
Timedictionary. Add ( Typeof (Itimerprovider ), Typeof (Sqlitetimeprovider ));
}
Public Object Create (type curtype)
{
Return Activator. createinstance (timedictionary [curtype]);
}
Public T create < T > ()
{
Return (T) Create ( Typeof (T ));
}
}

Next, the client will call the corresponding method to obtain the time. There are several methods. The first one is to implement injection when the client constructor is used, pass the specific instance to the client through the constructor parameter. This method has direct rows, one time,

This method receives parameters in the initial State of a class, and the object is not empty in subsequent calls. At the same time, this method cannot be modified once passed in because the instance has been created. The code for Constructor injection is as follows:

  class   formtest 
{< br> Public void Showtime ()
{< br> itimerprovider timerprovider = ( New analysis ()). create itimerprovider > ();
// show...
New dbclient (timerprovider ). gettime ();
}< BR >}

In this way, the dbclient receives an itimerprovider parameter and transmits the corresponding business to it for display time. This method may be familiar to everyone, it is to pass several interfaces as parameters to an object in the constructor, which is the constructor injection in dependency injection.

The Subject Code has already been presented, followed by common dependency injection methods, as well as the attribute assignment method. In this example, an interface is directly assigned a value to an object attribute, this method is flexible. You can assign values and read values when you need to use the object. This method can be used in combination with the actual project. Interface injection and property injection are also extended. You can follow up on the actual situation and come up with many practical project application methods. However, in general, the dependency is only used as much as possible, and the corresponding modifications are required without adding any clients to the server, which is too "dependent. Note that some of the knowledge in this book is based on the design model of Mr. Wang Xiang. It is a good book. We hope you can discuss other specific application methods to make projects easier to develop, maintain, and manage.

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.