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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:19:01+00:00 2026-06-10T00:19:01+00:00

Is there a better way to write this? As one class perhaps, instead of

  • 0

Is there a better way to write this? As one class perhaps, instead of two.

using System;

namespace SnippetTool.Repositories
{
    public abstract class ARepository<TProvider> where TProvider : class
    {
        protected TProvider Provider { get; set; }

        protected ARepository(TProvider provider)
        {
            if (provider == null)
                throw new ArgumentNullException("provider");

            Provider = provider;
        }
    }

    public abstract class ARepository<TProvider, TValidator> : ARepository<TProvider> where TProvider : class where TValidator : class
    {
        protected TValidator Validator { get; set; }

        protected ARepository(TProvider provider, TValidator validator) : base(provider)
        {
            Validator = validator;
        }
    }
}
  • 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-10T00:19:03+00:00Added an answer on June 10, 2026 at 12:19 am

    I don’t think you can do it as one class, currently, generally what I try to do in this situation is create the most general class (the one that takes the most generic args) to have all the logic, then make the more specific ones be subclasses that default those types.

    For example, let’s say we are writing a translator that translates from one type of value to another, so like a Dictionary but also has defaults, etc.

    We could define this as:

    public class Translator<TKey, TValue, TDictionary> where TDictionary : IDictionary<TKey, TValue>, new();
    {
        private IDictionary<TKey, TValue> _map = new TDictionary();
        ...
    }
    

    This is my generic case, which can have any implementation of IDictionary, but say we want a simpler version that always uses Dictionary if not specified, we could do:

    public class Translator<TKey, TValue> : Translator<TKey, TValue, Dictionary<TKey, TValue>>
    {
        // all this does is pass on the "default" for TDictionary...
    }
    

    In this way, I can make:

    // uses Dictionary<int, string>
    var generic = new Translator<int, string>(); 
    
    // uses SortedDictionary instead
    var specific = new Translator<int, string, SortedDictioanry<int, string>>();
    

    So in your case, maybe your generic always has a TValidator property, but it’s defaulted (maybe to always return true in your most generic form?

    For example, maybe you have a definition of a default validator (say called DefaultValidator) you could reverse your definitions so that the more generic (the one that takes more generic type parameters) has all the logic and any specializations (fewer type parameters) are just subclasses that default those extra types:

    using System;
    
    namespace SnippetTool.Repositories
    {
        public class DefaultValidator
        {
           // whatever your "default" validation is, may just return true...
        }
    
        public abstract class ARepository<TProvider> : ARepository<TProvider, DefaultValidator>
            where TProvider : class
        {
            protected ARepository(TProvider provider) : base(provider, new DefaultValidator());
            {
            }
    
            // needs no new logic, just any specialized constructors...
        }
    
        public abstract class ARepository<TProvider, TValidator> 
             where TProvider : class 
             where TValidator : class
        {
            public TValidator Validator { get; set; }
    
            protected ARepository(TProvider provider, TValidator validator) 
            {
                Provider = provider;
                Validator = validator;
            }
    
            // all the logic goes here...
        }
    }
    

    UPDATE: Yes, based on your comment, if the TValidator is an add-on (and not something defaulted), then layering it like you did is appropriate.

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

Sidebar

Related Questions

Is there a better way to write this spec? This works but I don't
I am wondering if there is a better way to write this JavaScript (Jquery)
is there any way to write this foreach in linq or another better way,
Is there better way to enumerate all photos on the device than this one?
Is there a better way to write this code? I know it's very simple,
Is there a better way to write the following function? Having the '#' +
Is there a better/shorter way to write the whoAmI method in the following code?
Is there a better way to code this? def __contains__(self, e): return e in
To share TaskKey among Tasks, which one is better? Are there other better way?
Is there better way to delete a parameter from a query string in a

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.