Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:50:43+00:00 2026-05-31T12:50:43+00:00

Suppose a MailConfiguration class specifying settings for sending mails : public class MailConfiguration {

  • 0

Suppose a MailConfiguration class specifying settings for sending mails :

public class MailConfiguration {

  private AddressesPart addressesPart;

  private String subject;

  private FilesAttachments filesAttachments;

  private String bodyPart;

  public MailConfiguration(AddressesPart addressesPart, String subject, FilesAttachments filesAttachements,
    String bodyPart) {
    Validate.notNull(addressesPart, "addressesPart must not be null");
    Validate.notNull(subject, "subject must not be null");
    Validate.notNull(filesAttachments, "filesAttachments must not be null");
    Validate.notNull(bodyPart, "bodyPart must not be null");
    this.addressesPart = addressesPart;
    this.subject = subject;
    this.filesAttachements = filesAttachements;
    this.bodyPart = bodyPart;
  }
  // ...  some useful getters ......

}

So, I’m using two values objects : AddressesPart and FilesAttachment.

Theses two values objects have similar structures so I’m only going to expose here AddressesPart :

public class AddressesPart {

  private final String senderAddress;

  private final Set recipientToMailAddresses;

  private final Set recipientCCMailAdresses;

  public AddressesPart(String senderAddress, Set recipientToMailAddresses, Set recipientCCMailAdresses) {
    validate(senderAddress, recipientToMailAddresses, recipientCCMailAdresses);
    this.senderAddress = senderAddress;
    this.recipientToMailAddresses = recipientToMailAddresses;
    this.recipientCCMailAdresses = recipientCCMailAdresses;
  }

  private void validate(String senderAddress, Set recipientToMailAddresses, Set recipientCCMailAdresses) {
    AddressValidator addressValidator = new AddressValidator();
    addressValidator.validate(senderAddress);
    addressValidator.validate(recipientToMailAddresses);
    addressValidator.validate(recipientCCMailAdresses);
  }

  public String getSenderAddress() {
    return senderAddress;
  }

  public Set getRecipientToMailAddresses() {
    return recipientToMailAddresses;
  }

  public Set getRecipientCCMailAdresses() {
    return recipientCCMailAdresses;
  }

}

And the associated validator : AddressValidator

public class AddressValidator {

  private static final String EMAIL_PATTERN
    = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

  public void validate(String address) {
    validate(Collections.singleton(address));
  }

  public void validate(Set addresses) {
    Validate.notNull(addresses, "List of mail addresses must not be null");
    for (Iterator it = addresses.iterator(); it.hasNext(); ) {
      String address = (String) it.next();
      Validate.isTrue(address != null && isAddressWellFormed(address), "Invalid Mail address " + address);
    }
  }

  private boolean isAddressWellFormed(String address) {
    Pattern emailPattern = Pattern.compile(EMAIL_PATTERN);
    Matcher matcher = emailPattern.matcher(address);
    return matcher.matches();
  }
}

Thus, I have two questions :

1) If for some reasons, later, we want to validate differently an address mail (for instance to include/exclude some aliases matching to existing mailingList), should I expose a kind of IValidator as a constructor parameter ? like the following rather than bringing concrete dependence (like I made):

public AddressValidator(IValidator myValidator) {
   this.validator = myValidator;
}

Indeed, this will respect the D principle of SOLID principle : Dependency injection.

However, if we follow this logical, would a majority of Values Objects own an abstract validator or it’s just an overkill the most of time (thinking to YAGNI ?) ?

2) I’ve read in some articles than in respect of DDD, all validations must be present and only present in Aggregate Root, means in this case : MailConfiguration.

Am I right if I consider that immutable objects should never be in an uncohesive state ? Thus, would validation in constructor as I made be preferred in the concerned entity (and so avoiding aggregate to worry about validation of it’s “children” ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:50:44+00:00Added an answer on May 31, 2026 at 12:50 pm

    I’m not quite sure if I follow you 100%, but one way to handle ensuring immutable objects are only allowed to be created if they are valid is to use the Essence Pattern.

    In a nutshell, the idea is that the parent class contains a static factory that creates immutable instances of itself based on instances of an inner “essence” class. The inner essence is mutable and allows objects to be built up, so you can put the pieces together as you go, and can be validated along the way as well.

    The SOLID principals and good DDD is abided by since the parent immutable class is still doing only one thing, but allows others to build it up through it’s “essence”.

    For an example of this, check out the Ldap extension to the Spring Security library.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose I have string Name and Image Photo as properties of a class in
Suppose the following constructor: class Needed { public: Needed () {} Needed (const char
Suppose I have a FruitDetector class which takes in a string and returns the
Suppose I define an export in AssemblyA: [Export(typeof(Foo))] public class Foo { ... }
Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}
Suppose you have the following Generic class heirarchy: public abstract class GenericBase<T> { T
Suppose I have some code like this: public string SomeMethod(int Parameter) { string TheString
Suppose we have the Lyric model: class Lyric < ActiveRecord::Base belongs_to :song end and
Suppose I have a grid of squared defined like so in a class: Square

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.