Skip to content
Advertisement

Can you somehow overload the query in a method?

So there’s a Customer and a Seller and they’re both User. The only difference between them is that seller has a NameOfBusiness and customer does not.

In the DAL I have this code:

 public void CreateCustomer(UserDTO u)
 {
     using (SqlConnection con = new SqlConnection(connectionString))
     {
         string query = "INSERT INTO [dbo].[User](FirstName, LastName, UserName, Password, HouseNumber, Adress, Zip) " +
             "VALUES(@FirstName, @LastName, @UserName, @Password, @HouseNumber, @Adress, @Zip)";
         using (SqlCommand cmd = new SqlCommand(query, con))
         {
             cmd.Parameters.Add("@FirstName", System.Data.SqlDbType.NVarChar).Value = u.FirstName;
             cmd.Parameters.Add("@LastName", System.Data.SqlDbType.NVarChar).Value = u.LastName;
             cmd.Parameters.Add("@UserName", System.Data.SqlDbType.NVarChar).Value = u.UserName;
             cmd.Parameters.Add("@Password", System.Data.SqlDbType.NVarChar).Value = u.Password;
             cmd.Parameters.Add("@HouseNumber", System.Data.SqlDbType.Int).Value = u.Housenumber;
             cmd.Parameters.Add("@Adress", System.Data.SqlDbType.NVarChar).Value = u.Adress;
             cmd.Parameters.Add("@Zip", System.Data.SqlDbType.NVarChar).Value = u.Zip;

             cmd.ExecuteNonQuery();
         }
     }
 }


public void CreateSeller(UserDTO u)
{
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        string query = "INSERT INTO [dbo].[User](FirstName, LastName, UserName, Password, HouseNumber, Adress, Zip, NameOfBusiness) " +
            "VALUES(@FirstName, @LastName, @UserName, @Password, @HouseNumber, @Adress, @Zip, @NameOfBusiness)";
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.Add("@FirstName", System.Data.SqlDbType.NVarChar).Value = u.FirstName;
            cmd.Parameters.Add("@LastName", System.Data.SqlDbType.NVarChar).Value = u.LastName;
            cmd.Parameters.Add("@UserName", System.Data.SqlDbType.NVarChar).Value = u.UserName;
            cmd.Parameters.Add("@Password", System.Data.SqlDbType.NVarChar).Value = u.Password;
            cmd.Parameters.Add("@HouseNumber", System.Data.SqlDbType.Int).Value = u.Housenumber;
            cmd.Parameters.Add("@Adress", System.Data.SqlDbType.NVarChar).Value = u.Adress;
            cmd.Parameters.Add("@Zip", System.Data.SqlDbType.NVarChar).Value = u.Zip;
            cmd.Parameters.Add("@NameOfBusiness", System.Data.SqlDbType.NVarChar).Value = u.NameOfBusiness;

            cmd.ExecuteNonQuery();
        }
    }
}

Is there a way to overload the query OR change the query so that you can fill in a NULL for NameOfBusiness?

Advertisement

Answer

Option A Do almost nothing

Rename CreateSeller to Create and use it as it is. Name of business is likely empty for customers already.

Option B Do just a little bit

Create could take in a bool parameter and clear name of business.

public void Create(UserDTO u, bool isSeller )
{
...
            cmd.Parameters.Add("@NameOfBusiness", System.Data.SqlDbType.NVarChar).Value = 
                   isSeller ? u.NameOfBusiness : null;

Option C. Craft

Create could take in a bool parameter and modify the query and the Parameters accordingly. Something like the following.

public void Create(UserDTO u, bool isSeller)
{
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        string query = "INSERT INTO [dbo].[User](FirstName, LastName, UserName, Password, HouseNumber, Adress, Zip" +
            (isSeller ? "NameOfBusiness" : "") +
            ") VALUES(@FirstName, @LastName, @UserName, @Password, @HouseNumber, @Adress, @Zip" +
            (isSeller ? "@NameOfBusiness" : "") +
            ")";
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            // (...)
            cmd.Parameters.Add("@Zip", System.Data.SqlDbType.NVarChar).Value = u.Zip;
            if(isSeller) {
                cmd.Parameters.Add("@NameOfBusiness", System.Data.SqlDbType.NVarChar).Value = u.NameOfBusiness;
            }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement