Because of the work I am busy lately to interact, via code, using the object model, with TFS . I would like to start a mini series dedicated to tips and short articles on the use of certain classes in the same object model. The series is not meant to be exhaustive but is intended as my habit of sharing know-how.
This post covers the initial part of every interaction with TFS: The connection to the server.
If we want to create client applications that interact with Team Foundation Server 2010, we can use the object model made available by libraries Microsoft.TeamFoundation .*. dll.
In particular to connect to the server you need to reference libraries and Microsoft.TeamFoundation.Client.dll Microsoft.TeamFoundation.Common.dll.
The connection to the TFS server (in order to obtain services for the management of the entity's own server) can be done using the class contained within the namespace TFSConfigurationServer Microsoft.TeamFoundation.Client (Microsoft.TeamFoundation.Client assembly. dll).
E 'can create an instance of TFSConfigurationServer in two ways: Using the manufacturer
- :
- Dim tfsInstance1 = New TfsConfigurationServer ( New Uri ( "http://server:8080/tfs" ) New UICredentialsProvider ())
- Using the factory class TFSConfigurationServerFactory:
- Dim tfsInstance1 = TfsConfigurationServerFactory . GetConfigurationServer ( New Uri ( "http://server:8080/tfs" ) New UICredentialsProvider ())
In both cases the address http://server:8080 / tfs must be a valid address to connect to TFS. We can get the address using the Team Explorer. And 'Just open the properties of a collection of Team Explorer to get the address to be included in our classrooms:
Both manufacturer who make available factory overload to specify the login credentials, but at this time, non ce ne preoccupiamo e come provider di credenziali utilizziamo la classe UICredentialProvider che permette, se TfsConfigurationServer non trova delle credenziali in cache, di richiederle all’utente con un prompt.
Quello che ci interessa in questo post è mettere in evidenza la sostanziale differenza tra costruttore e factory e cioè il fatto che il costruttore permette di ottenere differenti istanze di conessione  (una per ogni New) mentre la factory crea una nuova istanza la prima volta che viene richiamato il metodo GetConfigurationServer e restituisce la stessa istanza le volte successive.
Per verificare questo non possimao utilizzare il metodo Equals delal classe Object perchè tale metodo e ridefinito in the class from which the TfsConnection TfsConfigurationServer comes to compare two instances of the class defined as equal when they have the same URL. Then write the following code:
- Dim tfsInstance1 = New TfsConfigurationServer ( New Uri ( "http://server:8080/tfs" )
- & # 160; , & # 160; ; New UICredentialsProvider ()) Dim
- tfsInstance2 = New TfsConfigurationServer ( New Type ( "http://server:8080/tfs" )
- & # 160; & # 160; New UICredentialsProvider ())
- tfsInstance1 . Dispose ()
Following the call to the Dispose method, if the instances are distinct, tfsInstance1 will be "provisions" while tfsInstance2 not.
To test this we insert the QuickWatch in our project to highlight the Disposed property values \u200b\u200b(which are not directly visible because Friend) and we observe that, after Dispose has:
Let the same thing for the instances generated by the Factory:
- Dim
- tfsInstance1 = TfsConfigurationServerFactory . GetConfigurationServer ( New Type ( "http://server:8080/tfs" ) &
- # 160; : & # 160; : & # 160; ; New UICredentialsProvider ())
- Dim tfsInstance2 = TfsConfigurationServerFactory .GetConfigurationServer( New Uri ( "http://server:8080/tfs" ),
-                                                                      New UICredentialsProvider ())
- tfsInstance1 . Dispose ()
In this case, tell us QuickWatch:
If this were not enough, we can analyze GetConfigeurationServer is implemented as the method of using TfsConfigurationServerFactory Reflector :
The class implements a sort of cache (this is a Dictionary (Of Uri, TfsConfigurationServer)) in which it is sought any instance already created ( Step 1) and, in this case it is returned. If there is an instance for the URI passed as an argument, the factory uses the constructor to instantiate a new TfsConfigurationServer and stores in its cache (step 2).
0 comments:
Post a Comment