汎用リポジトリと汎用サービスを使用する多くのアプリケーションがあります。これらのアプリケーションはさまざまな開発者によって開発され、各プロジェクトには異なるメソッドシグネチャがあります。たとえば、GetAllまたはReturnAll、CreateまたはAddなど。 コードの標準を保証し、ジェネリックの作成を避けるためにすべてのアプリケーション用の汎用リポジトリおよびサービスレイヤを構築しようとしています。新しいアプリケーションのためのリポジトリおよびサービス層。多くのプロジェクトで使用する特定のレイヤーの汎用リポジトリ/サービス
私は、単純なインジェクタエラーを取得するプロジェクトを実行します。
The supplied type
BaseService<TEntity, TContext>
contains unresolvable type arguments. The type would never be resolved and is therefore not suited to be used. Parameter name: implementationType
BaseServiceクラス:
public class BaseService<TEntity, TContext> : IBaseService<TEntity, DbContext> where TEntity : class where TContext : DbContext
{
protected IBaseRepository<TEntity, TContext> _baseRepository;
public BaseService(IBaseRepository<TEntity, TContext> baseRepository)
{
_baseRepository = baseRepository;
}
public virtual void Create(TEntity entity)
{
try
{
_baseRepository.Create(entity);
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
public virtual void Create(IList<TEntity> entities)
{
try
{
_baseRepository.Create(entities);
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
}
ベースリポジトリクラス:リポジトリ内
public class BaseRepository<Entity, TContext> : IDisposable, IBaseRepository<Entity, TContext> where Entity : class where TContext : DbContext
{
private readonly TContext _context;
public BaseRepository(TContext context)
{
_context = context;
}
public void Add(Entity obj)
{
try
{
_context.Set<Entity>().Add(obj);
_context.SaveChanges();
}
catch (Exception ex)
{
throw new Exception("Error to add", ex);
}
}
public void Add(IList<Entity> list)
{
try
{
_context.Set<Entity>().AddRange(list);
_context.SaveChanges();
}
catch (Exception ex)
{
throw new Exception("Error to add", ex);
}
}
}
実装:
をpublic class UserRepository : BaseRepository<User, DbContext>, IUserRepository
{
private readonly MyContext _context;
public UserRepository(MyContext context)
: base(context)
{
_context = context;
}
}
は、ブートストラップクラスに登録:
container.Register(typeof(IBaseRepository<,>), (typeof(BaseRepository<,>)), Lifestyle.Scoped);
container.Register<IUserRepository, UserRepository>(Lifestyle.Scoped);
container.Register(typeof(IBaseService<,>), (typeof(BaseService<,>)), Lifestyle.Scoped);
container.Register<IUserService, UserService>(Lifestyle.Scoped);
container.Register<IUnitOfWork, UnitOfWork>(Lifestyle.Scoped);
container.Register<MyContext>(Lifestyle.Scoped);
container.Register(typeof(IBaseService<,>), (typeof(BaseService<,>)), Lifestyle.Scoped);
私は同じエラーで動作するプロジェクトの同じ構造を持つサンプルプロジェクトをビルドします。 Link to project in GitHub
そして、それは答えを得るためにとても時間がかかった理由を不思議に思っている場合は、あなたの質問があまりにも多くの詳細を持っているので、それはです。次の質問を[最小限で完全で検証可能な例](https://stackoverflow.com/help/mcve)に減らすと、応答時間が大幅に短縮されます。この場合、6行のコードに質問を減らすことができました。これは、あなた自身が何が間違っていたのかを知ることにも役立ちます。 – Steven
こんにちはスティーブン。ご回答有難うございます。私はベースサービスのコードを変更しますが、私はまだ同じエラーを受け取ります。私は、ブートストラップ登録クラスでは、私は間違いをしていると思うし、ベースリポジトリはEFのコンテキストタイプを推論することはできません。私は "最小限で読みやすい"質問を構築しようとしたので、gitで同じ問題を再現できるリポジトリを作成しました。 –
こんにちはTiagoさん、Stackoverflowの他の多くの人たちと同じように、私はかなり忙しい人なので、オンラインにしたプロジェクト全体を閲覧する時間がありません。そのプロジェクトは、確かに私が言及している「最小限で検証可能な例」ではありません。私が言ったように、私は元の問題を8行のコードで再現することができました。私の提案は、あなたの新しい問題で小さなプログラムを作り直そうとすることです。 Stackoverflowは、Stackoverflow自体にそのコードを投稿することを期待しています。私は10行から20行のコードを見てうれしいですが、確かに1400 +行のサンプルプロジェクトではありません。 – Steven