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

The Archive Base Latest Questions

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

I am trying to write a sort of extendable data layer for my application

  • 0

I am trying to write a sort of extendable data layer for my application One of the “repositories” is an in-memory implementation of my abstract store class

public abstract class Store<TEntity, TIdentifier> : IStore<TEntity, TIdentifier>
        where TEntity : StorableEntity<TIdentifier>        
{
  //abstract methods here

  public abstract TIdentifier GetUniqueIdentifier();
}

The “StorableEntity” abstract class is:

public abstract class StorableEntity<TIdentifier>
{
    public TIdentifier ID { get; set; }
}

I have a concrete class of Store called “InMemoryStore” that looks like this:

public class InMemoryStore<T, U> : Store<T, U>
    where T : StorableEntity<U>
{
    protected static Dictionary<U, T> store = new Dictionary<U, T>();

    public override U GetUniqueIdentifier()
    {
    // call relevant "generator" here - SOMETHING LIKE THIS??
    // var generator = GetGeneratorSomehow(U);
    // return generator.Create();
    }
}

Now, the type of “U” here could be string, int, Guid etc…
(most of the time it could be int)

My idea here is to create something like an IUIDGenerator like this:

public interface IUIDGenerator<T>
{
    T Create(ICollection<T> collection);
}

In the above “InMemoryStore” i would then create an instance of an IUIDGenerator, pass in the store dictionarys key collection, and call the “Create” method to return a unique identifier of the required type.

For example, i could have an IntUIDGenerator class like this (that would act as a kind of incremental number generator, based on the numbers already in the dictionary keys)

public class IntUIDGenerator : IUIDGenerator<int>
{
    public int Create(ICollection<int> collection)
    {
        var result = collection.Max() + 1;
        if (collection.Contains(result))
            return result;

        throw new NotUniqueException();
    }
}

Actual Question:
What I need to do, is within InMemoryStore, identify the type of U (the type of the identifier) and be able to dynamically select the required concrete implementation of IUIDGenerator – how can i do this?

I thought about having a type of class factory pattern – loading all the available UIDGenerators into a dictionary… but they can all have different types?

Is there a better way around this?

Also, I am aware the title of my question may be a bit off – if anyone has a better suggestion, please feel free to comment and I’ll change it.

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

    You could use an IoC framework, such as Unity, Castle, Ninject, etc. Then you’d configure your container with something like the following:

    _container = new UnityContainer();
    _container.RegisterType<IUIDGenerator<int>, IntUIDGenerator);
    _container.RegisterType<IUIDGenerator<Guid>, GuidUIDGenerator);
    

    Then in your class you’ve have something like the following:

    public override U GetUniqueIdentifier()
    {
        var generator = _container.Resolve<IUIDGenerator<U>>();
        return generator.Create();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a test for the following method: public class Sort {
I'm trying to write a Python class that acts like some sort of datastore.
I'm trying to write a simple implementation of a patience sort, using Scala. I've
I’m trying to write a web app using 4-tier design pattern ( Data Store,
I am currently trying to write a program to store data about people into
I'm trying to write a Counting sort function but for some reason it's getting
I'm trying to write a merge-sort function that takes a list and a comparison
I'm trying to write a code to actually sort my array in an ascending
I am trying to write a quicksort function to sort anywhere between 10 and
I'm trying to sort an array, filled with objects from a class i wrote.

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.