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.
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.
WARNING!!! I just typed this into the browser (no IDE or compiler) and my C# is very rusty, so this will likely need to be tweaked
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.
Something like this:
public interface BarFactory : IGenericFactory<Bar, BarEnum> { }
// Asking for a BarFactory and not a IGenericFactory<Bar, BarEnum>
public void DoWork(BarFactory barFactory) { ... }
Much less typing and no need for magic.
using ALIAS = TYPE;
statement, but I'd recommend against it since it isn't such a common thing to do so it will make your code harder to follow. – Dunant