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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:14:50+00:00 2026-05-27T15:14:50+00:00

I have a data mapping helper class that uses generics and reflection. Since I’m

  • 0

I have a data mapping helper class that uses generics and reflection.

Since I’m using reflection and generics in my helper class, the code for standard CRUD operations are identical across all of my business objects (as seen in the base class Create() method), so I’m trying to use a base BusinessObject class to handle the repetitive methods.

I want the base class to be able to call my generic DataUtils methods, which for example, accept a reference to the derived business object object to populate SQL parameters.

DataUtils.CreateParams expects an object of type T and a bool (indicates insert or update).

I want to pass “this,” representing my derived object the base class, but I’m getting compile error “The best overloaded match contains invalid parameters.”

If I implement Create() in the derived class, and pass the base class’s Create method a reference to “this”, it works – but then I’m still implementing all the CRUD methods, identically, in each business object class. I want the base class to handle these.

Is it possible for a base class to call a method and pass a reference to the derived object?

Here’s my base class:

public abstract class BusinessObject<T> where T:new()
{
    public BusinessObject()
    { }

    public Int64 Create()
    {
        DataUtils<T> dataUtils = new DataUtils<T>();
        string insertSql = dataUtils.GenerateInsertStatement();
        using (SqlConnection conn = dataUtils.SqlConnection)
        using (SqlCommand command = new SqlCommand(insertSql, conn))
        {
            conn.Open();

            //this line is the problem
            command.Parameters.AddRange(dataUtils.CreateParams(obj, true));
            return (Int64)command.ExecuteScalar();

        }
    }
}

}

And a derived class:

 public class Activity: BusinessObject<Activity>
 {
    [DataFieldAttribute(IsIndentity=true, SqlDataType = SqlDbType.BigInt)]
    public Int64 ActivityId{ get; set; }
    ///...other mapped fields removed for  brevity

    public Activity()
    {
        ActivityId=0;
    }

    //I don't want to have to do this...
    public Int64 Create()
    {

        return base.Create(this);
    }
  • 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-27T15:14:51+00:00Added an answer on May 27, 2026 at 3:14 pm

    Just cast this to T:

    dataUtils.CreateParams((T)this, true);
    

    If you create a public class Evil : BusinessObject<Good>, that will throw an InvalidCastException.

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

Sidebar

Related Questions

I have the following mapping: <hibernate-mapping package=server.modules.stats.data> <class name=User table=user> <id name=id> <generator class=native></generator>
I have a hibernate mapping as follows: <hibernate-mapping> <class name=kochman.elie.data.types.InvoiceTVO table=INVOICE> <id name=id column=ID>
I'm have .dbml Linq to SQL class named DExamination.dbml [global::System.Data.Linq.Mapping.TableAttribute(Name=dbo.Examination)] public partial class Examination
I have a number of tables containing some basic (business related) mapping data. What's
I have data that looks like CUSTOMER, CUSTOMER_ID, PRODUCT ABC INC 1 XYX ABC
I have data from a table in a database (string) that contain text and
I have data that needs to be executed on a certain background thread. I
I have a data access class with an Enum called Salutation: public enum Salutation
I have HTML content stored in a variable. How do I extract data that
I have implemented delta detection while loading data warehouse from transaction systems using an

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.