tags:

views:

35

answers:

1

Our architect has spoken about using SOA techniques throughout our codebase, even on interfaces that are not actually hosted as a service. One of his requests is that we design our interface methods so that we make no assumptions about the actual implementation. So if we have a method that takes in an object and needs to update a property on that object, we explictly need to return the object from the method. Otherwise we would be relying on the fact that Something is a reference type and c# allows us to update properties on a reference type by default.

So:

public void SaveSomething(Something something)
{
  //save to database

  something.SomethingID = 42;
}

becomes:

public Something SaveSomething(Something something)
{
  //save to database

  return new Something
  {
    //all properties here including new primary key from db
  };
}

I can't really get my head around the benefits of this approach and was wondering if anyone could help?

Is this a common approach?

+1  A: 

I think your architect is trying to get your code to have fewer side effects. In your specific example, there isn't a benefit. In many, many cases, your architect would be right, and you can design large parts of your application without side effects, but one place this cannot happen is during operations against a database.

What you need to do is get familiar with functional programming, and prepare for your conversations about cases like these with your architect. Remember his/her intentions are most likely good, but specific cases are YOUR domain. In this case, the side effect is the point, and you would most likely want a return type of bool to indicate success, but returning a new type doesn't make sense.

Show your architect that you understand limiting side effects, but certain side effects must be allowed (database, UI, network access, et cetera), and you will likely find that he or she agrees with you. Find a way to isolate the desired side effects and make them clear to him or her, and it will help your case. Your architect will probably appreciate it if you do this in the spirit of collaboration (not trying to shoot holes in his or her plan).

A couple resources for FP:

  1. A great tutorial on Functional Programming
  2. Wikipedia's entry on Functional programming

Good luck, I hope this helps.

Audie