29

Is there a better way to manage a bunch of generic functions? These all have a implementation that look almost the same. Changing them however is a monks job atm.

The interface that's implemented looks like this:

 IProxy<T> AddInterceptor<T1, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, TResult>, T1, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, TResult>, T1, T2, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, TResult>, T1, T2, T3, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, TResult>, T1, T2, T3, T4, T5, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, TResult>, T1, T2, T3, T4, T5, T6, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, TResult>, T1, T2, T3, T4, T5, T6, T7, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> func);
 IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult> func);

Pretty but you can emagine how the implementation looks. And what if I want to change something to the signature of these methods.

14
  • 1
    What about generating them compile-time using a T4 template? Mar 10, 2014 at 13:53
  • 2
    :) I don't think that's pretty at all Mar 10, 2014 at 13:53
  • 4
    @user1477388 This is Op-art in c#
    – albertjan
    Mar 10, 2014 at 13:59
  • 5
    @Sinatr I though regions only existed to make StyleCop complain.
    – Jodrell
    Mar 10, 2014 at 14:04
  • 3
    This is ASCII art depicting lines of slope -1, -2 and -3.
    – usr
    Mar 10, 2014 at 15:32

3 Answers 3

16

Here is possible implementation of T4 template which generates your interface:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#  const int maxParametersCount = 15; #>
using System;
using System.Linq.Expressions;

public interface IInterceptable<T>
{
<# for(int parametersCount = 1; parametersCount <= maxParametersCount; parametersCount++) { 
   string parameters = String.Join(", ", Enumerable.Range(1, parametersCount).Select(i => "T" + i));         
#>    IProxy<T> AddInterceptor<<#= parameters #>, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<<#= parameters #>, TResult>, <#= parameters #>, TResult> func);
<#}#>
}

You need only provide maxParametersCount value. If you will provide 15 it will generate following interface declaration:

using System;
using System.Linq.Expressions;

public interface IInterceptable<T>
{
    IProxy<T> AddInterceptor<T1, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, TResult>, T1, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, TResult>, T1, T2, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, TResult>, T1, T2, T3, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, TResult>, T1, T2, T3, T4, T5, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, TResult>, T1, T2, T3, T4, T5, T6, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, TResult>, T1, T2, T3, T4, T5, T6, T7, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> func);
    IProxy<T> AddInterceptor<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult>(Expression<Action<T>> functionOrProperty, Func<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult> func);
}

Of course, you can add some namespace to generated code.

1
11

You could use T4 templates to automatically generate code with N number of types. If you need to change slightly implementation, you simply edit your template and regenerate. And if you previously generated for 15, and now need 30, simply change your boundary and regenerate.

The greater question here is design - it's a bit of a suspect as to why this is needed in the first place. It may be worth to re-examine that part.

8
  • The implementation is mostly to create a nicer api. So you have intellismell when your writing the delegate. Eventually they're all passed into this function: IProxy<T> AddInterceptor(Expression<Action<T>> functionOrProperty, Func<Func<object[], object>, object[], object> func);
    – albertjan
    Mar 10, 2014 at 13:58
  • Could you expand on why this would be bad design?
    – albertjan
    Mar 10, 2014 at 14:02
  • @albertjan Well, maintenance pain - as you discovered - would be my primary argument against it (even with T4 use). Code readability is likely to suffer. Without knowing how it is used it's hard to be more specific, but in general terms, I suppose it is in a way similar to having a function with 15 overloads of identical arguments differing in count, rather than say instead take an array of same 15 arguments and have just one function signature. Note, I'm not saying that there cannot be a valid case for it, just that it may call for a sanity check. It may well be best approach in your case...
    – LB2
    Mar 10, 2014 at 14:28
  • Like I said all these functions are to make the api of this project easier to use and are all routed to a single function containing the logic. This is a public API so I do want to explicitly declare the options. Sanity checked :) T4 add as much code to render this code as it is to implement I don't know if I really like adding that.
    – albertjan
    Mar 10, 2014 at 14:38
  • 1
    @comeAndGo see github.com/Curit/DynamicProxy and see if you can create a typesafe API for adding Interceptors to the proxy without making these 15 overloades. Microsoft also makes 16 Funcs and 16 Actions and don't call everything a Delegate and let you handle the parameters blindly. I asked this question because I too wondered if this could be done differently. But apparently the Framework doesn't provide a short cut for these kinds of situations.
    – albertjan
    Mar 11, 2014 at 11:13
9

This seems like an ideal use for T4 Templates... If you haven't used them they allow you to write code that generates code. Anything this tedious that seems like a robot could do it - perfect application.

If you change something in the signature you simply change it in the t4 template and your .cs file will be updated automatically with the new signature.

3
  • Though it may be the solution I don't know what's worse. T4 is seriously hideous :P
    – albertjan
    Mar 10, 2014 at 14:29
  • @albertjan Haahaa, yeah I understand the reluctance. One thing I've found is that most T4 resources show the asp like syntax <% my completely illegible code block%> which when you are nesting complex code inside other complex code is completely terrible... The other syntax using WriteLine, PushIndent, etc leads to some almost readable code... You are dealing with strings so it isn't gorgeous but it is much less suicide inducing to debug. It also makes it easier to control your output indentation so you don't make yourself insane if you are a stickler for formatting. Mar 10, 2014 at 14:35
  • 1
    visualstudiogallery.msdn.microsoft.com/… this helps when writing the T4 templates in VS :) Mar 10, 2014 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.