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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:55:49+00:00 2026-05-16T03:55:49+00:00

I have a class filled with properties. When the properties are accessed it reads

  • 0

I have a class filled with properties. When the properties are accessed it reads some values out of an XDocument.

public class Foo
{
 private XDocument root;

 public Foo(Stream str)
 {
  root = XDocument.load(str);
 }

 public String Bar
 {
  get
  {
   return root.Element("bar").Value;
  }
 }

}

Only it seems a bit of overhead, since everytime it get accessed it has to read the XDocument again. I tried to ‘cache’ this a little bit as following

public String Bar
{
 get
 {
  if(String.IsNullOrEmpty(this.Bar))
   return root.Element("bar").Value;
  else
   return this.Bar;
 }
}

This seems pretty decent for me, only I have one problem with it. The class has like ~200 properties. Everytime I have to do this check, and since OOP is all about not copying large parts of code is there any way to make this work like this automatically?

  • 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-16T03:55:50+00:00Added an answer on May 16, 2026 at 3:55 am

    As “thelost” mentioned, don’t have a single field for each property. Keep a dictionary as a cache, so you don’t pay anything for properties you haven’t accessed.

    I would suggest you then have a method which you can provide with the mechanism for retrieving the real value. For example:

    public class Foo
    {
        private enum CacheKey
        {
            Bar, Baz, ...;
        }
    
        private readonly XDocument doc;
        private readonly Dictionary<CacheKey, string> cache;
    
        private string Fetch(CacheKey key, Func<XDocument, string> computation)
        {
            string result;
            if (!cache.TryGetValue(key, out result))
            {
                result = computation(doc);
                cache[key] = result;
            }
            return result;
        }
    
        public string Bar
        {
            get { return Fetch(CacheKey.Bar, doc => doc.Element("bar").Value); }
        }
    }
    

    That way each property ends up reasonably compact – it basically expresses the cache key involved, and how to compute the property. If you need properties of different types, you might want the cache to just have TValue as object, and make the Fetch method generic, casting where necessary. That will end up boxing value types, admittedly.

    If you use this approach in several places, you may well want to create a generic ComputingCache class to avoid repeating the logic.

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

Sidebar

Related Questions

I have class with back reference: public class Employee : Entity { private string
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have a ArrayList containing Attributes class Attribute{ private int id; public string getID(){
I`m trying to display classes that have properties of type some custom class inside
I have a custom class and std::vector filled with objects of this class. And
I have an array filled with instances of a custom class which contains two
I have following problem. In my view model I defined some list properties as
I have a public class I'm defining that will eventually be part of an
I have a web service which returns something of type MyData . public class
I have the following class: public class MainActivity extends Activity { /** Called when

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.