有没有什么方法可以使用更多的泛型来简化这段代码

本文关键字:段代码 代码 泛型 可以使 方法 什么 有没有 | 更新日期: 2023-09-27 18:01:12

我试图简化它,但想不出让它更简单的方法。这些行还有很多,我访问的每个表都有一行。有什么建议吗?

public static IAzureTable<Sequence> GetSequenceTable(string datastoreValue)
{
    var sequenceTable = new AzureTable<Sequence>(GetStorageAccount(datastoreValue), "Sequences");
    return (sequenceTable);
}
public static IAzureTable<Topic> GetTopicTable(string datastoreValue)
{
    var topicTable = new AzureTable<Topic>(GetStorageAccount(datastoreValue), "Topics");
    return (topicTable);
}
public static IAzureTable<Test> GetTestTable(string datastoreValue)
{
    var testTable = new AzureTable<Test>(GetStorageAccount(datastoreValue), "Tests");
    return (testTable);
}

以下是更多可供参考的内容。不是真的想改变这一点,但我可以添加:

 public class AzureTable<T> : AzureTableBase<T>, IInitializer where T : TableServiceEntity
    {
        public AzureTable()
            : this(CloudConfiguration.GetStorageAccount())
        {
        }
        public AzureTable(CloudStorageAccount account)
            : this(account, null)
        {
        }
        public AzureTable(CloudStorageAccount account, string tableName)
            : base(account, tableName)
        {
        }

有没有什么方法可以使用更多的泛型来简化这段代码

这将删除GetTable方法家族的重复:

public static IAzureTable<T> GetTable<T>(string datastoreValue, string tableName) where T : TableServiceEntity    
{    
     return new AzureTable<T>(GetStorageAccount(datastoreValue), tableName);    
}

你可以这样称呼它:

var table = GetTable<Sequence>("DatastoreName", "Sequences");

您可以使用可选参数(C#4.0(来组合最后2:

public AzureTable(CloudStorageAccount account, string tableName = null)
            : base(account, tableName)
        {
        }

基本上与Alex Peck的答案相同,但有更多的机制,所以您只在一个地方指定表名和类型(不太容易出错(。

public class TableSpec<T>
{
  public readonly string name;
  public TableSpec(string name) { this.name = name; }
}
public static readonly TableSpec<Sequence> SequenceTableSpec = new TableSpec<Sequence>("Sequences");
public static readonly TableSpec<Topic> TopicTableSpec = new TableSpec<Topic>("Topics");
public static readonly TableSpec<Test> TestTableSpec = new TableSpec<Test>("Tests");
public static IAzureTable<T> GetTable<T>(TableSpec<T> spec, string datastoreValue)
{
  var table = new AzureTable<T>(GetStorageAccount(datastoreValue), spec.name);
  return table;
}

示例用法:

var topicTable = GetTable(TopicTableSpec, datastoreValue)
private static Dictionary<Type, string> _tableNames = new Dictionary<Type, string>
{
    {typeof(Sequence), "Sequences"},
    {typeof(Account), "Accounts"},
    {typeof(Test), "Tests"}
}
public static IAzureTable<T> GetTable(string datastorevalue) where T: Microsoft.WindowsAzureStorageClient.TableServiceEntity
{
    return new AzureTable<T>(GetStorageAccount(datastoreValue), _tableNames[typeof(T)]);
}

或者,您可以只执行

    public static IAzureTable<T> GetTable<T>(string datastoreValue, string tableName = null) where T : TableServiceEntity
    {
        var pluralizationService = PluralizationService.CreateService(new CultureInfo("en-US"));
        tableName = tableName ?? pluralizationService.Pluralize(typeof(T).Name);
        return new AzureTable<T>(GetStorageAccount(datastoreValue), tableName);
    }

这使用。NET 4.0的多元化服务。