在做下面的简单示例时,我发现以下困难 正如标题所述,我打算在将数据存储在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类必须在一个单独的类...
这里的任何提示
是的,它帮助了很多。 – JOY