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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:12:09+00:00 2026-05-29T20:12:09+00:00

This will be a somewhat abstract question. I am working on a Data Access

  • 0

This will be a somewhat abstract question.

I am working on a Data Access Layer framework which needs to distinguish between a table, its abstract schema/layout, and concrete table records. I’m afraid that because of this distinction, there will be much code duplication. I could need some input on ways to avoid this.

+-----------+
|    Foo    |
+-----------+
| +Id: Guid |
+-----------+

Note that this diagram could describe any of these: a table schema, a concrete table, or a concrete table record, having a field Id with type Guid.

  • All that’s known in the schema is the field’s name and type.
  • In the concrete (opened) table, the field’s “column index” is additionally known.
  • With records, all of these things are known, plus the field has a concrete value.

Translating this to code, I would get lots of similar types (in pairs of three). I’ll use interfaces to keep the example brief; what I want to show is the similarity of the types:

// these interfaces only need to be implemented once:

interface ISchemaField<T>  {  string Name  { get; }       }


interface ITableField<T>   {  string Name  { get; }        
                              int    Index { get; }       }

interface IRecordField<T>  {  string Name  { get; }        
                              int    Index { get; }       
                              T      Value { get; set; }  }

// these three interfaces are an example for one entity; there would be
// three additional types for each additional entity.

interface IFooSchema
{
    ISchemaField<Guid> Id { get; }
    IFooTable Open(IDbConnection dbConnection, string tableName);
}

interface IFooTable
{
    ITableField<Guid> Id { get; }
    ICollection<IFooRecord> ExecuteSomeQuery();
}

interface IFooRecord
{
    IRecordField<Guid> Id { get; }
}

Now I would like to avoid having to write three very similar implementations for every entity in a concrete data model. What are some possible ways to reduce code duplication?

  • I’ve thought of code generation (e.g. T4), which would be an OK solution, but I would prefer a “manually” coded solution (if there is one) with fewer lines of code & more readable code.

  • I’ve thought about creating one class per entity which implements the schema, table, and record interface all at the same time… but that feels messy and like a violation of Separation of Concerns.

  • 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-29T20:12:10+00:00Added an answer on May 29, 2026 at 8:12 pm

    IMHO, I think you are going a bit too far with the abstraction of the table structure. Overusing interfaces can make the code hard to read. I find it very annoying, for example, that you can’t press F12 to see the implementation of an object because it’s an interface type.

    A proven model that I have worked with for years and it’s super easy to maintain is to keep all class names identical to the table names, then field names that match column names. You can then easily generate methods just using search and replace in the editor, and same goes for code changes. This way you don’t need to keep column names and column indexes in memory. You just hardcode them in your data access tier (HARDCODING IS NOT ALWAYS BAD!). For example:

    this.Price = reader["Price"] as decimal?;
    

    Performance is very good with this approach and code is super maintainable!

    Whatever approach you follow, the key thing is how you keep the mappings from table columns to class properties. I recommend hardcoding them and use a straightforward naming convention (column name = property name). This approach requires you to recompile when you add or change columns, but I think these changes do not happen often enough to justify having the column names in variables or reading them from a separate file. Doing so avoids recompilation, but makes the code less easy to follow. I don’t think it’s worth it.

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

Sidebar

Related Questions

This will be probable quite odd question. But i thought I will give it
This will probably be a bot of a waffly question but ill try my
This question is somewhat similar to this one Best way to deploy large *.war
The requirements of this are somewhat restrictive because of the machinery this will eventually
This will hopefully be an easy one. I have an F# project (latest F#
This will require a little setup. Trust me that this is for a good
[This will only make sense if you've seen Kevin Smith's 'Erlang in Practice' screencasts]
This will probably be obvious but I can't find the best way. I want
This will be implemented in Javascript (jQuery) but I suppose the method could be
This will be a bit subjective, I'm afraid, but I'd value the advice of

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.