Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm no expert on DDD but I remember an article from the great Jeremy Miller that answered this very question for me. You would typically want logic related to your domain objects - inside those objects, but your service class would exec the methods that contain this logic. This helped me push domain specific logic into the entity classes, and keep my service classes less bulky (as I found myself putting to much logic inside the service classes like you mentioned)</p> <p><strong>Edit: Example</strong> </p> <p>I use the enterprise library for simple validation, so in the entity class I will set an attribute like so:</p> <pre><code> [StringLengthValidator(1, 100)] public string Username { get { return mUsername; } set { mUsername = value; } } </code></pre> <p>The entity inherits from a base class that has the following "IsValid" method that will ensure each object meets the validation criteria </p> <pre><code> public bool IsValid() { mResults = new ValidationResults(); Validate(mResults); return mResults.IsValid(); } [SelfValidation()] public virtual void Validate(ValidationResults results) { if (!object.ReferenceEquals(this.GetType(), typeof(BusinessBase&lt;T&gt;))) { Validator validator = ValidationFactory.CreateValidator(this.GetType()); results.AddAllResults(validator.Validate(this)); } //before we return the bool value, if we have any validation results map them into the //broken rules property so the parent class can display them to the end user if (!results.IsValid()) { mBrokenRules = new List&lt;BrokenRule&gt;(); foreach (Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult result in results) { mRule = new BrokenRule(); mRule.Message = result.Message; mRule.PropertyName = result.Key.ToString(); mBrokenRules.Add(mRule); } } } </code></pre> <p>Next we need to execute this "IsValid" method in the service class save method, like so:</p> <pre><code> public void SaveUser(User UserObject) { if (UserObject.IsValid()) { mRepository.SaveUser(UserObject); } } </code></pre> <p>A more complex example might be a bank account. The deposit logic will live inside the account object, but the service class will call this method.</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.
    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