简体   繁体   中英

Connect and check out methods

I have a method to connect to tfs and check out files. I have to separate it into 2 methods because they won't occur consecutively. But I am not sure how to separate it into 2 methods because if I did the check out, it means I have to get the Credentials and project collection again?

public static void Connect(String server, string path)
        {
            try
            {
                Uri serverUri = new Uri(server + "/tfs");
                ICredentialsProvider credentials = new UICredentialsProvider();
                TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(serverUri, credentials);
                tpc.EnsureAuthenticated();

                VersionControlServer versionControl = tpc.GetService<VersionControlServer>();

                Workspace workspace = versionControl.TryGetWorkspace(path);
                workspace.PendEdit(path);

            }

I would suggest that you don't make the function static, then you can simply store the variables at class level (you can still store them at class level if they are static, but at least this way you have some scope as to the lifespan:

public class TfsWrapper
{
    private TfsTeamProjectCollection tpc = null;
    private VersionControlServer versionControl = null;

    public TfsWrapper(string server, ...)
    {
        try
        {
            Uri serverUri = new Uri(server + "/tfs");
            ICredentialsProvider credentials = new UICredentialsProvider();
            tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(serverUri, credentials);
            tpc.EnsureAuthenticated();
            versionControl = tpc.GetService<VersionControlServer>();
        }
    }

    public void Checkout(string path)
    {
        Workspace workspace = versionControl.TryGetWorkspace(path);
        workspace.PendEdit(path);
    }

I suggest you to use this code, he treats encapsulation & refactoring aspect between lot of servers & credentials

link : http://blogs.msdn.com/b/buckh/archive/2012/03/10/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM