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

  • Home
  • SEARCH
  • 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 6791641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:52:16+00:00 2026-05-26T17:52:16+00:00

Sorry if the title isn’t worded the best. My UserRepository has a method that

  • 0

Sorry if the title isn’t worded the best.

My UserRepository has a method that will create a new User. Where should my validation go to check to make sure all the fields are that are needed to create the user are filled in: Username, Password, Email and Created. If any of the values are null then the insert will throw an error.

public class UserRepository : IRepository<User>
{
    public DbConnection Connection { get; set; }

    public UserRepository(DbConnection connection)
    {
        this.Connection = connection;
    }

    public void Create(User user)
    {
        string sql = "INSERT INTO [dbo].[User] (Username, Password, Email, Created) VALUES (@Username, @Password, @Email, @Created)";

        using (DbCommand command = new SqlCommand())
        {
            command.Connection = this.Connection;
            command.CommandText = sql;
            command.Parameters.Add(new SqlParameter("@Username", user.Username));
            command.Parameters.Add(new SqlParameter("@Password", user.Password));
            command.Parameters.Add(new SqlParameter("@Email", user.Email));
            command.Parameters.Add(new SqlParameter("@Created", user.Created));
            command.ExecuteScalar();
        }
    }        
}

Should my create method actually check the values here? I feel it should not do the validation here, but I’m not sure the appropriate place for separation.

  • 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-26T17:52:17+00:00Added an answer on May 26, 2026 at 5:52 pm

    It’s always a good idea to keep business logic separate from data access code. It makes business logic code more readable and maintainable. “Validation” here is part of business logic, therefore I recommend handling it at an upper layer. Think of creating “service” classes which receive input, validate them and perform DB access through repository.

    Let’s say your scenario is “create a new user through registration”. It makes sense to have a RegistrationService class with a CreateUser method.

    There is another option of using User class itself for point of validation. You can make User class immutable, have it receive all parameters in constructor and validate them during construction. Therefore you rule out the possibility of having an “invalid” instance. Any function which receives User as a parameter actually is guaranteed to receive a “valid User” instance.

    As a final note, it helps to make it a habit to check for null arguments and throw ArgumentNullException in public functions, regardless of validation.

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

Sidebar

Related Questions

I just want to create plugin that will when visitor(user,visitor,...) visit some post,remember what
Sorry if the question isn't that clear in the title but I had a
Sorry the title isn't more help. I have a database of media-file URLs that
The title to this question isn't written very well. Sorry 'bout that. I'm curious
Sorry if the title isn't clear enough, but in Javascript you can do this
Sorry if the title isn't very clear, I couldn't think of anything better... I'm
I'm a really newbie, so sorry if the title isn't grammatically correct :p Anyway...
My question title isn't clear, sorry. I tried ;) I have a binary string
First of all, sorry about the title -- I couldn't figure out one that
I know that title didn't make sense, Im sorry! Its hard to word what

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.