是否有注册静态封闭与温莎城堡一个简单的方法?静态、城堡、温莎、简单

2023-09-04 09:09:32 作者:浓妆艳抹

我一直在尝试使用单方法接口命名的委托来代替。这有一定的优势,为code尺寸,因为我们可以从(一些换行符去掉,以免夸大的情况下):

I've been experimenting with using named delegates instead of single-method interfaces. This has some advantages for code size, as we can go from (some linebreaks removed so as not to overstate the case):

public interface IProductSource
{
    IEnumerable<Product> GetProducts();
}
public class DataContextProductSource : IProductSource
{
    private readonly DataContext _DataContext;
    public DataContextProductSource(DataContext dataContext)
    {
        if (dataContext == null) throw new ArgumentNullException("dataContext");
        _DataContext = dataContext;
    }
    public IEnumerable<Product> GetProducts()
    {
        return _DataContext.Products.AsEnumerable();
    }
}

public delegate IEnumerable<Product> DGetProducts();
public static class DataContextFunctions
{
    public DGetProducts GetProducts(DataContext dataContext)
    {
        if (dataContext == null) throw new ArgumentNullException("dataContext");
        return () => dataContext.Products.AsEnumerable();
    }
}

这基本上是走的事实,一旦你远远不够用依赖注入,很多班级变得比少封更有优势。这些类可以换成返回lambda表达式函数。整组相关功能(即不需要任何封装可变状态,但是会一直EX $ P $利用班班通标准依赖注入pssed),然后可以卷起来放进一个静态类(或模块在VB中的说法)。

This is basically taking advantage of the fact that once you go far enough with dependency injection, a lot of classes become little more than closures. Those classes can be replaced with functions that return lambdas. Entire sets of related functions (that don't need to encapsulate any mutable state, but would have been expressed using classes in "standard" dependency injection), can then be rolled up into a static class (or "module" in VB parlance).

这是一切都很好,但我无法找到注册这些静态方法与温莎城堡的最佳方式。有没有依赖的方法很简单:

This is all well and good, but I'm having trouble finding the best way to register these static methods with Castle Windsor. Methods with no dependencies are easy:

Component.For<DGetIntegers>().Instance(Integers.GetOneToTen)

但是,我们从上面DataContextFunctions.GetProducts有一定的相关性。我发现登记本的最佳方式是:

But our DataContextFunctions.GetProducts from above has some dependencies. The best way I've found to register this is:

Component.For<DGetProducts>().UsingFactoryMethod(
    kernel => DataContextFunctions.GetProducts(kernel.Resolve<DataContext>())

这可以得到相当冗长,显然不必直接请求内核为每个依赖那种失败的点位。在我看来,这是所有的静态信息的容器应需要提供,所以它应该有可能做到这一点。

This can get quite verbose, and obviously having to ask the kernel directly for each dependency kind of defeats the point a bit. It seems to me that all the static information that a container should need is available, so it should be possible to do this.

现在的问题是,是否温莎城堡(或任何其他容器)有一个简单的方法来做到这一点,我已经错过了,还是有出现的技术问题,或者是它太利基一个用例已被列入?

The question is, does Castle Windsor (or any other container) have a simple way to do this that I've missed, or are there technical problems that arise, or is it just too niche a use case to have been included?

推荐答案

这是一个有趣的方法。我想你可以得到相当轻松这个工作有一个自定义的活化剂。

That's an interesting approach. I think you could get this working quite easily with a custom activator.