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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:47:11+00:00 2026-05-22T22:47:11+00:00

I have an abstract class called DatabaseRow that, after being derived and constructed, is

  • 0

I have an abstract class called DatabaseRow that, after being derived and constructed, is mainly loaded from a Load(object id) method.

I have lots of code that creates a new instance of the class, loads it from an ID, then returns the class. I’d like to simplify this code into one line of code (just to be neat, there are plenty of classes that will just have lists of properties that return these Loaded instances).

There are two ways I can think of doing it, but neither seem ‘right’ to me.

1. I could return this; at the end of my Load method and use return new Derived().Load(id);

2. I could create a generic method to return a loaded method.

public static T LoadRow<T>(object id) where T : DatabaseRow, new()
{
    T row = new T();
    row.Load(id);
    return row;
}

I’ve seen some other code that uses the same method as number 1, but I’ve never seen any experienced developer recommend it, nor have I came across any methods in the .NET framework that do the same thing, so maybe this isn’t best practice?

Does anybody know of any other solutions that could be better than both of these?

Solution:

After reading SirViver’s answer and comment, I realised that all the properties being returned need to be cached anyway. The solution was sightly different, but similar to option 2 (that I wouldn’t expect anyone to come up with this answer as I didn’t explain this part of the design)

All these instances will be loaded from a value retrieved from another column in the database (database relationships if you like). Instead of trying to load the new instance from this value, I made a method to load the instance from the column name and cache the loaded value in a Dictionary. This works well as this is one of the primary functions of the DatabaseRow class.

    private Dictionary<string, DatabaseRow> linkedRows;

    protected T GetLinkedRow<T>(string key) where T : DatabaseRow, new()
    {
        if (linkedRows.ContainsKey(key)) return (T)linkedRows[key];
        else
        {
            T row = new T();
            row.Load(this[key]);
            linkedRows.Add(key, row);
            return row;
        }
    }
  • 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-22T22:47:12+00:00Added an answer on May 22, 2026 at 10:47 pm

    Personally, I think it’s bad practice to chain method calls that have actual sideffects on the object instance. To be honest, I think both examples are quite ugly “hacks” whose only purpose is saving two lines of code. I don’t think the result is actually more readable either.

    If you want a record to be loaded immediately, I’d probably rather supply a constructor variant that takes the ID you load from and make the object auto-populate itself on construction, though when I think about it, I wouldn’t bother at all to be honest – cramming more information in a single line does not make for more read- and maintainable code.

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

Sidebar

Related Questions

I have an abstract base class called Shape from which both Circle and Rectangle
Ok so I have an abstract base class called Product, a KitItem class that
Let say I have abstract class called: Tenant and Customer. The tenant in this
I have an abstract class called camera which PointCamera uses as its super class.
I have an abstract class called 'Template' defined as: [DataContract] public abstract class Template
I have an abstract class called user, and 2 sub classes: RegisteredUser & VisitorUser.
I have an abstract class called Grouping. I have a subclass called GroupingNNA. public
I have some abstract class called IClass (has pure virtual function). There are some
Let's say I have an abstract class called ViewController, and another abstract class called
Ok, so I have an abstract class called Product. I have 3 tables called

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.