加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 存储库模式和Azure表存储(???)

发布时间:2020-12-16 06:25:00 所属栏目:asp.Net 来源:网络整理
导读:在做以下简单示例时,我发现了以下困难 正如标题所说,我打算在将数据存储在Azure表存储中时使用存储库模式.现在我有几个类,Repository.cs,IRepository.cs,DataContext.cs和Controller. 在我阅读期间,我发现了一些信息,并且一直在做如下. IRepository.cs publi
在做以下简单示例时,我发现了以下困难
正如标题所说,我打算在将数据存储在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); }
   }


}

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

公共类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创建查询时,表名已经设置.

问题是我没有看到你的上下文类的需要,特别是因为它只能带来一个实体类型(它是通用的,基于一个实体).
我的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();
    }

}

希望这对你有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读