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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:31:32+00:00 2026-05-31T15:31:32+00:00

I have a UserEntity that is eventually persisted in the DB according to it’s

  • 0

I have a UserEntity that is eventually persisted in the DB according to it’s id property. In this case, the id property is obviously sensitive, because changing it would cause the UserEntity to be saved over a different UserEntity when persisted later.

I would therefore like to help safe guard against something like this happening…

Option 1. Do I FORCE the id to be passed into the constructor, thereby removing the Setter?

  • This would mean that every UserEntity that the Repository is given to persist will be valid (because it has the minimum requirement of an id).
  • The downside is that my Controller wouldn’t be able to create a new UserEntity() because it wouldn’t have an id to give it.

Option 2. Allow Id in the constructor to be null, but not provide a Setter

  • This would essentially create two types of UserEntity objects: ones that have been persisted, and ones that haven’t.
  • I could do new UserEntity() anywhere, fill it with it’s values, then pass to the Repository who would then save it, create a new UserEntity with the new id, and give it back.
  • This is cool, but means I need to double check that my entity has an id anywhere that I want to use it for persisting / domain logic / etc, to make sure that it is a saved entity.

Option 3. Do I not worry so much, and provide a setter for it?

  • This exposes the risk of changing the id of a UserEntity, and overwriting a different UserEntity in the db.
  • On the upside, I can create a new UserEntity() without knowing the id ahead of time, which I can pass to my Repository for saving.
  • 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-31T15:31:33+00:00Added an answer on May 31, 2026 at 3:31 pm

    If you care about the validity of your domain objects, you need to ensure validity in all times.

    This means that you don’t provide setters on properties that you do not want changed directly, but use specified methods to mutate their state.

    If the id in your example never changes, you should only ever set it in the constructor (and enforce it being passed in).

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

Sidebar

Related Questions

I have a User entity that has a IList property. The mapping for this
I have this code: NSEntityDescription *userEntity = [[[engine managedObjectModel] entitiesByName] objectForKey:@User]; User *user =
I have a User entity which has a HasCompletedSecurity property which indicates whether that
I have this: namespace Demo.Framework.Domain { public class UserEntity { public virtual Guid UserId
I have a User entity that contains the username, name and other non sensitive
I have a 'User' Entity that contains an 'Address' Value Object. I have this
I have a user entity that contains a collection of survey entities. i would
In our application, I have seen code written like this: User.java (User entity) public
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have a user entity with the following properties that map to a field

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.