带参数的结构图解析类型

发布于 2024-09-17 05:19:59 字数 840 浏览 2 评论 0原文

我有一个问题...... 以免说我有一个这样的类:

public class A: InterfaceA
{
    private FileInfo _fileInfo = null;
    public A(FileInfo fileInfo)
    {
        this._fileInfo = fileInfo;
    }

    ...
}

和另一个:

public class B: InterfaceB
{
    private A _classA = null;
    public B(A classA)
    {
        this._classA = classA;
    }

    public void Do()
    {
        FileInfo fi = new FileInfo(...);
        _classA.DoSomething();
    }
}

现在,我已经设置了如下所示的 StructureMap 寄存器:

For<InterfaceA>().Use<A>();
For<InterfaceB>().Use<B>();

当我执行 B.Do() 时,结构映射将引发错误,因为没有 FileInfo 参数的注册表项。 A类的参数(FileInfo)是在B类中构造的; 我知道我可以这样做:ObjectFactor.GetInstance() 并传递参数,但我想要依赖注入而不是服务提供者。我希望当我执行 ObjectFactory.GetInstance() 时,构造整个对象图。

如何做到这一点?

I have a problem....
Lest say I have a class like this one:

public class A: InterfaceA
{
    private FileInfo _fileInfo = null;
    public A(FileInfo fileInfo)
    {
        this._fileInfo = fileInfo;
    }

    ...
}

and another one:

public class B: InterfaceB
{
    private A _classA = null;
    public B(A classA)
    {
        this._classA = classA;
    }

    public void Do()
    {
        FileInfo fi = new FileInfo(...);
        _classA.DoSomething();
    }
}

Now, I have setup StructureMap registers like this:

For<InterfaceA>().Use<A>();
For<InterfaceB>().Use<B>();

and when I execute B.Do() structuremap will throw an error because there is no registry entry for FileInfo parameter.
The parameter of class A (FileInfo) is constructed in class B;
I know that I can do: ObjectFactor.GetInstance() and pass parameters, but I want Dependency injection not service provider. And I want that when I do ObjectFactory.GetInstance(), to construct entire object graph.

How to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2024-09-24 05:20:16

您可以使用 Ctor 指令告诉 SM 使用哪些对象作为 Ctor 参数。

var myFile = new FileInfo(...);
For<InterfaceA>.Use<A>().Ctor<FileInfo>().Is(myFile);

此处了解有关 Ctor 参数的更多信息。

编辑:
如果在执行 B 中的 Do 方法之前不知道文件名,则必须推迟 A 对象的创建,直到执行 do 方法。为此,您可以使用手动编码或 Func/Lazy 工厂。

对于 Func 方法,您可以更改 B 以将 A 的 Func 作为 ctor 依赖项:

public class B : InterfaceB
{
  private readonly Func<string, InterfaceA> _aBuilder;
  public B(Func<string, InterfaceA> aBuilder)
  {
    _aBuilder = aBuilder;
  }

  public void Do()
  {
    InterfaceA anA = _aBuilder("fileName");
    anA.DoSomething();
  }
}

使用以下命令引导它:

ObjectFactory.Initialize(
  c=>
  {
    c.For<InterfaceA>().Use<A>();
    c.For<Func<string, InterfaceA>>().Use(d => 
      new Func<string, InterfaceA>( s => 
        ObjectFactory.With(new FileInfo(s)).GetInstance<InterfaceA>()));
    c.For<InterfaceB>().Use<B>();
  }
);

You can use the Ctor instruction to tell SM what objects to use for the Ctor parameters.

var myFile = new FileInfo(...);
For<InterfaceA>.Use<A>().Ctor<FileInfo>().Is(myFile);

Read more about Ctor arguments here.

Edit:
In case the file name is not known until the execution of the Do method in B, you have to postpone the creation of the A-object until the do method is executed. In order to do so you can use a factory, either hand coded or a Func/Lazy.

For a Func approach you can change your B to take a Func of A as ctor dependency:

public class B : InterfaceB
{
  private readonly Func<string, InterfaceA> _aBuilder;
  public B(Func<string, InterfaceA> aBuilder)
  {
    _aBuilder = aBuilder;
  }

  public void Do()
  {
    InterfaceA anA = _aBuilder("fileName");
    anA.DoSomething();
  }
}

Boot strap it using:

ObjectFactory.Initialize(
  c=>
  {
    c.For<InterfaceA>().Use<A>();
    c.For<Func<string, InterfaceA>>().Use(d => 
      new Func<string, InterfaceA>( s => 
        ObjectFactory.With(new FileInfo(s)).GetInstance<InterfaceA>()));
    c.For<InterfaceB>().Use<B>();
  }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文