c# - Google .NET API - 除了 FileDataStore 之外还有其他数据存储吗?

标签 c# asp.net asp.net-mvc google-api google-api-dotnet-client

我正在使用 Google .NET API 从 Google Analytics 获取分析数据。

这是我开始验证的代码:

IAuthorizationCodeFlow flow =
    new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets
            {
                ClientId = googleApiClientId,
                ClientSecret = googleApiClientSecret
            },
            Scopes = new[] { 
                Google.Apis.Analytics.v3.AnalyticsService.Scope.AnalyticsReadonly
            },
            DataStore = new Google.Apis.Util.Store.FileDataStore("Test_GoogleApi")
        });

它使用 FileDataStore,将其作为文件存储在本地用户配置文件中。我在 ASP.NET 应用程序中运行这段代码,所以我不能真正使用那个 FileDataStore,所以我需要的是其他获取数据的方法。

Google.Apis.Util.Store 仅包含 FileDataStore 和 IDataStore 接口(interface)。在我开始实现自己的 DataStore 之前 - 是否有任何其他 DataStore 对象可供下载?

谢谢

最佳答案

Google 的 FileDataStore 的源可用 here .

我编写了 IDataStore 的简单 Entity Framework (版本 6)实现,如下所示。

如果您希望将它和 EF 一起放在一个单独的项目中,您将需要 Google.Apis.Core已安装 nuget 包。

public class Item
{
    [Key]
    [MaxLength(100)]
    public string Key { get; set; }

    [MaxLength(500)]
    public string Value { get; set; }
}

public class GoogleAuthContext : DbContext
{
    public DbSet<Item> Items { get; set; }
}

public class EFDataStore : IDataStore
{
    public async Task ClearAsync()
    {
        using (var context = new GoogleAuthContext())
        {
            var objectContext = ((IObjectContextAdapter)context).ObjectContext;
            await objectContext.ExecuteStoreCommandAsync("TRUNCATE TABLE [Items]");
        }
    }

    public async Task DeleteAsync<T>(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentException("Key MUST have a value");
        }

        using (var context = new GoogleAuthContext())
        {
            var generatedKey = GenerateStoredKey(key, typeof(T));
            var item = context.Items.FirstOrDefault(x => x.Key == generatedKey);
            if (item != null)
            {
                context.Items.Remove(item);
                await context.SaveChangesAsync();
            }
        }
    }

    public Task<T> GetAsync<T>(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentException("Key MUST have a value");
        }

        using (var context = new GoogleAuthContext())
        {
            var generatedKey = GenerateStoredKey(key, typeof(T));
            var item = context.Items.FirstOrDefault(x => x.Key == generatedKey);
            T value = item == null ? default(T) : JsonConvert.DeserializeObject<T>(item.Value);
            return Task.FromResult<T>(value);
        }
    }

    public async Task StoreAsync<T>(string key, T value)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentException("Key MUST have a value");
        }

        using (var context = new GoogleAuthContext())
        {
            var generatedKey = GenerateStoredKey(key, typeof (T));
            string json = JsonConvert.SerializeObject(value);

            var item = await context.Items.SingleOrDefaultAsync(x => x.Key == generatedKey);

            if (item == null)
            {
                context.Items.Add(new Item { Key = generatedKey, Value = json});
            }
            else
            {
                item.Value = json;
            }

            await context.SaveChangesAsync();
        }
    }

    private static string GenerateStoredKey(string key, Type t)
    {
        return string.Format("{0}-{1}", t.FullName, key);
    }
}

关于c# - Google .NET API - 除了 FileDataStore 之外还有其他数据存储吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20502542/

相关文章:

c# - 将 JSON 数组绑定(bind)到 ASP.NET MVC 3 中的列表的问题模型

mysql - 使用 LINQ/lambda .net MVC 从表中选择某些列的所有数据?

c# - 在自定义条件 ValidationAttribute 中检索条件值

c# - 如何将 DisplayName 设置为 ErrorMessage 格式

c# - 在运行时更改类属性中的值

c# - 使用此设置防止隧道?

asp.net - DataReader,按列检索数据

asp.net - 什么是 ASP.NET 中的跨页面发布?

c# - HTTP header 或 SOAP header 中的 WCF Soap Actions?

c# - 如何在没有数据库的情况下为开发环境实现 SignInManager?