有没有办法为这个类/接口使用'友好'的名字? [英] Is there a way to use a 'friendy' name for this class/interface?
问题描述
基本上我只是觉得类型名称在我的项目中传递时很难看。我知道你不能使用实现接口的接口,因为它是一个刚刚实现原始接口的新接口,对吧?
Basically I just think the type name is ugly when passing it around my project. I know you can't use an interface that implements the interface because then it's a new interface that just happens to implement the original interface, right?
public class GenericFactory<T, TTypeEnum> : IGenericFactory<T, TTypeEnum>
{
private readonly IIndex<TTypeEnum, Func<CCompParms, T>> _factory;
public GenericFactory(IIndex<TTypeEnum, Func<CCompParms, T>> factory)
{
_factory = factory;
}
public T Create(TTypeEnum serviceType, CCompParms comp)
{
return _factory[serviceType](comp);
}
}
public interface IGenericFactory<T, TTypeEnum>
{
T Create(TTypeEnum serviceType, CCompParms comp);
}
我试过:
public interface FriendlyName : IGenericFactory<T, TTypeEnum>
{
}
但是当我尝试执行以下操作时,它无法无论我如何施展它都会投射。
But when I try to do the following it fails to cast no matter how I cast it.
IGenericFactory<T, TTypeEnum> inter = GetTheInterface();
FriendlyName fn = (inter as FriendlyName);
有没有办法让类型名称友好?
Is there a way to make the type name friendly?
推荐答案
首先,对于友好名称的任何通用解决方案仍然必须使用两种泛型类型进行参数化,所以我不认为这就是你的意思寻找,因为它不会真正为你节省任何打字。
First, any general solution for a "Friendly Name" will still have to be parameterized with both of the generic types, so I don't think that's what you're looking for since it won't really save you any typing.
假设你想要 FriendlyName
已经拥有类型绑定,然后
我认为你可以通过使用隐式转换和装饰器模式。
Assuming you want FriendlyName
to already have the types bound, then
I think you can get to a workable solution by using Implicit Conversions and the Decorator pattern.
警告!!!我只是在浏览器中输入了这个(没有IDE或编译器)而且我的C#非常生疏,所以这可能需要调整
public interface FooFactory : IGenericFactory<Foo, FooEnum> {
IGenericFactory<Foo, FooEnum> Wrapped { get; }
// The "magic" - Note that magic always makes your code harder to understand...
public static implicit operator FooFactory(IGenericFactory<Foo, FooEnum> wrapped) {
// I think this can be placed here. If C# won't let you add this
// implicit operator here, then you can easily implement this factory
// method as an extension on IGenericFactory<Foo, FooEnum>
return new FooFactoryWrapper(wrapped);
}
public static implicit operator IGenericFactory<Foo, FooEnum>(FooFactory wrapper) {
return wrapper.Wrapped;
}
// I'm pretty sure we can hide this implementation here in the interface,
// but again, my C# is pretty rusty, so you may have to move this
// and/or change the visibility
private class FooFactoryWrapper : FooFactory {
public IGenericFactory<Foo, FooEnum> Wrapped { get; private set; }
public FooFactoryWrapper(IGenericFactory<Foo, FooEnum> wrapped) {
this.wrapped = wrapped;
}
// Since the "friendly type" is still an instance of the base type,
// you'll still have to fully implement that interface. Just delegate
// all calls to your wrapped type (most useless Decorator ever)
public Foo Make() { return Wrapped.Make(); } // sample method in IGenericFactory<>
}
}
现在,你应该能够像这样使用它:
Now, you should be able to use it like this:
IGenericFactory<Foo, FooEnum> inter = GetTheInterface();
FooFactory fn = inter; // implicit conversion to wrapper type
DoWork(fn); // use the "friendly name" like it were it's wrapped type
// implicit conversion back to wrapped type
public void DoWork(IGenericFactory<Foo, FooEnum> fooFactory) {
...
}
全部话虽如此,我不会经历这种努力。每当我创建这样的友好名称类型时,我就将它们作为我的模型的一部分并将它们视为正确的类型,这意味着我直接在方法签名和构造函数中请求它们。
All that being said, I wouldn't go through this effort. Whenever I've made "Friendly Name" types like this, I then make them part of my "model" and treat them as proper types, which means that I directly ask for them in method signatures and constructors.
这样的事情:
public interface BarFactory : IGenericFactory<Bar, BarEnum> { }
// Asking for a BarFactory and not a IGenericFactory<Bar, BarEnum>
public void DoWork(BarFactory barFactory) { ... }
打字少得多需要魔法。
这篇关于有没有办法为这个类/接口使用'友好'的名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!