洋葱架构:尊重应用程序 MVC 层中的依赖关系
Onion architecture : respecting dependencies in the MVC Layer of the application
我正在使用 ASP.NET MVC 和洋葱架构制作网站。我有以下架构:
- 域:实体/域接口
- 存储库:通用存储库(目前)使用Entity Framework代码优先方法
- 服务:调用存储库的通用服务
- MVC
现在我正尝试在我的控制器中创建一个方法来开始测试我在 Repository
和 Service
中实现的方法,我很难确定我可以做什么在此控制器中创建。我想在 Repository
中测试一个简单的 Get
方法,但为此我需要在我的控制器中使用 GenericService
对象和 GenericRepository
对象。为了证明我的意思,这是我的 GenericRepository 的一个片段(我将跳过接口):
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private readonly PrincipalServerContext context;
private DbSet<T> entities;
public Repository(PrincipalServerContext context)
{
this.context = context;
entities = context.Set<T>();
}
}
现在我的通用服务:
public class GenericService<T> : IGenericService<T> where T : class
{
private IRepository<T> repository;
public GenericService(IRepository<T> repository)
{
this.repository = repository;
}
public T GetEntity(long id)
{
return repository.Get(id);
}
}
最后,我的问题是,我是否可以按如下方式在我的控制器中创建这些对象(使用名为 PrincipalServerContext 的 dbcontext):
public class NavigationController : Controller
{
private IGenericService<DomainModelClassHere> domainService;
private IGenericRepository<DomainModelClassHere> domainRepo;
private PrincipalServerContext context;
public ActionResult MyMethod(){
context = new PrincipalServerContext();
domainRepo = new GenericRepository<DomainModelClassHere>(context);
domainService = new GenericService<DomainModelClassHere>(domainRepo);
if(domainService.GetEntity(1)==null)
return View("UserNotFound");//Just as an example
return View();
}
}
这是允许的吗?根据 Jeffrey Palermo 的说法,UI 可以依赖于 Service
和 Domain
,所以我不知道 Repository
。从技术上讲,我没有使用 repository
中的方法,但我确实需要添加对项目的引用。
如果我不能,那么如果我没有 GenericRepository
,我该如何创建一个新的 GenericService
?有没有更好的方法来实例化我的对象?
EDIT 我认为我的问题的答案在 Startup.cs
中,我可以在其中放置类似 service.addScoped(typeof(IGenericRepository<>),typeof(GenericRepository<>));
的内容
但我对此不确定,有什么想法吗?
如果有人遇到同样的问题,我会自己回答这个问题。我们可以使用一些配置方法在需要时创建 类 的实例。在 Startup.cs
文件中,您必须添加 ConfigureServices(IServiceCollection services)
方法,其中有几种方法可应用于 services
以创建这些实例。例如,您可以使用:
services.AddTransient(IGenericRepository, GenericRepository)
(这个link解释了方法之间的差异)。
AddTransient
对我来说很好,因为它在应用程序的整个生命周期内创建了一个对象实例,这正是我所需要的。这意味着 UI 依赖于解决方案的其余部分,因为 Startup.cs 需要了解 Repositories
以及 Services
。
可以在这里找到一个很好的答案:.
我正在使用 ASP.NET MVC 和洋葱架构制作网站。我有以下架构:
- 域:实体/域接口
- 存储库:通用存储库(目前)使用Entity Framework代码优先方法
- 服务:调用存储库的通用服务
- MVC
现在我正尝试在我的控制器中创建一个方法来开始测试我在 Repository
和 Service
中实现的方法,我很难确定我可以做什么在此控制器中创建。我想在 Repository
中测试一个简单的 Get
方法,但为此我需要在我的控制器中使用 GenericService
对象和 GenericRepository
对象。为了证明我的意思,这是我的 GenericRepository 的一个片段(我将跳过接口):
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private readonly PrincipalServerContext context;
private DbSet<T> entities;
public Repository(PrincipalServerContext context)
{
this.context = context;
entities = context.Set<T>();
}
}
现在我的通用服务:
public class GenericService<T> : IGenericService<T> where T : class
{
private IRepository<T> repository;
public GenericService(IRepository<T> repository)
{
this.repository = repository;
}
public T GetEntity(long id)
{
return repository.Get(id);
}
}
最后,我的问题是,我是否可以按如下方式在我的控制器中创建这些对象(使用名为 PrincipalServerContext 的 dbcontext):
public class NavigationController : Controller
{
private IGenericService<DomainModelClassHere> domainService;
private IGenericRepository<DomainModelClassHere> domainRepo;
private PrincipalServerContext context;
public ActionResult MyMethod(){
context = new PrincipalServerContext();
domainRepo = new GenericRepository<DomainModelClassHere>(context);
domainService = new GenericService<DomainModelClassHere>(domainRepo);
if(domainService.GetEntity(1)==null)
return View("UserNotFound");//Just as an example
return View();
}
}
这是允许的吗?根据 Jeffrey Palermo 的说法,UI 可以依赖于 Service
和 Domain
,所以我不知道 Repository
。从技术上讲,我没有使用 repository
中的方法,但我确实需要添加对项目的引用。
如果我不能,那么如果我没有 GenericRepository
,我该如何创建一个新的 GenericService
?有没有更好的方法来实例化我的对象?
EDIT 我认为我的问题的答案在 Startup.cs
中,我可以在其中放置类似 service.addScoped(typeof(IGenericRepository<>),typeof(GenericRepository<>));
的内容
但我对此不确定,有什么想法吗?
如果有人遇到同样的问题,我会自己回答这个问题。我们可以使用一些配置方法在需要时创建 类 的实例。在 Startup.cs
文件中,您必须添加 ConfigureServices(IServiceCollection services)
方法,其中有几种方法可应用于 services
以创建这些实例。例如,您可以使用:
services.AddTransient(IGenericRepository, GenericRepository)
AddTransient
对我来说很好,因为它在应用程序的整个生命周期内创建了一个对象实例,这正是我所需要的。这意味着 UI 依赖于解决方案的其余部分,因为 Startup.cs 需要了解 Repositories
以及 Services
。
可以在这里找到一个很好的答案: