Validation in CQRS Journey Conference project

"wonderfulworldofpingpong" <[email protected]>
Newsgroups gmane.comp.programming.domain-driven-design
Message-ID <[email protected]>
The following code (in yellow background) is taken from the CQRS Journey
project <http://msdn.microsoft.com/en-us/library/jj554200.aspx>  . In
this code, a conference can be created in the Conference Bounded
Context. No CQRS is being used here but simple CRUD.

You can see a validation happening in this snippet of code in the
CreateConference which is part of the Conference Service. If there is
already an existing slug, the method throws an exception. This is a
simple validation, so doing a validation at the service level may be
okay. But we can make an argument that the validation has now leaked to
the service.

In real applications, in the place of checking for duplicate slug, there
would be complicated validation logic. Also, the Conference BC may have
to talk to other Bounded Contexts to decide on whether the conference
can be created.

My question is, how do we handle the creation of a conference with
validation by talking to other Bounded Contexts? (1) Is it okay for the
following method / command wait until the other BC returns with the
validation status? The operation is synchronous. If the validation from
the other Bounded Context is true, then continue creating the
conference. (2) Instead of making this as synchronous, the Conference
service in this method / command, send a validation command to the other
Bounded Context. Then in the HandleEvent, create the conference. This
approach can scale.

       public void CreateConference(ConferenceInfo conference)
         {
             using (var context = new
ConferenceContext(this.nameOrConnectionString))
             {
                 var existingSlug = this.retryPolicy.ExecuteAction(() =>
                     context.Conferences
                         .Where(c => c.Slug == conference.Slug)
                         .Select(c => c.Slug)
                         .Any());

                 if (existingSlug)
                     throw new DuplicateNameException("The chosen
conference slug is already taken.");

                 // Conference publishing is explicit.
                 if (conference.IsPublished)
                     conference.IsPublished = false;

                 context.Conferences.Add(conference);
                 this.retryPolicy.ExecuteAction(() =>
context.SaveChanges());

                
this.PublishConferenceEvent<ConferenceCreated>(conference);
             }
         }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.