Create a fluent interface from method parameters?

Should this be possible with the structural replace feature or are these kind of replacements too complex?

Convert method with X params
-----------------------------------------------------------------    
public class Foo
{
public void Bar(int a, int b, int c, int d, string e)
{
if (e == null)
throw new ArgumentNullException("e");
//do something
}
}
//Bar-Call:
fooInstance.Bar(1,2,3,4,"foo");
-----------------------------------------------------------------
To a "fluent interface" instead of the params:
public class Foo
{
public interface IBarOptions
{
IBarOptions A(int value);
IBarOptions B(int value);
IBarOptions C(int value);
IBarOptions D(int value);
IBarOptions E(string value);
}
private class BarOptions : IBarOptions
{
public IBarOptions A(int value) { AValue = value;
return this; }

public int AValue { get; set; }
public int BValue { get; set; }
public int CValue { get; set; }
public int DValue { get; set; }
public string EValue { get; set;}

public IBarOptions B(int value) { BValue = value;
return this; }

public IBarOptions C(int value) { CValue = value;
return this; }

public IBarOptions D(int value) { DValue = value;
return this; }

public IBarOptions E(string value) {
if (value == null)
throw new ArgumentNullException("value");

EValue = value;
return this;}
}
public void Bar(Action<IBarOptions> optionsCb)
{
if (optionsCb == null)
throw new ArgumentNullException("optionsCb");
var options = new BarOptions();
optionsCb(options);
if (options.EValue == null)
throw new ArgumentException(@"E cannot be null.", @"optionsCb");

//do something
}
}

//Bar-Call:
fooInstance.Bar(opt => opt.E("something").A(1));
-----------------------------------------------------------------

Thanks.

0
1 comment
Avatar
Permanently deleted user

Hello,

I'm afraid such replacements can not be fulfilled with Structural Search
and Replace at the moment. Thank you!

Andrey Serebryansky
Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Should this be possible with the structural replace feature or is
these kind of replacements too complex?

Convert method with X params
-----------------------------------------------------------------
public class Foo
{
public void Bar(int a, int b, int c, int d, string e)
{
if (e == null)
throw new ArgumentNullException("e");
//do something
}
}
//Bar-Call:
fooInstance.Bar(1,2,3,4,"foo");
-----------------------------------------------------------------
To a "fluent interface" instead of the params:
public class Foo
{
public interface IBarOptions
{
IBarOptions A(int value);
IBarOptions B(int value);
IBarOptions C(int value);
IBarOptions D(int value);
IBarOptions E(string value);
}
private class BarOptions : IBarOptions
{
public IBarOptions A(int value) { AValue = value;
return this; }
public int AValue { get; set; }
public int BValue { get; set; }
public int CValue { get; set; }
public int DValue { get; set; }
public string EValue { get; set;}
public IBarOptions B(int value) { BValue = value;
return this; }
public IBarOptions C(int value) { CValue = value;
return this; }
public IBarOptions D(int value) { DValue = value;
return this; }
public IBarOptions E(string value) {
if (value == null)
throw new ArgumentNullException("value");
EValue = value;
return this;}
}
public void Bar(Action<IBarOptions> optionsCb)
{
if (optionsCb == null)
throw new ArgumentNullException("optionsCb");
var options = new BarOptions();
optionsCb(options);
if (options.EValue == null)
throw new ArgumentException(@"E cannot be null.", @"optionsCb");
//do something
}
}
//Bar-Call:
fooInstance.Bar(opt => opt.E("something").A(1));
-----------------------------------------------------------------
Thanks.

---
Original message URL:
http://www.jetbrains.net/devnet/message/5264846#5264846



0

Please sign in to leave a comment.