Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
针对ASP.NET MVC/WebApi的Azure表存储最佳实践_Asp.net_Asp.net Mvc_Asp.net Web Api_Azure Table Storage - Fatal编程技术网

针对ASP.NET MVC/WebApi的Azure表存储最佳实践

针对ASP.NET MVC/WebApi的Azure表存储最佳实践,asp.net,asp.net-mvc,asp.net-web-api,azure-table-storage,Asp.net,Asp.net Mvc,Asp.net Web Api,Azure Table Storage,从ASP.NET MVC或Web API应用程序连接到Azure表存储的最佳做法是什么 现在,我创建了一个StorageContext类,其中包含对CloudStorageAccount和CloudTableClient的引用,如下所示: public class StorageContext { private static CloudStorageAccount _storageAccount; private static CloudTableClient _tableCl

从ASP.NET MVC或Web API应用程序连接到Azure表存储的最佳做法是什么

现在,我创建了一个StorageContext类,其中包含对CloudStorageAccountCloudTableClient的引用,如下所示:

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;
    }
}
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);
    }
}
我的控制器是这样使用的:

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;
    }
}
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);
    }
}
公共类HomeController:ApicController
{
私有存储上下文数据库;
公共HomeController():此(新的StorageContext()){}
公共HomeController(StorageContext上下文)
{
this.db=上下文;
}
public IHttpActionResult Get()
{
var表=数据库表(“用户”);
var results=(来自表.CreateQuery()中的用户)
选择user.Take(10.ToList();
返回Ok(结果);
}
}
这是最好的方法吗

API将用于流量大于1000请求/秒的高流量站点

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


我是走对了路还是有更好的联系方式?

实际上是你的问题

连接到Azure表存储的最佳做法是什么 来自ASP.NET MVC或Web API应用程序

可以重述为“在web应用程序中使用数据访问层的最佳实践是什么”。是一样的

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


在您的示例中,您的数据访问逻辑已经包装在StorageContext中,这很好,我将另外提取接口并为其使用DI/IoC和依赖项解析器。这就是谈论代码片段时的全部内容。你走的路是对的。

我之所以没有走存储库/UoW之路,是因为该应用程序相当简单,所以我认为它只会增加应用程序的复杂性,我不会有任何好处。我可能错了。你不应该使用存储库和UoW。这只是一个选择。事实上,从您的代码片段中,您已经将数据访问逻辑包装在StorageContext中,并且它是分开的。所以我写了我想做的额外工作,但是你的代码片段在没有存储库的情况下看起来很好,也许你是对的,它会增加更多的复杂性,所以这取决于你。