流畅的接口,需要类似 C# 中的全局方法之类的东西
我目前正在尝试为 ServiceLocator 构建流畅的界面。为了确保每个开发人员都可以轻松设置 1 对 n 映射,
我希望像这样的
ServiceLocator.Instance.For<IFoo>(Use<Foo>(), Use<FooBar>());
Singleton 工作正常... For 方法的 methodsignature 看起来像这样
public void For<TInterface>(params type[] services)
{
// ...
}
所以我正在寻找类似 global 的东西方法
C# 有一些全局方法,所有方法都定义在 System.Object 上。但是,当我在 System.Object 上创建新的通用 ExtensionMethod 时,该方法将不可见。
public static class MyExtensions
{
public static void Use<T>(this Object instance)
{
// ..
}
}
MethodChaining 是另一种选择,但这种方法看起来很性感:D
有人有想法吗?
Im currently trying to build a Fluent Interface for a ServiceLocator. To ensure that each the developer can easily setup 1-to-n mappings
I'd like something like this
ServiceLocator.Instance.For<IFoo>(Use<Foo>(), Use<FooBar>());
Singleton is workin fine... the methodsignature for the For method looks like this
public void For<TInterface>(params type[] services)
{
// ...
}
So I was looking for something like a global method
C# has some global methods, all methods which are defined on System.Object. But when I create a new generic ExtensionMethod on System.Object, the method will not be visible.
public static class MyExtensions
{
public static void Use<T>(this Object instance)
{
// ..
}
}
MethodChaining would be the alternative, but this approach looks sexy :D
Has anyone an idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,实际上当我为
Object
创建扩展方法时,它是可见的。比如,我误解了你的问题吗?
Well, actually when I create an extension method for
Object
, it is visible. As in,Did I misunderstand your question?
您需要为包含扩展方法的命名空间添加
using
语句才能使其可见。向对象
添加扩展方法并不是一个好主意。编辑:
好吧,现在我明白你在问什么了。为了使用扩展方法,您需要一个实例。您要求对象上的静态扩展方法(Equals 和 ReferenceEquals 是 static方法),这是不可能的。如果您在对象上定义扩展方法,它将在所有实例上可用,我确信这不是您想要的。
You need to add a
using
statement for the namespace containing your extension method in order for it to be visible. Adding extension methods toobject
is rarely a good idea.EDIT:
Okay, now I understand what you're asking. In order to use an extension method you need an instance. You're asking for a static extension method on object (Equals and ReferenceEquals are static methods), and that's not possible. If you define an extension method on object, it will be available on all instances and I'm sure that's not what you want.
服务定位器被广泛认为是一种反模式。此外,通用注册接口被广泛认为是一个无法解决的问题,除非您需要使用特定的容器。
回顾这两个有问题的决定,您可以通过定义接受多个类型参数的
For
重载来消除对全局方法的需求:For 方法如下所示:
必须为每个类型计数定义一个重载,但它需要最少的语法和最大的可发现性。作为参考,.NET Framework 的
Action
和Func
类型支持 9 个类型参数。不过,写完之后,我想知道我是否误解了这个问题:为什么要为同一个接口指定多个实现?这不会导致解析
IFoo
时出现歧义吗?Service Locator is widely considered to be an anti-pattern. Also, a common registration interface is widely considered to be an unsolvable problem unless you are requiring use of a specific container.
Looking past these two questionable decisions, you can remove the need for the global method by defining overloads of
For
which accept multiple type arguments:The
For
methods would look like this:You have to define an overload for each type count, but it requires the minimal syntax and maximum amount of discoverability. For reference, the .NET Framework's
Action
andFunc
types support 9 type arguments.After writing this out, though, I wonder if I misunderstood the question: why would you specify multiple implementations for the same interface? Wouldn't that lead to ambiguity when resolving
IFoo
?