我试图简化这个,但想不到让它更简单的方法。这些行中有很多行,我访问的每个表都有一行。有什么建议吗?
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)
{
}
答案 0 :(得分:1)
这将删除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");
答案 1 :(得分:0)
你可以使用optional parameters(C#4.0)来组合最后的2:
public AzureTable(CloudStorageAccount account, string tableName = null)
: base(account, tableName)
{
}
答案 2 :(得分:0)
基本上与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)
答案 3 :(得分:0)
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)]);
}
答案 4 :(得分:-1)
或者,你可以做到
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多元化服务。