在做以下简单示例时,我发现了以下困难正如标题所说,我打算在将数据存储在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 …Run Code Online (Sandbox Code Playgroud)