0

How can I make method invocation order explicit with C#?

I want to make it explicit in code that one method should be called before the other, somewhat similar to the way callbacks work in JavaScript. I'm thinking that something like an array or List of Action might do the trick, but am not sure if this the best way, given what I am trying to achieve - it winds up being a better experience for the user if things happen in a certain order, but I want to make this explicit through code, instead of adding a comment.

Here's what I have so far:

Action<dynamic>[] deleteOperations = new Action<dynamic>[2];
deleteOperations[0] = (resource => this.RemoveImageFromDatabase(resource));
deleteOperations[1] = (filename => _blobStorage.DeleteBlob(filename));
ryanwebjackson
  • 1,017
  • 6
  • 22
  • 36
  • 3
    You can't statically enforce the order of method calls. Perhaps you want a separate method that calls the methods in the order you want? – D Stanley Jan 05 '17 at 03:47
  • Do you need a delegate that executes methods in order when it is called? Not sure I understand what you need to achieve. Can you give an example of how you use your construction? – Yuriy Tseretyan Jan 05 '17 at 03:53
  • That's essentially what I already had. Could Task be useful here? I want to execute one of the methods even if there is an exception with the other. – ryanwebjackson Jan 05 '17 at 03:55
  • 1
    @ryanwebjackson I think so. my first thought was about `Task` and `ContinueWith`. – Yuriy Tseretyan Jan 05 '17 at 04:00
  • If your `RemoveImageFromDatabase` is synchronous, you can simply call them in the order you need. If it is asynchronous, you can use `ContinueWith`. – Yeldar Kurmangaliyev Jan 05 '17 at 04:05
  • Compilation aside - Another option is the "Builder" pattern or something like it; essentially creating an internal API for invocation order. – ryanwebjackson Feb 19 '17 at 19:50
  • UPDATE: Looks like invocation order is a thing that is managed by MulticastDelegate, and the current implementation in the .NET framework executes handlers in the order they were registered. – ryanwebjackson Apr 27 '17 at 20:45

2 Answers2

0

That can be a difficult question, especially without more information. Why not provide a method that performs the desired actions in the order that you require?

void Delete(Resource resource, String filename) {
    try {
        this.RemoveImageFromDatabase(resource);
        }
    catch {
        }
    try {
        _blobStorage.DeleteBlob(filename);
        }
    catch {
        }
    }

This ensures that order is as required and gives you greater control over the operation (allowing you to log exceptions for example).

Dweeberly
  • 4,668
  • 2
  • 22
  • 41
  • This does achieve the desired effect. I was trying to avoid wrapping every line. Thinking about it, however, I could move those into the child methods and call the whole thing something like "ExecuteOrderedDeleteOperations". – ryanwebjackson Jan 05 '17 at 04:15
  • why the empty `catch` blocks? They just swallow any exception which may be worse than just letting them bubble up. – D Stanley Jan 05 '17 at 04:21
0

Have you looked into temporal coupling? Basically, you make the second function dependent on the first.

I don't know if this will help in your situation, but you end up with something like this:

var first = FirstMethod(p1, p2);
var second = SecondMethod(first, p3);

With the second call dependent on the first, you can't call them out of order.

There are some links and ideas in this thread:

How to deal with temporal coupling?

Community
  • 1
  • 1