0

私は3つのレイヤーを持つプロジェクトを持っています。

1位はDAL

2つ目はドメイン

3つ目はプレゼンテーション

ドメイン層 (ICategoryRepository) にインターフェイスを作成しました。コードは次のとおりです。

public interface ICategoryRepository
{
    List<CategoryDTO> GetCategory();
}

DAL にクラスを作成して、ドメインに ICategoryRepository を実装しました。

public class CategoryRepository : ICategoryRepository
{                     
    BookInfoContext _context;

    public List<CategoryDTO> GetCategory()
    {
        _context = new BookInfoContext();

        var categoryDto = _context.Categories
                            .Select(c => new CategoryDTO
                            {
                                CategoryId = c.CategroyId,
                                CategoryName = c.CategoryName
                            }).ToList();
        return categoryDto;
    }
}

次に、ドメインにクラスを作成し、ICategoryRepository をコンストラクターのパラメーターとして渡します。

public class CategoryService
{

    ICategoryRepository _categoryService;

    public CategoryService(ICategoryRepository categoryService)
    {
        this._categoryService = categoryService;
    }

    public List<CategoryDTO> GetCategory()
    {
        return _categoryService.GetCategory();
    }
}

これを行うと、コントロールが反転します。ドメインが DAL に依存する代わりに、コントロールを反転して、myDAL がドメインに依存するようにします。

私の問題は、プレゼンテーション層で CategoryService を呼び出すたびに、DAL にあるコンストラクターのパラメーターとして ICategoryRepository を渡す必要があることです。プレゼンテーション レイヤーを DAL に依存させたくありません。

なにか提案を?

ありがとう、

4

1 に答える 1