Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit:</strong> Judging from the other answers, the correct name for such a 'domain service' is <em>specification</em>. I've updated my answer to reflect this, including a more detailed code sample.</p> <p>I'd go with option 3; create a <strike>domain service</strike> specification which encapsulates the actual logic that performs the validation. For example, the specification initially calls a repository, but you could replace it with a web service call at a later stage. Having all that logic behind an abstract specification will keep the overall design more flexible.</p> <p>To prevent someone from editing the name without validating it, make the specification a required aspect of editing the name. You can achieve this by changing the API of your entity to something like this:</p> <pre><code>public class User { public string Name { get; private set; } public void SetName(string name, ISpecification&lt;User, string&gt; specification) { // Insert basic null validation here. if (!specification.IsSatisfiedBy(this, name)) { // Throw some validation exception. } this.Name = name; } } public interface ISpecification&lt;TType, TValue&gt; { bool IsSatisfiedBy(TType obj, TValue value); } public class UniqueUserNameSpecification : ISpecification&lt;User, string&gt; { private IUserRepository repository; public UniqueUserNameSpecification(IUserRepository repository) { this.repository = repository; } public bool IsSatisfiedBy(User obj, string value) { if (value == obj.Name) { return true; } // Use this.repository for further validation of the name. } } </code></pre> <p>Your calling code would look something like this:</p> <pre><code>var userRepository = IoC.Resolve&lt;IUserRepository&gt;(); var specification = new UniqueUserNameSpecification(userRepository); user.SetName("John", specification); </code></pre> <p>And of course, you can mock <code>ISpecification</code> in your unit tests for easier testing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload