JOY*_*JOY 4 azure-storage azure-table-storage asp.net-mvc-3
在做以下简单示例时,我发现了以下困难正如标题所说,我打算在将数据存储在Azure表存储中时使用存储库模式.现在我有几个类,Repository.cs,IRepository.cs ,DataContext.cs和Controller.
在我阅读期间,我发现了一些信息,并且一直在做如下.IRepository.cs
public interface IRepository<T> where T: TableServiceEntity
{
T GetById(int Id);
IQueryable<T> GetAll();
}
Run Code Online (Sandbox Code Playgroud)
和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); }
}
}
Run Code Online (Sandbox Code Playgroud)
加上控制器的某些部分(我之前创建的表中已有数据)
public class 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);
}
Run Code Online (Sandbox Code Playgroud)
和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;
Run Code Online (Sandbox Code Playgroud)
错误是关于null引用,调试器显示变量结果为null?
最后,我需要了解一些事情.
我应该在Repository.cs构造函数中做什么?我相信Datacontext.cs类必须在一个单独的类中......
任何暗示在这里
HY,
首先,我假设您遗漏了一些代码,因为我没有看到您如何在您的存储库中获取您的上下文.但是假设你确实设置了它(注入?),考虑到你设置datacontext的方式,存储库不需要知道表名,因为它是在以下代码行中设置的:
public IQueryable<T> DeviceTable
{
get { return CreateQuery<T>(Constants.DeviceTableName); }
}
Run Code Online (Sandbox Code Playgroud)
因此,当您基于IQueryable DeviceTable创建查询时,表名已经设置.
问题是我没有看到你的上下文类的需要,特别是因为它只能带来一个实体类型(它是通用的,基于一个实体).我的Azure表存储库的基本布局是:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
希望这对你有所帮助.
| 归档时间: |
|
| 查看次数: |
2458 次 |
| 最近记录: |