开发者

Is there any way I could simplify this code using more generics?

I've tried to simplify this but can't think of a way to make it simpler. There are a lot more of these rows with one for every table I access. Any suggestions?

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);
}

Here's more for reference. Not really wanting to change this but I could add to it:

 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)
        {
        }


This would remove repitition of the GetTable family of methods:

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

You would call it like this:

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


you could use optional parameters (C# 4.0) to combine the last 2:

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


Basically the same as Alex Peck's answer, but with a little more machinery so you only specify the table name and type in a single place (less error prone).

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;
}

Example usage:

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)]);
}


Or, you could just do

    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);
    }

This uses the .NET 4.0 pluralization service.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜