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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:45:03+00:00 2026-06-07T07:45:03+00:00

let’s say I have an generic data access layer, that is supposed to support

  • 0

let’s say I have an generic data access layer, that is supposed to support different implementations. To realize this I have an base interface IPeristenceService that defines all methods to access data inside the persistence tier. The interface could look something like this:

public interface IPersistence
{
    TData GetData<TData, TKey>(TKey Key);
}

So the persistence service cannot create instances of objects that are only defined by their interfaces, so it is a good decision to restrict the implementation of the GetData-method:

public interface IPersistence
{
    TData GetData<TData, TKey>(TKey Key) where TData : class, new();
}

This is pretty straightforward, because developers who are implementing this interface will not be able to compile their code when they try to return an interface pointer. The data inside the persistence layer needs to be represented by implemented objects, not by interfaces!

Now assume we are creating an persistence service, that persists data inside an relational database, represented by entities. All entities are inheriting from one base interface: IEntity. Passing non-entity objects must fail, because the persistence service can only persist entities. So we need to validate if the passed object instance inherits from IEntity. Using the where-notation this would be possible during compile-time, but simply implementing an interface like this:

public class DbPersistence : IPersistence
{
    public TData IPersistence.GetData<TData, TKey>(TKey Key)
        where TData : IEntity, class, new();
    {
        // fancy data access code
    }
}

does not work, does it?!

Is there any way to realize what I am trying to do?

Thank you in advance 🙂

  • 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-06-07T07:45:06+00:00Added an answer on June 7, 2026 at 7:45 am

    Type constraints on TData are inherited from the interface method you are implementing, that’s why it isn’t possible to modify them on the implementation. There are two typical solutions that I can think of:

    Move the IEntity constraint up to the interface definition

    public interface IPersistence
    {
        TData GetData<TData, TKey>(TKey Key) where TData : class, IEntity, new();
    }
    

    This is appropriate in lots of cases because a certain amount of common ground between all persisted objects is both reasonable and desired.

    Implement the original interface explicitly and a specialized interface publicly

    public interface IPersistence
    {
        TData GetData<TData, TKey>(TKey Key) where TData : class, new();
    }
    
    public interface IDbPersistence
    {
        TData GetData<TData, TKey>(TKey Key) where TData : class, IEntity, new();
    }
    
    public class DbPersistence : IPersistence, IDbPersistence
    {
        TData IPersistence.GetData<TData, TKey>(TKey Key)
        {
            // fancy data access code
        }
    
        TData GetData<TData, TKey>(TKey Key)
        {
            return ((IPersistence)this).GetData<TData, TKey>(Key);
        }
    }
    

    This way you get to use your custom persistence layer as a more richly specified IDbPersistence while at the same time retaining compatibility with those parts of the data access layer that only know about IPersistence.

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

Sidebar

Related Questions

Let's say I'm building a data access layer for an application. Typically I have
Let's say that I have a set of relations that looks like this: relations
Let's say we have this code: <form action='' method='POST' enctype='multipart/form-data'> <input type='file' name='userFile'><br> <input
Let's say I have an array of objects with different specific generic types like
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2

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.