次の簡単な例を実行しているときに、次の問題が見つかりました。タイトルにあるように、Azureテーブルストレージにデータを格納するときにリポジトリパターンを使用するつもりです。現在、Repository.cs、IRepository.csの2つのクラスがあります。 、DataContext.csおよびコントローラー。
私の読書中に私はいくつかの情報を見つけ、次のようにやっています。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クラスは別のクラスになければならないと思います...
ここにヒント