ジェネリックインターフェースと汎用リポジトリを作成しました。私は、これらのジェネリックを既存のアーキテクチャーで活用して、依存性注入の実装を改善し単純化しようとしています。Ninjectでジェネリックスを使用してインターフェイスをクラスにバインドするにはどうすればよいですか?
バインディングはジェネリックス実装なしで動作します。 どこが間違っているのか分からないようです。
以下は、これらのジェネリックをNinjectで実装する方法です。私が受け取る
ありエラーは、次のとおりです。
///エラーメッセージ: 型のオブジェクトをキャストすることができません 'DataRepository' 'IDataRepository' と入力します。ここで
は、私が../App_Startの下で静的クラスNinjectWebCommonで
//repo
public class DataRepository : GenericRepository<IDataRepository>
{
public IQueryable<MainSearchResult> SearchMain(){ //.... stuff here}
}
//interface
public interface IDataRepository : IGenericRepository<MainSearchResult>
{
IQueryable<MainSearchResult> SearchMain(){ //.... stuff here}
}
をジェネリックを使用してレポしてインタフェースを作成しました。ここ
//generic interface
public interface IGenericRepository<T> where T : class
{
IQueryable<T> GetAll();
IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Delete(T entity);
void Edit(T entity);
void Save();
}
//generic repo
public abstract class GenericRepository<T> : IGenericRepository<T> where T : class
{
////removed the implementation to shorten post...
ジェネリックレポとのインタフェースであります私は、RegisterServices(IKernelカーネル)メソッドのクラスをバインドしています。 いくつかのバインド方法を試しましたが、「オブジェクトタイプをキャストできません...」というエラーが引き続き表示されます。
private static void RegisterServices(IKernel kernel)
{
// current failed attempts
kernel.Bind(typeof(IGenericRepository<>)).To(typeof(GenericRepository<>));
kernel.Bind(typeof(IDataRepository)).To(typeof(DataRepository));
// failed attempts
//kernel.Bind<IDataRepository>().To<GenericRepository<DataRepository>>();
//kernel.Bind<IDataRepository>().To<GenericRepository<DataRepository>>();
}
誰もがこの問題を引き起こす私が間違ってやっている何も見えていますか?
は 'DataRepository'がIDataRepository''を継承していますか?おそらく – MaKCbIMKo
のように見えますが、 'class DataRepository:GenericRepository、IDataRepository' –
が正しいでしょう。あなたは答えとして提出する必要があります – ironman