2012-06-20 240 views
3

在做下面的簡單示例時,我發現以下困難 正如標題所述,我打算在將數據存儲在Azure表存儲中時使用存儲庫模式。現在我有幾個類,Repository.cs,IRepository.cs,DataContext.cs和Controller。存儲庫模式和Azure表存儲(???)

在閱讀過程中,我發現了一些信息,並做了如下操作。 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:控制器 {

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; 

錯誤是對空引用,調試器顯示了不同的結果爲空?

最後我需要知道一些事情。

我應該在Repository.cs構造函數中做些什麼?我相信Datacontext.cs類必須在一個單獨的類...

這裏的任何提示

回答

5

海蘭,

首先我想你留下了一些代碼,因爲我不瞭解如何在存儲庫中獲取上下文。但是,假如你正確設置它,(注射?)考慮到您desinged您的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(); 
    } 

} 

希望這有助於你。

+0

是的,它幫助了很多。 – JOY