本文介绍了ASP.NET MVC/WebApi的Azure表存储最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从ASP.NET MVC或Web API应用连接到Azure表存储的最佳实践是什么?

What are the best practices for connecting to a Azure Table Storage from a ASP.NET MVC or Web API app?

现在,我制作了一个StorageContext类,其中包含对 CloudStorageAccount CloudTableClient 的引用,如下所示:

Right now I've made a StorageContext class which holds a reference to the CloudStorageAccount and CloudTableClient, like this:

public class StorageContext
{
    private static CloudStorageAccount _storageAccount;
    private static CloudTableClient _tableClient;

    public StorageContext() : this("StorageConnectionString") { }

    public StorageContext(string connectionString)
    {
        if (_storageAccount == null)
            _storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings[connectionString].ConnectionString);

        if (_tableClient == null)
            _tableClient = _storageAccount.CreateCloudTableClient();
    }

    public CloudTable Table(string tableName)
    {
        var table = _tableClient.GetTableReference(tableName);

        table.CreateIfNotExists();

        return table;
    }
}

我的控制器正在像这样使用它:

And my controller I'm using it like this:

public class HomeController : ApiController
{
    private StorageContext db;

    public HomeController() : this(new StorageContext()) { }

    public HomeController(StorageContext context)
    {
        this.db = context;
    }

    public IHttpActionResult Get()
    {
        var table = db.Table("users");
        var results = (from user in table.CreateQuery<User>()
                       select user).Take(10).ToList();

        return Ok<List<User>>(results);
    }
}

这是首选方法吗?

该API将在流量超过1000 req/sec的高流量站点上使用.

The API is going to be used on a high traffic site with > 1000 req/sec.

我还需要单元测试.像上面一样使用它,我可以传入另一个connString名称,而在单元测试中连接到Azure存储模拟器.

I also need unit tests. Using it like above it I can pass in another connString name and instead connect to the Azure Storage emulator in my unit tests.

我走上正轨还是有更好的联系方式?

Am I on the right track or are there better ways to connect?

推荐答案

实际上是您的问题

可以像在Web应用程序中使用数据访问层的最佳实践是什么"这样重申.一样.

could be restated like "What are the best practices to use data access layer in web application". It is the same.

您可以找到许多有关数据访问层最佳实践的答案.但是在这里,铁律使您的数据访问层与控制器或表示分离.在MVC模式的范围内通过模型使用它的最佳方法,或者,如果您愿意的话,也可以考虑使用存储库和/或工作单元模式.

You can find a lot of answers about data access layer best practices. But iron rule here keep your data access layer separated from your controller or presentation. The best way to use it through Model in scope of MVC pattern, or you can think about Repository and/or Unit of work pattern if you like them.

在您的示例中,您的数据访问逻辑已经包装在StorageContext中,这很好,我将另外提取接口并为其使用DI/IoC和依赖项解析器.谈到您的代码段时就这些了.您在正确的道路上.

In your example your data access logic is already wrapped in StorageContext, which is fine, I would additionally extract interface and use DI/IoC and dependency resolver for it. That's all when speaking about your code snippet. You are on right way.

这篇关于ASP.NET MVC/WebApi的Azure表存储最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 11:57