它是一个依赖注入 anti-pattern 一个有很多构造函数的基础 class 吗?
Is it a dependency Injection anti-pattern a base class with many constructors?
我想做一个基础 class 来集中所有可以在需要时在 child class 中使用的属性。
我的问题是我正在做的是否是依赖注入 anti-pattern。
如果是这样,您能否举例说明如何最好地维护依赖注入模式和 SOLID 原则?
基地Class
public class BaseClass
{
protected readonly IProductRepository _productoRepository;
protected readonly ICategoryRepository _categoryRepository;
protected readonly IImageRepository _imageRepository;
public BaseClass(IProductRepository productoRepository)
{
_productoRepository = productoRepository;
}
public BaseClass(ICategoryRepository categoryRepository)
{
_categoryRepository = categoryRepository;
}
public BaseClass(IImageRepository imageRepository)
{
_imageRepository = imageRepository;
}
}
产品childclass
public class ProductClass : BaseClass
{
public ProductClass(IProductRepository productoRepository) : base(productoRepository)
{
}
public ProductClass(ICategoryRepository categoryRepository) : base(categoryRepository)
{
}
public ProductClass(IImageRepository imageRepository) : base(imageRepository)
{
}
}
类别childclass
public class CategoryClass : BaseClass
{
//Only this constructor will be required in this class
public CategoryClass(ICategoryRepository categoryRepository) : base(categoryRepository)
{
}
}
感谢您对此事的关注!
在您分享的代码中,似乎没有理由开始使用继承。
DI 和 IOC 与继承模式并不矛盾,但在您的基础中显然违反了 SRP class。
由于 class 只能实例化一次,因此您不会在 BaseClass 上有一个 DI 调用多个构造函数来注入额外的存储库 post-creation。
我怀疑您在 BaseClass
中真正想要的是 all 需要同时初始化的存储库:
public class BaseClass
{
protected readonly IProductRepository _productoRepository;
protected readonly ICategoryRepository _categoryRepository;
protected readonly IImageRepository _imageRepository;
public BaseClass(IProductRepository productoRepository, ICategoryRepository categoryRepository, IImageRepository imageRepository)
{
_productoRepository = productoRepository;
_categoryRepository = categoryRepository;
_imageRepository = imageRepository;
}
这是一种非常标准和明智的方式来设置依赖项注入。
如果您确实希望每个 class 有一个不同的 repo 实例,那么您最好引入一个更通用的 IRepository
并将其改为 DI:
public class BaseClass
{
protected readonly IRepository _repository;
public BaseClass(IRepository repository)
{
_repository= repository;
}
如果您想对派生的 classes 中需要的实现有更多的控制,您可以选择泛型:
public interface IRepository { }
public interface IProductRepository : IRepository { } // you can inherit interfaces
public interface ICategoryRepository : IRepository { }
public interface IImageRepository : IRepository { }
public class BaseClass<TRepo> where TRepo : IRepository // we will require all generic parameters to be descendant of base interface
{
protected readonly TRepo _repo;
public BaseClass(TRepo repo)
{
_repo = repo;
}
}
class Category : BaseClass<ICategoryRepository> {
public Category(ICategoryRepository repo) : base(repo) {} // DI will inject a specific implementation
}
希望概述的示例适用于您
我想做一个基础 class 来集中所有可以在需要时在 child class 中使用的属性。
我的问题是我正在做的是否是依赖注入 anti-pattern。
如果是这样,您能否举例说明如何最好地维护依赖注入模式和 SOLID 原则?
基地Class
public class BaseClass
{
protected readonly IProductRepository _productoRepository;
protected readonly ICategoryRepository _categoryRepository;
protected readonly IImageRepository _imageRepository;
public BaseClass(IProductRepository productoRepository)
{
_productoRepository = productoRepository;
}
public BaseClass(ICategoryRepository categoryRepository)
{
_categoryRepository = categoryRepository;
}
public BaseClass(IImageRepository imageRepository)
{
_imageRepository = imageRepository;
}
}
产品childclass
public class ProductClass : BaseClass
{
public ProductClass(IProductRepository productoRepository) : base(productoRepository)
{
}
public ProductClass(ICategoryRepository categoryRepository) : base(categoryRepository)
{
}
public ProductClass(IImageRepository imageRepository) : base(imageRepository)
{
}
}
类别childclass
public class CategoryClass : BaseClass
{
//Only this constructor will be required in this class
public CategoryClass(ICategoryRepository categoryRepository) : base(categoryRepository)
{
}
}
感谢您对此事的关注!
在您分享的代码中,似乎没有理由开始使用继承。
DI 和 IOC 与继承模式并不矛盾,但在您的基础中显然违反了 SRP class。
由于 class 只能实例化一次,因此您不会在 BaseClass 上有一个 DI 调用多个构造函数来注入额外的存储库 post-creation。
我怀疑您在 BaseClass
中真正想要的是 all 需要同时初始化的存储库:
public class BaseClass
{
protected readonly IProductRepository _productoRepository;
protected readonly ICategoryRepository _categoryRepository;
protected readonly IImageRepository _imageRepository;
public BaseClass(IProductRepository productoRepository, ICategoryRepository categoryRepository, IImageRepository imageRepository)
{
_productoRepository = productoRepository;
_categoryRepository = categoryRepository;
_imageRepository = imageRepository;
}
这是一种非常标准和明智的方式来设置依赖项注入。
如果您确实希望每个 class 有一个不同的 repo 实例,那么您最好引入一个更通用的 IRepository
并将其改为 DI:
public class BaseClass
{
protected readonly IRepository _repository;
public BaseClass(IRepository repository)
{
_repository= repository;
}
如果您想对派生的 classes 中需要的实现有更多的控制,您可以选择泛型:
public interface IRepository { }
public interface IProductRepository : IRepository { } // you can inherit interfaces
public interface ICategoryRepository : IRepository { }
public interface IImageRepository : IRepository { }
public class BaseClass<TRepo> where TRepo : IRepository // we will require all generic parameters to be descendant of base interface
{
protected readonly TRepo _repo;
public BaseClass(TRepo repo)
{
_repo = repo;
}
}
class Category : BaseClass<ICategoryRepository> {
public Category(ICategoryRepository repo) : base(repo) {} // DI will inject a specific implementation
}
希望概述的示例适用于您