asp.net-mvc-3 – 存储库模式和Azure表存储(???)
在做以下简单示例时,我发现了以下困难
正如标题所说,我打算在将数据存储在Azure表存储中时使用存储库模式.现在我有几个类,Repository.cs,IRepository.cs,DataContext.cs和Controller. 在我阅读期间,我发现了一些信息,并且一直在做如下. public interface IRepository<T> where T: TableServiceEntity { T GetById(int Id); IQueryable<T> GetAll(); } 和DataContext.cs public class DataContext<T>:TableServiceContext where T:TableServiceEntity { public DataContext(CloudStorageAccount storageaccount,StorageCredentials credentials) : base(storageaccount.TableEndpoint.AbsoluteUri,credentials) { // _storageAccount = storageaccount; var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(KEY_STORAGE)); storageAccount.CreateCloudTableClient().CreateTableIfNotExist(tableName); } public IQueryable<T> DeviceTable { get { return CreateQuery<T>(tableName); } } } 加上控制器的某些部分(我之前创建的表中已有数据) 公共类DeviceMeController:Controller private IRepository<Entity>_repository; public Controller() : this(new Repository<Entity>()) { } public Controller(IRepository<Entity> repository) { _repository = repository; } public ActionResult Index() { var List = _repository.GetAll(); return View(deviceList); } 和Reposistory.cs接口的实现,这里是我有错误并在某处丢失的地方 public class Repository<T>:IRepository<T> where T:TableServiceEntity { private DataContext<T> _serviceContext; // here get tablename as pararameter; // so the Enities call this function public Repository() { // the same context for multiple tables ? } // perhaps it should take the table Name public void Add(T item) { _serviceContext.AddObject(TableName,item); _serviceContext.SaveChangesWithRetries(); } public IQueryable<T> GetAll() { var results = from c in _serviceContext.Table select c; return results; 错误是关于null引用,调试器显示变量结果为null? 最后,我需要了解一些事情. 我应该在Repository.cs构造函数中做什么?我相信Datacontext.cs类必须在一个单独的类中…… 任何暗示在这里 解决方法
HY,
首先,我假设您遗漏了一些代码,因为我没有看到您如何在您的存储库中获取您的上下文.但是假设你确实设置了它(注入?),考虑到你设置datacontext的方式,存储库不需要知道表名,因为它是在以下代码行中设置的: public IQueryable<T> DeviceTable { get { return CreateQuery<T>(Constants.DeviceTableName); } } 因此,当您基于IQueryable DeviceTable创建查询时,表名已经设置. 问题是我没有看到你的上下文类的需要,特别是因为它只能带来一个实体类型(它是通用的,基于一个实体). public abstract class CloudRepository<TEntity> : ICloudRepository<TEntity> { private TableServiceContext _tableServiceContext; private string _tableName; public string TableName { get { return _tableName ?? ( _tableName = typeof(TEntity).Name.Replace("Entity",string.Empty).ToLower()); } } public CloudStorageAccount StorageAccount { get { return CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString")); } } public CloudTableClient TableClient { get { CloudTableClient cloudTableClient = StorageAccount.CreateCloudTableClient(); cloudTableClient.CreateTableIfNotExist(TableName); return cloudTableClient; } } public TableServiceContext ServiceContext { get { return _tableServiceContext ?? (_tableServiceContext = TableClient.GetDataServiceContext()); } } public IEnumerable<TEntity> FindAll() { return ServiceContext.CreateQuery<TEntity>(TableName).ToList(); } } 希望这对你有所帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 从ASP.NET Core 3.0 preview 特性,了解CLR的Garbage Colle
- 让OData和NHibernate结合进行动态查询
- asp.net-mvc-2 – 如何覆盖ActionLink行为
- asp.net-mvc – MVC – 无法解析视图(单独项目中的控制器和
- 我如何保护我的ASP.Net源代码从我的开发人员
- asp.net – SimpleMembershipProvider不工作
- asp.net-mvc – 应用程序每15分钟编译一次
- kendo-ui – Kendo UI ASP.NET MVC网格数据源过滤器值为nul
- 腾讯云-容器-常用操作命令
- asp.net-mvc – WebSecurity.InitializeDatabaseConnection