Sugar syntax for double common function

I have the following function in C #:

bool Handle<TCommandHandler, TModel>(TModel model) where TCommandHandler : ICommandHandler<TModel> { // ... _container.Resolve<TCommandHandler>(); // ... } 

Since TModel clear from a function parameter, I want some way not to indicate its type when calling the function. Ideally, I want to call it this:

 Handle<MyCommandHandler>(model); 

Since this is probably not possible, I came up with the following:

 HandleTemp<TModel> Handle<TModel>(TModel model) { return new HandleTemp<TModel>(model); } public class HandleTemp<TModel> { private TModel _model; public HandleTemp(TModel model) { _model = model;} public bool With<TCommandHandler>() where TCommandHandler : ICommandHandler<TModel> { } } 

So now I call it:

 Handle(model).With<MyCommandHandler>(); 

Are there any other options? Did I do something completely wrong with my decision?

+6
source share
2 answers

No, your analysis and solution look right. In fact, type type inference can only work on an all-or-nothing basis. If there are any general parameters that cannot be displayed, all of them must be explicitly specified. Personally, I would really like to say "you are worried about these parameters, I will tell you this," but ... this does not exist.

The only other option is to add an artificial extra regular parameter so that it can output the general parameter - a little yucky.

Another option: challenge the suggestion that generics are needed. For example, could it be an instance of Type ? Will be:

 bool Handle<TModel>(TModel model, Type type)... ... Handle(model, typeof(MyCommandHandler)); 

work for example? I cannot answer this directly since I don’t know the details of your _container.Resolve<TCommandHandler>(); as to whether it can be adjusted to take Type and not <T> .

+4
source

All the needs of the C # compiler is a demonstration of the type of arguments, so instead of trying to put it in general arguments (on the usage site), do something that allows you to provide an argument that helps the compiler identify this type, To make it less confusing, here is an example:

 // Your classes/interfaces. class Container { public static T Resolve<T>() { Console.WriteLine("Resolving {0}", typeof(T).FullName); return default(T); } } interface ICommandHandler<TModel> { void DoSomething(); } // An implemented ICommandHandler. public class WackyCommandHandler : ICommandHandler<string> { public void DoSomething() { } } // Used to help the C# compiler identify types. public static class Identify { public static TypeIdentity<TType> TheType<TType>() { return null; // You don't actually need an instance. } } public sealed class TypeIdentity<TType> { private TypeIdentity() { } } // Your method static bool Handle<TCommandHandler, TModel>(TModel model, TypeIdentity<TCommandHandler> handler) where TCommandHandler : ICommandHandler<TModel> { var item = Container.Resolve<TCommandHandler>(); return true; } // And the usage site: var a = "hello"; Handle(a, Identify.TheType<WackyCommandHandler>()); Console.ReadLine(); 
+1
source

Source: https://habr.com/ru/post/898567/


All Articles