.Net核心中的洋葱架构

时间:2016-06-24 14:00:48

标签: c# .net .net-core .net-core-rc2

我想用.net核心(RC2)框架创建一个洋葱类型的架构。

我创建了一个控制台应用程序(.NET Core)和5个类库(.NET Core)

  • 示例(控制台)
  • Example.Logic
  • Example.Logic.Interfaces
  • Example.Repository
  • Example.Repository.Interfaces
  • Example.Model

Example.Logic引用了Example.Logic.Interfaces,Example.Repository.Interfaces和Example.Model

Example.Repository引用了Example.Repository.Interfaces

对于依赖注入,我依赖于Autofac(Autofac.Extensions.DependencyInjection)。在Example.Logic和Example.Repository中,我有自动命令模块,用于注册接口和具体类。

public class Configuration : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<EmployerLogic>().As<IEmployerLogic>();
    }
}

我的控制台看起来像这样

public class Program
{
    public static void Main()
    {
        var builder = new ContainerBuilder();

        builder.RegisterModule<Logic.Configuration>();

        var container = builder.Build();
        GetEmployers(container);
    }

    private static void GetEmployers(IContainer container)
    {
        var employerLogic = container.Resolve<IEmployerLogic>();

        employerLogic.GetEmployers();

        var repository = container.Resolve<Repository.Interfaces.IRepository<string>>();
    }
}

因为我引用了Example.Logic,所以我现在也可以访问Example.Repository.IRepository。请参阅 var repository = container.Resolve ...

所以我的问题是:

  • 为什么Example可以访问IRepository接口?它不会被控制台应用程序引用。
  • 如何防止在控制台层中访问IRepository?

0 个答案:

没有答案