使几个类似的特定方法通用

时间:2016-06-03 09:31:51

标签: c#

我有一个像这样的方法......

    static async Task GetSupplierProducts(ForceClient client)
    {
        Console.WriteLine("Get SupplierProduct");
        var accts = new List<SupplierProduct>();
        var results = await client.QueryAsync<SupplierProduct>(SupplierProduct._select);
        var totalSize = results.TotalSize;
        Console.WriteLine("Queried " + totalSize + " SupplierProduct.");
        accts.AddRange(results.Records);
        Console.WriteLine("Added " + results.Records.Count + " SupplierProduct...");
        var nextRecordsUrl = results.NextRecordsUrl;
        if (!string.IsNullOrEmpty(nextRecordsUrl))
        {
            Console.WriteLine("Found more records...");

            while (true)
            {
                var continuationResults = await client.QueryContinuationAsync<SupplierProduct>(nextRecordsUrl);
                Console.WriteLine("Queried an additional " + continuationResults.Records.Count + " SupplierProduct.");
                accts.AddRange(continuationResults.Records);
                if (string.IsNullOrEmpty(continuationResults.NextRecordsUrl)) break;

                nextRecordsUrl = continuationResults.NextRecordsUrl;
            }
        }
        Upsert(accts, SupplierProduct.target);
    }

我有另外一种方法..

server {
    listen 7070;
    server_name localhost;
    location /jenkins/ {
        auth_basic "Please authenticate to use Jenkins";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_pass http://jenkins:8080/jenkins/;
        proxy_read_timeout  90;
        proxy_redirect off;

        proxy_set_header        Host $host:$server_port;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;
        proxy_set_header Authorization $http_authorization;
        #proxy_set_header Authorization "Basic ....";
        #proxy_pass_header  Authorization;
        proxy_set_header    X-Host       $host;



        #proxy_redirect      http://jenkins:8080 $scheme://localhost:7070;

    }

如何制作一般抽象这种行为的方法?

3 个答案:

答案 0 :(得分:3)

类型LeadSupplierProduct必须以某种方式相关,因为它们要么实现相同的接口,要么继承相同的基类以使其工作。显然,相似之处是_select,它显然是一个静态成员,不能包含在界面中。此外,人类可读字符串的生成必须重构为类型。

如果Base是假设的基类,则泛型函数的签名必须如下所示。

static async Task Get<T>(ForceClient client) where T : Base

答案 1 :(得分:2)

假设Lead和SupplierProduct都继承了相同的Parent:

 static async Task GetMyInstance<T>(ForceClient client) where T : Parent
    {
        Console.WriteLine("Get " + T.GetType().Name);
        var accts = new List<T>();
        var results = await client.QueryAsync<T>(T._select);
        var totalSize = results.TotalSize;
        Console.WriteLine("Queried " + totalSize + " " + T.GetType().Name +".");
        accts.AddRange(results.Records);
        Console.WriteLine("Added " + results.Records.Count + T.GetType().Name + "...");
        var nextRecordsUrl = results.NextRecordsUrl;
        if (!string.IsNullOrEmpty(nextRecordsUrl))
        {
            Console.WriteLine("Found more records...");

            while (true)
            {
                var continuationResults = await client.QueryContinuationAsync<T>(nextRecordsUrl);
                Console.WriteLine("Queried an additional " + continuationResults.Records.Count + " " + T.GetType().Name + ".");
                accts.AddRange(continuationResults.Records);
                if (string.IsNullOrEmpty(continuationResults.NextRecordsUrl)) break;

                nextRecordsUrl = continuationResults.NextRecordsUrl;
            }
        }
        Upsert(accts, T.target);
    }

请注意,Parent应包含_select和target以使其正常工作

你这样称呼它:

var foo = GetMyInstance<Lead>(forceClient);

var foo = GetMyInstance<SupplierProduct>(forceClient);

答案 2 :(得分:1)

这可能完全不够,但是当我想通过单个函数访问多个部分类似的方法时,我倾向于传递目标字符串并对它们使用重复的switch语句。 这样,我们也可以通过提供一系列目标来相互运行多个动作。

请注意;这段代码没有经过调试或其他任何东西,我只是想指出一下switch语句对你有用的方式。我无法提供更全面的答案,因为我无法完全理解您的代码的意图。

static async Task GetRecordsFor(ForceClient client, string[] targets )
    {
        foreach (string target in targets){
            switch ( target )
            {
                case 'leads':
                    Console.WriteLine("Get Leads");
                    var accts = new List<Lead>();
                    // more specific code for fetching leads
                    break;

                case 'suppliers':
                    Console.WriteLine("Get SupplierProduct");
                    var accts = new List<SupplierProduct>();
                    // more specific code for fetching suppliers
                    break;
            }
            // Actions you want to perform on each of these.
            accts.AddRange(continuationResults.Records);
        }
    }