什么是Func键<的首选命名约定; TResult>方法参数?
我承认,这个问题是主观的,但我感兴趣的是社会的观点。我有一个缓存类,一个类型的缓存加载功能 Func键< TResult方式>
,它用来从数据库中检索值,并将其存储在缓存中。
I admit that this question is subjective but I am interested in the view of the community. I have a cache class that takes a cache loader function of type Func<TResult>
, which it uses to retrieve a value from the database and store it in cache.
public static class Cache
{
public TResult Get<TResult>(string cacheKey, Func<TResult> cacheLoader)
{
// Implementation
}
}
我的问题是:我应该如何命名的函数参数
- 我应该将其命名为喜欢的对象,例如
的CacheLoader
? - 我应当命名的方法,如
loadResult
? - 我应该明确地提到它作为一个功能,例如
cacheLoadFunction
? (我不喜欢这样。)
- Should I name it like an object, e.g.
cacheLoader
? - Should I name it like a method, e.g.
loadResult
? - Should I explicitly refer to it as a function, e.g.
cacheLoadFunction
? (I don't like this.)
我是不太感兴趣,我应该命名的这个的特定的功能参数,更感兴趣的是如何命名的一般功能参数。你怎么说你们,堆栈溢出社区?
I'm less interested in what I should name this particular function parameter and more interested in how you name function parameters in general. What say ye, Stack Overflow community?
有在框架中使用的名词的先例,如:
There are precedents for using a noun in the Framework, e.g.
Enumerable.Average<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
Enumerable.Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
Enumerable.GroupBy<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
ConcurrentDictionary<TKey,TValue>.GetOrAdd(TKey key,
Func<TKey, TValue> valueFactory);
名词往往是一个合适的动词用的的施事后缀。
在你的榜样,我会使用类似装载机
或可能 valueFactory
。我个人不喜欢的CacheLoader
,因为据推测这是主叫方,而不是做在缓存中插入的工作委托。
In your example I would use something like loader
or possibly valueFactory
. I personally don't like cacheLoader
because presumably it's the caller rather than the delegate that does the work of inserting in the cache.