【问题标题】:What are the references in an onion architecture supposed to look like洋葱架构中的参考应该是什么样的
【发布时间】:2017-04-11 12:57:03
【问题描述】:

我在.net core here 中关注了关于洋葱架构的msdn 文档,但似乎你真的不能让UI 只知道服务层,而不复制代码。在文章的最后一部分(UI),启动文件正在访问 repo。这是合乎逻辑的吗?如果有,为什么?

【问题讨论】:

    标签: c# architecture .net-core onion-architecture


    【解决方案1】:

    在文章的最后一部分(UI),启动文件是 访问回购。这是合乎逻辑的吗?如果有,为什么?

    我相信您指的是以下代码-

    services.AddDbContext<ApplicationContext>(options => 
      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
    services.AddTransient<IUserService, UserService>();
    services.AddTransient<IUserProfileService, UserProfileService>();
    

    UI项目引用其他项目,在IoC容器中注册依赖是正常的,因为Composition Root 应放置在尽可能靠近应用程序入口点的位置。

    var userService = new UserService();
    

    如果您在 UI 中使用 new 关键字来实例化 UserService,它们就会变得 紧密耦合 - 一个类的变化会迫使另一个类发生变化。

    IoC 容器通过在运行时解析依赖关系并根据需要传递它们来解决依赖关系问题。

    如果您想了解更多关于 DI 的信息,您可能需要阅读 Dependency Injection in .NET by Mark SeemannAdaptive Code via C# by Gary McLean Hall

    【讨论】:

      猜你喜欢
      • 2013-08-12
      • 2019-01-13
      • 2011-03-24
      • 2011-10-09
      • 2014-10-15
      • 1970-01-01
      • 2014-06-22
      • 2015-03-17
      • 1970-01-01
      相关资源
      最近更新 更多