2

The methodful roles contains the actual algorithm, but what should the Contexts executing method do but execute one of those methods?

public class SomeContext
{
    // ... Constructor omitted ...

    public void Execute()
    {
        // Is this all?
        someRole.DoStuff(this.anotherRole, this.otherData);
    }
}

It seems very simple, so I'm thinking that the Context should be responsible of for example database lookups. Wouldn't that simplify the methodful roles?

ciscoheat
  • 3,719
  • 1
  • 35
  • 52

1 Answers1

3

The main responsibility of a context is to bind roles to objects. Sometimes one or more of the "execute" methods will be complex but often they are not. They are there to capture the interaction between objects

The binding of role to objects is atomic. It happens at one location in the context and for all roles at the same time.

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • So instantiating a Context with a text string (like a search query), letting it lookup domain objects with a ORM and passing them to the Role method, is a responsibility it should undertake? – ciscoheat Oct 29 '12 at 05:20
  • Of course there could also be other constructors, so it's possible to pass RolePlayers to the Context. (The Execute method will test for object existence, so no work is made in the constructor) – ciscoheat Oct 29 '12 at 05:26
  • @ciscoheat it's perfectly reasonable for the context to find the roleplayers through a ORM. However you do not pass RolePlayers to role methods. A role method to a RolePlayer is like an instance method on any other object. So someRolePlayer.ARoleMethod() is how you woull invoke it not ARoleMethod(someRolePLayer); – Rune FS Oct 29 '12 at 07:18
  • But then how to get strongly typed access to the Context within the Role method? I'm considering the example at http://horsdal.blogspot.se/2009/05/dci-in-c.html where the RolePlayers are passed to a Role method (TransferMoneySourceTrait.TransferTo) because it doesn't have access to the Context. – ciscoheat Oct 29 '12 at 09:15
  • 1
    @horsdal has some good examples on how close you can come to DCI in C# However you can't do DCI in C#. One main issue is that Role methods take presedence over instance methods however extension methods take presedence over extension methods. You can take a look at Marvin at http://fuullOO.info/Examples/Marvin to see a very C#-like language that does support DCI fully. However to the question it's seldom needed to have access to the context for more than getting to the other roles – Rune FS Oct 29 '12 at 11:25
  • 1
    @ciscoheat and RolePLayers are "never" passed to a Role method, you can compare that to pass a field as an argument to a private instance method. You can do that but it's not as common as simply accessing the field in the method and I'd say it's even more unlikely that you would want to pass a RolePlayer – Rune FS Oct 29 '12 at 11:27