Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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
C# 了解AutoFac捕获依赖项_C#_Autofac - Fatal编程技术网

C# 了解AutoFac捕获依赖项

C# 了解AutoFac捕获依赖项,c#,autofac,C#,Autofac,原谅我,我只是一个AutoFac的新手,我刚刚开始学习它是如何工作的。当我在官方网站上阅读时,我在理解Captive dependencies和sopelife时遇到了一些问题 让我困惑的是帖子中提到的例子。请仔细阅读下面的评论。谢谢 原来的解释看起来像是吹了。我刚才在这里引用了它 public class RuleManager { public RuleManager(IEnumerable<IRule> rules) { this.Rules = rules

原谅我,我只是一个
AutoFac
的新手,我刚刚开始学习它是如何工作的。当我在官方网站上阅读时,我在理解
Captive dependencies
和sope
life
时遇到了一些问题

让我困惑的是帖子中提到的例子。请仔细阅读下面的评论。谢谢

原来的解释看起来像是吹了。我刚才在这里引用了它

public class RuleManager
{
  public RuleManager(IEnumerable<IRule> rules)
  {
      this.Rules = rules;
  }

  public IEnumerable<IRule> Rules { get; private set; }
}

public interface IRule { }

public class SingletonRule : IRule { }

public class InstancePerDependencyRule : IRule { }


[Fact]
public void CaptiveDependency()
{
    var builder = new ContainerBuilder();

    // The rule manager is a single-instance component. It
    // will only ever be instantiated one time and the cached
    // instance will be used thereafter. It will be always be resolved
    // from the root lifetime scope (the container) because
    // it needs to be shared.
    builder.RegisterType<RuleManager>()
           .SingleInstance();

    // This rule is registered instance-per-dependency. A new
    // instance will be created every time it's requested.
    builder.RegisterType<InstancePerDependencyRule>()
           .As<IRule>();

    // This rule is registered as a singleton. Like the rule manager
    // it will only ever be resolved one time and will be resolved
    // from the root lifetime scope.
    builder.RegisterType<SingletonRule>()
           .As<IRule>()
           .SingleInstance();

    using (var container = builder.Build())
    using (var scope = container.BeginLifetimeScope("request"))
    {
      // The manager will be a singleton. It will contain
      // a reference to the singleton SingletonRule, which is
      // fine. However, it will also hold onto an InstancePerDependencyRule
      // which may not be OK. The InstancePerDependencyRule that it
      // holds will live for the lifetime of the container inside the
      // RuleManager and will last until the container is disposed.
      var manager = scope.Resolve<RuleManager>();
    }
}
公共类规则管理器
{
公共规则管理器(IEnumerable规则)
{
这个。规则=规则;
}
公共IEnumerable规则{get;private set;}
}
公共接口IRule{}
公共类单例规则:IRule{}
公共类InstancePerDependencyRule:IRule{}
[事实]
公共无效俘虏独立()
{
var builder=new ContainerBuilder();
//规则管理器是一个单实例组件
//将只实例化一次,并且缓存
//实例将在以后使用。它将始终被解析
//从根生存期范围(容器)中删除,因为
//它需要被分享。
builder.RegisterType()
.SingleInstance();
//此规则是每个依赖项注册的实例。新的
//实例将在每次请求时创建。
builder.RegisterType()
.As();
//此规则已注册为单例。与规则管理器类似
//它只会被解决一次,而且会被解决
//从根生存期范围。
builder.RegisterType()
.As()
.SingleInstance();
使用(var container=builder.Build())
使用(var scope=container.BeginLifetimeScope(“请求”))
{
//经理将是单身汉。它将包含
//对singleton SingletonRule的引用,它是
//很好。但是,它也将保留InstancePerDependencyRule
//这可能不正常。InstancePerDependencyRule
//货舱将在集装箱内的整个生命周期内有效
//RuleManager,并将持续到容器被释放为止。
var manager=scope.Resolve();
}
}
阅读后,我有一些问题和理解,如下所示。请帮忙复习一下

  • 当作用域(container.BeginLifetimeScope(“request”))的生存期结束时,当
    using
    语句完成时,哪个组件实例将是一次性的<代码>InstancePerDependencyRule或
    规则管理器
  • 我的答案是,
    InstancePerDependencyRule
    将是一次性的。是这样吗?原因在第二章中说明

  • 为什么
    InstancePerDependencyRule
    的生存期比
    规则管理器的生存期长?这个例子说它违反了约束依赖规则。请帮我更准确地解释一下。据我所知,默认情况下,它被注册为每个依赖项的
    实例。在作用域(container.BeginLifetimeScope(“请求”)结束后,它将是一次性的。(因为它由using语句包装)。我认为
    InstancePerDependencyRule
    的生存期应该比
    RuleManger
    短,后者是
    Singleton

    如果我的理解有任何错误,请帮我改正。谢谢。

    Joe.wang你得到答案了吗?