我正在尝试设计一个 3 层的应用程序:
1) 数据访问层
2) 业务层
3) UI
我尝试保持类解耦,因此在业务层上我为数据访问类创建了接口,如下所示:
public interface ICountryRepository:IRepository
{
Country GetCountry(int ID);
int CreateCountry(Country obj);
Boolean UpdateCountry(Country obj);
Boolean DeleteCountry(Country obj);
...
...
}
我将接口作为参数传递给服务构造函数:
public CountryService(ICountryRepository repository,ILanguageRepository lang_repository)
{
....
}
但是例如在 CountryService 上,我需要加载当前用户及其权限,以便检查是否可以应用该操作:
public Country GetCountry(int ID)
{
if securityService.UserHasPermission(currentUser, GetPermission("CanGetCountry"))
{
return repository.GetCountry(ID);
}
else
{
Throw(New SecurityException("No permissions for that operation ...."))
}
}
这意味着我必须实例化 SecurityDataAccess 对象并将其传递给我的业务层程序集上的 SecurityService 的构造函数,我试图避免这样做以保持对象解耦。现在我什至没有在我的业务程序集中引用任何 DataAccess 程序集。
我正在考虑在这里使用 IoC 容器。使用外部配置,我可以从配置文件中获得正确的类/程序集。但我不确定这是否是正确的解决方案,因为据说 IoC 容器应该在一个地方使用以保持简单,并且大多数时候它应该是顶级程序集(UI 程序集)。
有人有解决这个问题的建议吗?