存储库模式和Azure表存储(???)

问题描述 投票:4回答:1

[在执行以下简单示例时,我发现了以下困难就像标题所说的那样,我打算在将数据存储到Azure表存储中时使用存储库模式。现在我有几个类,Repository.cs,IRepository.cs,DataContext.cs和Controller。

[在阅读期间,我找到了一些信息,并正在做如下操作。IRepository.cs

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); }
   }


}

加上控制器的某些部分(我之前创建的表中已经有数据)

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);
    }

以及接口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类必须在单独的类中...

这里有任何提示

asp.net-mvc-3 azure-storage azure-table-storage
1个回答
5
投票
嗨,

首先,我假定您遗漏了一些代码,因为我看不到如何在存储库中获取上下文。但是假设您设置正确((注入?)),并考虑到您设计数据上下文的方式,则存储库不需要知道表名,因为它是在以下代码行中设置的:

public IQueryable<T> DeviceTable { get { return CreateQuery<T>(Constants.DeviceTableName); } }

因此,当您基于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(); } }

希望对您有帮助。
© www.soinside.com 2019 - 2024. All rights reserved.