Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 是否可以对windows库项目实施依赖项注入?_.net_Design Patterns_C# 4.0_Dependency Injection_Lib - Fatal编程技术网

.net 是否可以对windows库项目实施依赖项注入?

.net 是否可以对windows库项目实施依赖项注入?,.net,design-patterns,c#-4.0,dependency-injection,lib,.net,Design Patterns,C# 4.0,Dependency Injection,Lib,是否可以对windows库项目实施依赖项注入?或者这种设计模式在图书馆中不适用?当然可以 例如,Microsoft在其所有ASP.NET核心库中都这样做。它们都支持依赖注入 易用性的重要部分是确保库知道如何连接依赖项。例如,Microsoft库都为ServiceCollection提供扩展方法,因此我可以编写ServiceCollection.AddLibraryName()而不必自己添加所有必需的类。当然可以 例如,Microsoft在其所有ASP.NET核心库中都这样做。它们都支持依赖注入

是否可以对windows库项目实施依赖项注入?或者这种设计模式在图书馆中不适用?

当然可以

例如,Microsoft在其所有ASP.NET核心库中都这样做。它们都支持依赖注入

易用性的重要部分是确保库知道如何连接依赖项。例如,Microsoft库都为
ServiceCollection
提供扩展方法,因此我可以编写
ServiceCollection.AddLibraryName()而不必自己添加所有必需的类。

当然可以

例如,Microsoft在其所有ASP.NET核心库中都这样做。它们都支持依赖注入


易用性的重要部分是确保库知道如何连接依赖项。例如,Microsoft库都为
ServiceCollection
提供扩展方法,因此我可以编写
ServiceCollection.AddLibraryName()而不必自己添加所有必需的类。

可以。您可以使用与windows窗体相同的Asp.net核心,而无需使用其他库,如autofac(如果您想使用,也可以使用),也可以使用与.net相同的库,就像您可以注册服务、repo和Singleton一样

    var builder = new ContainerBuilder();

// Register individual components
builder.RegisterInstance(new TaskRepository())
       .As<ITaskRepository>();
builder.RegisterType<TaskController>();
builder.Register(c => new LogManager(DateTime.Now))
       .As<ILogger>();

// Scan an assembly for components
builder.RegisterAssemblyTypes(myAssembly)
       .Where(t => t.Name.EndsWith("Repository"))
       .AsImplementedInterfaces();

var container = builder.Build();

是的,你可以。您可以使用与windows窗体相同的Asp.net核心,而无需使用其他库,如autofac(如果您想使用,也可以使用),也可以使用与.net相同的库,就像您可以注册服务、repo和Singleton一样

    var builder = new ContainerBuilder();

// Register individual components
builder.RegisterInstance(new TaskRepository())
       .As<ITaskRepository>();
builder.RegisterType<TaskController>();
builder.Register(c => new LogManager(DateTime.Now))
       .As<ILogger>();

// Scan an assembly for components
builder.RegisterAssemblyTypes(myAssembly)
       .Where(t => t.Name.EndsWith("Repository"))
       .AsImplementedInterfaces();

var container = builder.Build();

你说的“windows库”是什么意思?你说的“windows库”是什么意思?在没有入口点的库中,我在哪里写上面的注册码?@bill更新我的代码。这样你就可以在startup类中注册你的回购协议和服务。在没有入口点的库中,我在哪里写上面的注册码?@bill更新我的代码。像这样,你可以在startup类中注册你的回购协议和服务。
   public partial class Form1 : Form {
    private readonly IUserRepository userRepository;
    private readonly IUserContext userContext;

    public Form1(IUserRepository userRepository, IUserContext userContext) {
        this.userRepository = userRepository;
        this.userContext = userContext;

        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
        if (this.userContext.IsAdministrator) {
            this.userRepository.ControlSomeStuff();
        }
    }
}