Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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
C# 集中化CloudStorageAccount和TableServiceContext实例_C#_Asp.net Mvc_Azure_Azure Storage_Azure Table Storage - Fatal编程技术网

C# 集中化CloudStorageAccount和TableServiceContext实例

C# 集中化CloudStorageAccount和TableServiceContext实例,c#,asp.net-mvc,azure,azure-storage,azure-table-storage,C#,Asp.net Mvc,Azure,Azure Storage,Azure Table Storage,在ASP.NET MVC 3 Web角色中,我意识到我一直在编写以下代码: var account = CloudStorageAccount.Parse( RoleEnvironment.GetConfigurationSettingValue("DataConnectionString") ); var ctx = account.CreateCloudTableClient().GetDataServiceContext(); 因此,我决定将

在ASP.NET MVC 3 Web角色中,我意识到我一直在编写以下代码:

var account = 
    CloudStorageAccount.Parse(
        RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")
    );

var ctx = 
    account.CreateCloudTableClient().GetDataServiceContext();
因此,我决定将其集中于整个ASP.NET MVC应用程序,并创建了以下具有静态属性的类:

internal class WindowsAzureStorageContext {

    public static CloudStorageAccount StorageAccount { 

        get {
            return
                CloudStorageAccount.Parse(
                    RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")
                );
        }
    }

    public static TableServiceContext TableServiceCtx { 
        get {

            return
                StorageAccount.CreateCloudTableClient().GetDataServiceContext();
        } 
    }
}
在我的控制器中,我使用以下方法:

public class HomeController : Controller {

    private readonly TableServiceContext ctx = 
        WindowsAzureStorageContext.TableServiceCtx;

    public ViewResult Index() {

        var model = ctx.CreateQuery<TaskEntity>(Constants.TASKS_TABLE).
            Where(x => x.PartitionKey == string.Empty);

        return View(model);
    }

    public ViewResult Create() {
        return View();
    }

    [ActionName("Create")]
    [HttpPost, ValidateAntiForgeryToken]
    public ViewResult Create_post(TaskEntity taskEntity) {

        ctx.AddObject(Constants.TASKS_TABLE, new TaskEntity(taskEntity.Task));
        ctx.SaveChangesWithRetries();

        return RedirectToAction("Index");
    }
}
公共类HomeController:控制器{
专用只读表ServiceContext ctx=
WindowsAzureStorageContext.TableServiceCtx;
公共视图结果索引(){
var model=ctx.CreateQuery(常数.任务\表)。
其中(x=>x.PartitionKey==string.Empty);
返回视图(模型);
}
公共视图结果创建(){
返回视图();
}
[ActionName(“创建”)]
[HttpPost,ValidateAntiForgeryToken]
公共视图结果创建发布(任务实体任务实体){
ctx.AddObject(Constants.TASKS_表,新TaskEntity(TaskEntity.Task));
ctx.SaveChangesWithRetries();
返回操作(“索引”);
}
}

我知道这不是一个友好的单元测试,我应该通过DI接口来实现<代码> TabelServEclipse文本/Cux>实例,但是当我这样做的时候,我考虑使用这个代码> WieldSaZurSturgEngEngest类来获得一个实例:<代码> TabelServEngEngest.<代码>类.< /P> 这是一种好的做法吗?因为我在整个应用程序生命周期中使用同一个类,这会对我造成任何伤害吗


有什么已知的模式可以做到这一点吗?

我认为这样做没有任何问题。看起来是个干净的好方法。我不知道有哪种已知的模式可以做到这一点,但我只是认为昨天应该有这种模式。

我认为您可以将存储库模式用于通用数据上下文,并在其上添加一个通用接口。不确定它是否有用,但您可以参考我的博客

我不相信上下文实例之间存在任何共享状态。话虽如此,控制器执行的事务时间是不平凡的。你抓住上下文的时间越长,就越有可能发生冲突。我发现将冲突和重叠保持在最低限度的一种方法是使加载/更改/保存周期尽可能短


埃里克

谢谢!我问这个问题的原因是我不能理解
static
关键字的全部功能。我在想如果我这样做的话会不会有问题。