Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to nested type of type Expression<Func<T>>

Tags:

I have a function used when calling a service. Before it call the service, it will create a log entry:

protected TResult CallService<TService, TResult>(TService service,
    Expression<Func<TService, TResult>> functionSelector)
{
    Logger.LogServiceCall(service, functionSelector);
    return functionSelector.Compile()(service);
}

The Visual Studio 2010 Code Analyzer informs me that I shouldn't use Nested Type in the following message:

CA1006 : Microsoft.Design : Consider a design where 'ServiceManager.CallService<TService, Result>(TService, Expression<Func<TService, TResult>>)' doesn't nest generic type 'Expression<Func<TService, TResult>>'.

While I could simply create a suppression rule for this entry, is there is an alternative that exist that would prevent displaying such warning?

like image 594
Pierre-Alain Vigeant Avatar asked Aug 09 '10 15:08

Pierre-Alain Vigeant


People also ask

How to Type A nested object in typescript?

Use an interface or a type alias to type a nested object in TypeScript. You can set properties on the interface that point to nested objects. The type of the object can have as deeply nested properties as necessary. We used an interface to type an object that has nested properties.

How to type an object that has nested properties?

We used an interface to type an object that has nested properties. The address property points to an object that has country and city properties of type string. You can also use a type alias to achieve the same result.

Is it possible to express a lambda expression with static typing?

From your edited question, it looks like you do have some statically typed knowledge of the type, otherwise you wouldn't be able to express that lambda expression.

How does typescript infer the type of an object?

If you declare a nested object and initialize all of its key-value pairs, you can let TypeScript infer its type. TypeScript is able to infer the type of the object, based on the key-value pairs we have provided upon initialization.


2 Answers

I would suppress it in this case, with the reason that the caller doesn't have to cope with nested generics, he is just passing a lambda expression, which is easy to use.

CA does not make exceptions for lambda expressions. Sometimes It is better to suppress it then to write weird code.

like image 200
Stefan Steinegger Avatar answered Jan 18 '23 11:01

Stefan Steinegger


I'll be honest, I suppress that rule most of the time. While I can understand that some of the construction of the nested types can be avoided, it is more often than not the case; you usually want to leave that to the call site because you can't guarantee that the call site will want the nested generic type to be instantiated in the same way.

This is one of those rules that I find a bit overbearing; I generally agree with most of them, but not this one.

like image 41
casperOne Avatar answered Jan 18 '23 10:01

casperOne