0

EDIT:

A stored procedure returns the following data:

|  Name   |   Value   |
|---------|-----------|
| Member1 | someValue | 
| Member2 | someValue |

And I have a class that with properties that have a custom attribute that matches the "Name" field in the database

class example{
    [CustomMapping(DBName="Member1",...)]
    public string MyName1{get; set;}

    [CustomMapping(DBName="Member2",...)]
    public int MyName2{get; set;}
}

And I have the following method to populate the class from DB:

public void LoadFromDataReader(IDataReader reader)
{
        Type type = this.GetType();
        PropertyInfo[] props = type.GetProperties();

        foreach (PropertyInfo property in props)
        {
            CustomMappingAttribute mappingAttribute = (CustomMappingAttribute)Attribute.GetCustomAttribute(property, typeof(CustomMappingAttribute));

            object val = null;

            if (property.GetType() == typeof(string))
            {
                val = CustomObjectData.SafeRead<string>(mappingAttribute.DBName, reader, string.Empty);
            }
            else
            {
                val = CustomObjectData.SafeRead<int>(mappingAttribute.DBName, reader, int.MinValue);
            }

            property.SetValue(property, val);
        }
}

I want to assign each property its corresponding value from the DB. The code above will not do that accurately as it will overwrite some property every time LoadFromDataReader() is called because of the for loop.

How do I go about doing this?

FYI: CustomObjectData.SafeRead reads the fieldName from the DB and assigns it to the property.

SafeRead<T>(string fieldName, IDataReader reader, T defaultValue);

.

.

.

.

.

Old Question:

I want to create a class that is populated from the database. The class members as well as their values are to be read from the database.

A stored procedure returns the following data:

|  Name   |   Value   |
|---------|-----------|
| Member1 | someValue | 
| Member2 | someValue |

Now I want my class to have the following structure read/loaded from the stored procedure:

class example{

    public string Member1{get; set;}
    public string Member2{get; set;}
}

There will always be a row returned, and the stored procedure could return any number of Key/Value pairs.

Is this possible? Is there a better way to do it?

I want to do this because I will be serializing this class to JSON. And this becomes trivial to do if I have the class structured correctly.

stackErr
  • 4,130
  • 3
  • 24
  • 48
  • 2
    Yes, it's definitely possible, in a number of ways. How much have you read up on database access in .NET? Are you using LINQ already? – Jon Skeet Dec 15 '14 at 15:58
  • You could use an [ExpandoObject](http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.100).aspx), but I wouldn't recommend to do that. Why don't you want to use a simple `List`? It can be easily (de-)serialized. – dymanoid Dec 15 '14 at 15:59
  • 1
    @stackErr LINQ applies to everything ;-) – Murven Dec 15 '14 at 16:04
  • 2
    I would recommend reading a LINQ to SQL or Entity Framework tutorial. It's a broader topic than can comfortably be handled in a single question/answer. (Nor do you *have* to use LINQ, of course...) – Jon Skeet Dec 15 '14 at 16:04
  • This answer may be helpful to you: http://stackoverflow.com/questions/20157421/how-to-map-object-class-property-to-dictionary-when-object-property-and-dictio – Murven Dec 15 '14 at 16:12
  • OK, if you do not know the names of the members being read, you will certainly have to make use of reflection at some point, in order to map the names of your class members with the names of the items returned from your database. – Murven Dec 15 '14 at 16:18
  • This one uses reflection when you do not know the contents of the dictionary http://stackoverflow.com/questions/4943817/mapping-object-to-dictionary-and-vice-versa – Murven Dec 15 '14 at 16:36
  • Voting leave closed because I can't easily understand what the question is asking after reading it several times. – Superbest Dec 16 '14 at 20:46

1 Answers1

0

I think that you can do something like

public class Example
    {
        public IEnumerable<string> Members { get; set; }
    }

If all of them are string, you can use something like this, if not, you could use T in order to make this generic.

I hope this helps

Oscar Bralo
  • 1,912
  • 13
  • 12