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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:46:09+00:00 2026-05-26T09:46:09+00:00

I have 2 independent classes A and B and I have a Storage class

  • 0

I have 2 independent classes A and B and I have a Storage class which manages the storage of objects of type A and B.

I am trying to abstract the code that does the Store of A and B, however I am stuck due to List covariance I could not assign List<object> objList = new List<A>(); in the following code.

 [DataContract]
public class A {
    public int UID;
}

[DataContract]
public class B {
    public int UID;
}

public class Storage {

    public void Store(A a) {
        List<A> aList = ReadA();
        if (aList == null) {
            aList = new List<A>();
        }
        aList.Add(a);
        WriteNodes(aList);
    }

    public void StoreB(B b) {
        List<B> bList = ReadB();
        if (bList == null) {
            bList = new List<B>();
        }
        bList.Add(b);
        WriteNodes(bList);
    }

    public List<A> ReadA() {
        //deserializes from aFileName and returns List<A>
    }

    public List<B> ReadB() {
        //deserializes from bFileName adn returns List<B>
    }

    private static void WriteNodes<T>(List<T> nodeList) {
        FileStream fs = new FileStream(aFileName, FileMode.Create);
        XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
        DataContractSerializer ser =
            new DataContractSerializer(typeof(List<T>));
        ser.WriteObject(writer, nodeList);
        writer.Close();
        fs.Close();
    }
}

If you look at StoreA and StoreB methods they have a generic pattern except for the type that is used. ReadA and ReadB are no problem I could just take the type as another parameter and create a single function Read.

So is it possible to create an abstraction for Store so I don’t end up with StoreA and StoreB methods?

  • 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-26T09:46:10+00:00Added an answer on May 26, 2026 at 9:46 am

    Yes, you can do it by introducing common interface for items to be sotred and extract item type into enum.

    STORAGE

    public class Storage 
    {       
       public Storage()
       {
         // create it once on construction stage
         // so you do not need to check for null each time in Sore()/Read()
         this.AllItems = new List<IItem>();
       }
    
       public IList<IItem> AllItems { get; private set; }
    
       public void Store<TItem>(TItem item)  
          where TItem: IItem
       {          
           this.AllItems.Add(item);
       }
    
       public IEnumerable<IItem> Read(StorageItemType itemType)
       {
          return this.AllItems.Where(item => item.ItemType == itemType);
       }
    }
    

    Abstract Storage Item type (more generic solution):

    // Item types
    enum StorageItemType
    {
      A,
      B
    }
    
    interface IItem
    {
       int UID { get; }
       StorageItemType ItemType { get; }
    }
    
    public abstract class StorageItemBase: IItem
    {
      public int UID { get; private set; }
    
      public abstract StorageItemType ItemType 
    }
    
    public sealed class B : StorageItemBase    
    {
      public override StorageItemType ItemType 
      { 
        get 
        {
           return StorageItemType.B; // !!!
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have 3 classes. Abstract class A Class B extends class A independent
So i have two classes that are independent of eachother, but both are related
In C++, I have an Object class, from which a number of other classes
I have a class CContainer that has some members CMemberX , CMemberY , which
Say I have two independent classes: class Foo { int bar; } class Baz
Suppose I have an event KeyPress subscribed to by various classes. Assume that Class
I have several independent executable Perl, PHP CLI scripts and C++ programs for which
I'm going to have two independent programs (using SqlAlchemy / ORM / Declarative) that
I have a page which has several independent sections. Data is to be populated
I have an update program that is completely independent of my main application. I

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.