patterncsharpMinor
helper method returning a call to itself
Viewed 0 times
itselfmethodhelpercallreturning
Problem
I wanted to eliminate some duplication and came up with something I've not seen so I figured I post it and see if others had.
Using a delegate to allow the method to return another call to itself.
The commented out code is what I replaced.
Update: I worked it into my repository class as a static method. I know this is all probably obfuscating from a clean code standpoint, but it's fun to experiment with new things from time to time.
```
public class Repository : IDisposable
{
...
public delegate Chain Chain(Action action);
public static Chain Command(Action action)
{
using (var repo = new Data.Repository(true))
{
action(repo);
}
return new Chain(next => Command(next));
}
...
}
public class Server : IServer
{
...
public Server(string name)
{
serverName = name;
Data.Repository.Command
(repo => repo.AddServer(name));
}
public void Register(ISender sender)
{
Data.Repository.Command
(repo => repo.AddClient(sender.Name, true))
(repo => repo.AddMessage
Using a delegate to allow the method to return another call to itself.
The commented out code is what I replaced.
delegate ChainRepositoryCommand ChainRepositoryCommand(Action action);
ChainRepositoryCommand RepositoryCommand(Action action)
{
using (var repo = new Data.Repository(true))
{
action(repo);
}
return new ChainRepositoryCommand(a => RepositoryCommand(a));
}
public void Register(ISender sender)
{
RepositoryCommand
(repo => repo.AddClient(sender.Name, true))
(repo => repo.AddMessageType())
(repo => repo.AddRegistration(this.name, sender.Name));
//using (var repo = new Data.Repository(true))
//{
// repo.AddClient(sender.Name, true);
//}
//using (var repo = new Data.Repository(true))
//{
// repo.AddMessageType();
//}
//using (var repo = new Data.Repository(true))
//{
// repo.AddRegistration(this.name, sender.Name);
//}
sender.Sending += sender_Sending;
}Update: I worked it into my repository class as a static method. I know this is all probably obfuscating from a clean code standpoint, but it's fun to experiment with new things from time to time.
```
public class Repository : IDisposable
{
...
public delegate Chain Chain(Action action);
public static Chain Command(Action action)
{
using (var repo = new Data.Repository(true))
{
action(repo);
}
return new Chain(next => Command(next));
}
...
}
public class Server : IServer
{
...
public Server(string name)
{
serverName = name;
Data.Repository.Command
(repo => repo.AddServer(name));
}
public void Register(ISender sender)
{
Data.Repository.Command
(repo => repo.AddClient(sender.Name, true))
(repo => repo.AddMessage
Solution
Using a delegate to allow the method to return another call to itself.
You don't necessarily need a delegate to allow another call to itself. For example:
I'm not saying your code is wrong, only saying that you don't need delegates to achieve this. Also this:
Could also be rewritten as:
I don't know if you already knew or tried this but I'm just pointing it out as a possibility. But I agree with you: experimenting with code is fun! :)
You don't necessarily need a delegate to allow another call to itself. For example:
public class SimpleCalculation
{
private int result;
public int Result
{
get { return result; }
}
public SimpleCalculation Add(int i)
{
result += i;
return this;
}
public SimpleCalculation Subtract(int i)
{
result -= i;
return this;
}
}
//Usage:
var sc = new SimpleCalculation();
sc.Add(5).Subtract(2);
Console.WriteLine(sc.Result); //Writes '3' to the screenI'm not saying your code is wrong, only saying that you don't need delegates to achieve this. Also this:
//using (var repo = new Data.Repository(true))
//{
// repo.AddClient(sender.Name, true);
//}
//using (var repo = new Data.Repository(true))
//{
// repo.AddMessageType();
//}
//using (var repo = new Data.Repository(true))
//{
// repo.AddRegistration(this.name, sender.Name);
//}Could also be rewritten as:
using (var repo = new Data.Repository(true))
{
repo.AddClient(sender.Name, true);
repo.AddMessageType();
repo.AddRegistration(this.name, sender.Name);
}I don't know if you already knew or tried this but I'm just pointing it out as a possibility. But I agree with you: experimenting with code is fun! :)
Code Snippets
public class SimpleCalculation
{
private int result;
public int Result
{
get { return result; }
}
public SimpleCalculation Add(int i)
{
result += i;
return this;
}
public SimpleCalculation Subtract(int i)
{
result -= i;
return this;
}
}
//Usage:
var sc = new SimpleCalculation();
sc.Add(5).Subtract(2);
Console.WriteLine(sc.Result); //Writes '3' to the screen//using (var repo = new Data.Repository(true))
//{
// repo.AddClient(sender.Name, true);
//}
//using (var repo = new Data.Repository(true))
//{
// repo.AddMessageType<T>();
//}
//using (var repo = new Data.Repository(true))
//{
// repo.AddRegistration<T>(this.name, sender.Name);
//}using (var repo = new Data.Repository(true))
{
repo.AddClient(sender.Name, true);
repo.AddMessageType<T>();
repo.AddRegistration<T>(this.name, sender.Name);
}Context
StackExchange Code Review Q#28259, answer score: 2
Revisions (0)
No revisions yet.