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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:17:48+00:00 2026-05-27T15:17:48+00:00

I am trying to fill products with values parsed from a website. The process

  • 0

I am trying to fill products with values parsed from a website. The process goes fine. What I want is an easy way (with reduced coupling) to fill values in products easily.
At the moment, the way to add a value is like: productX.Attributes[Price].SetValueFromString("95,3€");. Please see the design below and help me improve it.

class Product
{
    Dictionary<KeyValuePair<Type, AAtribute>> _attributes;

    Product()
    {
        // add values to _attributes. for example
        // Name with StrinAttribute
        // Price with NumericAttribute
        // Images with StringListAttribute
    }
}

enum Type
{
    Name,
    Price,
    Images
    ...
}

abtract class AAtribute
{
    abstract void SetValueFromString(string value);
}

and there are several classes that derive from AAtribute:

  • StringAtribute,
  • StringListAtribute,
  • KeyValueStringListAtribute,
  • BoolAtribute
  • NumericAtribute

I’ve made this design so other classes don’t need to know witch attribute is of what type and how to give values to them.
If for example I want to give value to the Price attribute I would say:

productX.Attributes[Price].SetValueFromString("95,3€");

This has really helped since there are more than 40 attributes. It would be pain to parse value for each one by hand.

My problem is that I wan’t to reduce even more coupling. Any ideas on how to make other classes unaware of AAtributes or product types? Something between decorator and strategy pattern should do but I can’t find a way to it.

The problem that lead me make this question is:
– How can I add values to a ListStringAttribute?

But this rises the problem and know I feel there is a need for refactoring.

  • 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-27T15:17:48+00:00Added an answer on May 27, 2026 at 3:17 pm

    The way that we handle this is that all of our business classes inherit from a common base class.

    The common base class contains a value setter and a value getter which use reflection to set and get properties within the class. If the requested property isn’t present and the caller indicates it is ok to do so, the setter adds the value to a user-defined field (UDF) collection, which is similar to your attributes.

    This approach removes the caller’s need to know anything about how the values are actually stored (i.e. is it a regular property on the class or in the UDF collection) or even what data type it is.

    For example, your caller code:

    productX.Attributes[Price].SetValueFromString("95,3€");
    

    in our system would be:

    productX.SetFieldValue("Price", "95,3€");
    

    We use this extensively for database interaction, form data binding, etc.

    Here is an example of the core setter method:

        public static void SetFieldValue(object oRecord, string sName, object oValue)
        {
            PropertyInfo theProperty = null;
            FieldInfo theField = null;
            System.Type oType = null;
    
            try
            {
                oType = oRecord.GetType();
    
                // See if the column is a property in the record
                theProperty = oType.GetProperty(sName, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public, null, null, new Type[0], null);
                if (theProperty == null)
                {
                    theField = oType.GetField(sName, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public);
                    if (theField != null)
                    {
                        theField.SetValue(oRecord, Global.ValueFromDB(oValue, theField.FieldType.Name));
                    }
                    else
                    {
                        object[] aAttributes = null;
    
                        // See if the class type is decorated with the NoUDFs attribute. If so, do not add the attribute.
                        aAttributes = oType.GetCustomAttributes(typeof(NoUDFsAttribute), true);
                        if (aAttributes.Length == 0)
                        {
                            // Otherwise, anything that is not found as a property or a field will be stored as a UDF
                            oRecord.SetUDFValue(sName, oValue);
                        }
                    }
                }
                else
                {
                    if (theProperty.CanWrite)
                    {
                        theProperty.SetValue(oRecord, Global.ValueFromDB(oValue, theProperty.PropertyType.Name), null);
                    }
                }
            }
            catch (Exception theException)
            {
                // Handle the exception
            }
        }
    

    And one of the getter methods where we get the value as a string:

        public static string GetFieldValueForSQL(object oRecord, string sName)
        {
            PropertyInfo theProperty = null;
            FieldInfo theField = null;
            System.Type oType = null;
    
            try
            {
                oType = oRecord.GetType();
    
                // See if the column is a property in the record
                theProperty = oType.GetProperty(sName, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public, null, null, new Type[0], null);
                if (theProperty == null)
                {
                    theField = oType.GetField(sName, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public);
                    if (theField != null)
                    {
                        return Global.ValueForSQL(theField.GetValue(oRecord), theField.FieldType.Name);
                    }
                    else
                    {
                        UDF oUDF = null;
                        object[] aAttributes = null;
    
                        // See if the class type is decorated with the NoUDFs attribute. If so, do not get the value.
                        aAttributes = oType.GetCustomAttributes(typeof(NoUDFsAttribute), true);
                        if (aAttributes.Length == 0)
                        {
                            oUDF = oRecord.GetUDF(sName);
                        }
    
                        if (oUDF != null)
                        {
                            return Global.ValueForSQL(oUDF.Value);
                        }
                        else
                        {
                            return "Null";
                        }
                    }
                }
                else
                {
                    return Global.ValueForSQL(theProperty.GetValue(oRecord, null), theProperty.PropertyType.Name);
                }
            }
            catch (Exception theException)
            {
                // Handle the exception
                return null;
            }
        }
    

    I’ve left some of the internal method calls that we use to coerce the values into the appropriate format (ValueFromDB, ValueForSQL) in place so you can see how they are used, but they are pretty straightforward to implement.

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

Sidebar

Related Questions

I am trying to fill a form in a php application from a C#
I'm trying to fill an array of 20 ints with numbers from 1-20 in
Trying to find the best way of create an overlap/overlay layer to fill the
i am trying to create a database value from the values submited, and inserting
I am trying to fill a dataset by OleDbDataAdapter from MS Access DB table.
I am trying to fill up my ListPreference from within my Activity rather than
I am trying to fill out a DataGridView with data from a database. The
I'm trying to fill some data from an xml feed we made into my
I'm trying to fill-out a form automatically and press a button on that form
I am trying to fill object[] with List<string> but I cannot figure out how

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.