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 7693297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:03:28+00:00 2026-05-31T21:03:28+00:00

An Entity (let’s say a UserEntity) has rigid rules for it’s properties, and it

  • 0

An Entity (let’s say a UserEntity) has rigid rules for it’s properties, and it can exist in 2 states – persisted (which means it has an id) and pre-persisted (which means it does not have an id yet).

According to the answer to this question about how to handle required properties, a “real” UserEntity should only ever be created with an id passed in to its constructor.

However, when I need to create a new UserEntity from information sent by the browser, I need to be able to validate the information before persisting into the db.

In the past, I would simply create a blank UserEntity (without an id), set the new properties, and the validate it – but, in this new, more secure way of thinking about Entities, I shouldn’t ever create a new UserEntity without its id.

I don’t want to create TWO places that know how to validate my UserEntity’s properties, because if they ever change (and they will) it would be double the code to update and double the chances for bugs.

How do I efficiently centralize the validation knowledge of my entity’s properties?

Note

One idea I had is reflected in this question, in which I consider storing the non-state properties like email, password and name in a standardized value object that would know about the rules for its properties that different services, like the Controller, Validator, and Repo, or Mapper could use.

  • 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-31T21:03:29+00:00Added an answer on May 31, 2026 at 9:03 pm

    that’s what factories are for. to the factory method you pass only the data that is required to enforce the real invariants of UserEntity (take some time to figure out what are your real invariants of UserEntity and you’d better do it with your domain experts).

    in the factory method you create a new Id and pass it to the UserEntity constructor.

    In this stage i don’t think it is that bad to discard the instance if the validation inside the constructor fails. in the worst case – you’ve lost an id… it’s not a case that suppose to happen quite often – most of the time the data should be validated in the client.

    Of course another option is that in the factory method you first validate the parameters and only then create a new Id and pass it to the UserEntity constructor.

    itzik saban

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

Sidebar

Related Questions

Let's say I have an entity Author which has a name can write 0
Let say I have an entity user which has a one to many relationship
Let's say we have an entity that has attributes att1 and att2, where att1
Let's say I have an Account entity, and an AccountData entity (which stores some
Let's say there is an entity called Product, which is connected to Shop (a
Let's say I have a Meeting entity. Each meeting has a single attendee and
Let's say I have a House entity which maps to many Person entities. I
I have many to one relation, let's say User has a Company. Company entity
Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
Let's say I have the typical entity Car class Car : Entity { public

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.