Communicating between two Bounded Contexts in DDD

后端 未结 6 1709
南笙
南笙 2020-11-29 23:34

I have few different Bounded Contexts in the domain. The validation of a CRUD operation is built in each Bounded Context.

For example, I can cr

相关标签:
6条回答
  • 2020-11-29 23:47

    Here is how I would reason about it.

    I would argue that the Game BC doesn't know about "Users", it might however know about "Players".

    If the Game BC is dependant on an active/current player then it should be passed into the BC when creating the Game BC instance.

    eg.

     Player currentPlayer = GetPlayerSomehow...();
     GameBC gameBC = new GameBC(currentPlayer);
     gameBC.DoStuff();
    

    Now your two BC's are still separate, you can test them separately etc.

    And to make it all work you simply do something like:

     User currentUser = GetCurrentUser();
     Player currentPlayer = new Player();
     currentPlayer.IsGroupLeader = currentUser.IsGroupLeader;
     GameBC gameBC = new GameBC(currentPlayer);
     gameBC.DoStuff();
    

    This serves as an anticorruption layer between the UserBC and the GameBC, you can move and validate the state you want from the UserBC into the state you need for your GameBC.

    And if your GameBC needs to access many users, you can still pass some sort of mapping service into the game BC that does this kind of transformation internally.

    0 讨论(0)
  • 2020-11-29 23:50

    I think you're almost there. Close to a good solution. I'm not so sure you have to split these two into two BC's. Your User Aggregateroot (?) and Game maybe belong in one BC and depend on each other. A User "has a" Membership "to one or many" Games (just guessing your entity relations). But I'm just brainstorming now. Try to follow :) Different approaches follows:

    First GameBC has a Create() method that actually take a UserMembership as param. Create(UserMembership). You then through UserMembership entity know what kind of membership and User this. If accepted ,game is created. If not exception is thrown or Game gets a broken rule message, depends on what approach you want to communicate back to client. The coordination can be done in application layer without leakage of domain knowledge.

    Second You do as one of the other answers. You raise a CreateGameEvent within Game.Create(UserId) method. That Event is caught by an EventHandler (registered by IoC in Application startup) that resides in Application layer and look up UserMembership through repository. The small leakage of domain knowledge is that business rule that knows who are allowed yo do what are verified in application layer. This can be solved by letting the CreateGameEventHandler take UserId and RuleRef (can be string "CAN_CREATE_GAME" or enum) and letting a UserPermission object verify the permission. If not. Exception is thrown and catched in application layer. Drawback can be that you do mot want permission reference strings be hardcoded in Create method.

    Third ...continues where second approach ends. You know that GameBC may not be the right place to do user permission lookups if you follow SRP principle. But the action is triggered around that method somehow. An alternative an be a Create(GroupLeader user). OR you can have Game.Create(User user) then do a validation that User is GroupLeader type. Create(GroupLeader) tells you what you need to call this method.

    Last Maybe an alternative that I like more now when writing this. When you want to create an entity I usually let that Create(Save) method be on the repository. The IGameRepository interface is located next to Game Entity in domain assembly project. But You can also create a GameFactory that are responsible for starting the lifecycle of Game Entity. Here is also a good place to put Create method... GameFactory.Create(GroupLeader) { return new Game.OwnerUserId = GroupLeader.Id; } Then you just save it IGameRepository.Save(Game)

    Then you have an intuitive and self describing way of telling other developers that "You must have a GroupLeader instance to create a Game".

    Finally I hope you realize that you know the domain and you will figure out what suits you best. Be pragmatic and don't go Eric Evan hardcore. There is so many devs out there that are stuck in a "religion" in how things are gonna be done. Size of project, money, time, and dependencies to other systems etc. also affect how well you can be strict in doing DDD.

    Good luck.

    0 讨论(0)
  • 2020-11-30 00:00

    I think I may have to follow a different approach where I will make the User entity part of the Game BC (the same entity is part of User BC also). I will use the Repository to read the IsGroupLeader flag from the db in the Game BC. This way, the dependency on the User BC is removed and no communication is needed with the User BC.

    What do you think?

    0 讨论(0)
  • 2020-11-30 00:06

    When integrating BCs, you have a few options. The reason that calling out to an external BC is discouraged is because it requires for both BCs to be operational at the same time. However, this is often quite acceptable and is simpler than the alternative. An alternative is to have the Game BC subscribe to events from the User BC and keep local copies of the data it needs, which in this case is information about whether a user is a group leader. In this way, when the Game BC needs to determine whether a user is a group leader, it doesn't need to call out to the User BC, it just reads locally stored data. The challenge of this event-driven alternative is synchronizing the events. You have the make sure the Game BC receives all appropriate events from the User BC. Another challenge is dealing with eventual consistency, since the BCs may be slightly out of sync at any given point in time.

    CQRS is somewhat orthogonal to this problem.

    0 讨论(0)
  • 2020-11-30 00:06

    To cope with the kind of problems you are facing, we use bounded roles a modeling pattern that emerged over the years and proved to work very well. Bounded contexts are defined after semantical units that often, in enterprise organizations, can be mapped to specific roles.

    That should be obvious considering that different roles face different problems and thus speak slightly (or entirely) different languages.

    Thus, we always model the roles that interact with our application as a junction point between applicative requirements (infrastructure, persistence, UI, localization etc...) and business rules (the domain) and we code them in different modules (aka assemblies or packages).

    As for your second question, CQRS can be a way to code the kind of interactions between BCs that you are describing, but I don't like it in this particular context.

    0 讨论(0)
  • 2020-11-30 00:06

    I would suggest to pass the role of user and user information to Game bounded context service and also have a GroupLeader value object inside Game BC. this way you can always know who is group_leader.

    0 讨论(0)
提交回复
热议问题