简体   繁体   English

连接并签出方法

[英]Connect and check out methods

I have a method to connect to tfs and check out files. 我有一种方法可以连接到tfs并检出文件。 I have to separate it into 2 methods because they won't occur consecutively. 我必须将其分为2种方法,因为它们不会连续发生。 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 链接: http : //blogs.msdn.com/b/buckh/archive/2012/03/10/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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