user2462093
user2462093

Reputation: 71

Dictionary dynamic vs object and reference

I have my own data types for use with a database that have various properties to manipulate values such as:

First: original value
IsNull: is the current state null (no value in Data)
IsFirstNull: was the initial state of the object null
Changed: has the value changed since the initial value was set.
SetNull(): set the object to null
SetFirstNull: set the initial value to null
Reset: set values all to original settings.

Each one of the objects has these. There is an object for each type of standard variables, such as:

int - IntType
string - StringType
bool - BoolType

I have these variables in a class for each table I am using.

I want to be able to access these, so I am looking at adding these to a dictionary. But each item would be a different type (IntType, StringType, BoolType etc).

So I set these up as Dictionary<string, object> or as Dictionary<string, dynamic>.

Not sure which is the best - Is one better than the other?

 public class LoginDC
 {
    private IntType loginID = new IntType();
    private StringType userName = new StringType();

    public LoginDC()
    {
       Dictionary<string, dynamic> propertyList = new Dictionary<string, dynamic>();

       propertyList.Add("LoginID", loginID);
       propertyList.Add("UserName", userName);

       propertyList["UserName"].First = "Tom"
    }
 }

So my other question is:

Does propertyList contain the reference to loginID and userName after the .Add? So that if I change either the propertyList or the variable both would contain the same value. Or does propertyList contain a copy of the value in the two variables?

It seems to be a reference but not sure.

Upvotes: 4

Views: 3120

Answers (1)

StriplingWarrior
StriplingWarrior

Reputation: 156634

Both Dictionary<string, object> and Dictionary<string, dynamic> have their down-sides. Using object, you'd have to cast each object to its type before you could use it. Using dynamic, you'd lose compile-time checks on the methods you call, increasing the likelihood of having errors that you don't notice until it's too late.

I wouldn't suggest taking the approach you're taking at all. The commenters are right: you appear to be trying to reinvent the wheel. There are lots of really good libraries for mapping data from a database into objects. Use what's freely available.

To answer your second question:

  • If your custom object types are classes, then propertyList contains references to them.
  • If they are structs, it will contain a copy of them.

You can test this yourself by running a quick script like this in a tool like LinqPad:

void Main()
{
    var a = new A{I = 1};
    var b = new B{I = 1};
    var propertyList = new Dictionary<string, dynamic>();
    propertyList.Add("a", a);
    propertyList.Add("b", b);
    a.I = 2;
    b.I = 2;
    foreach (var value in propertyList.Values)
    {
        Console.WriteLine(value.I);
    }
    // Output:
    //  2
    //  1
}

public class A{public int I{get;set;}}
public struct B{public int I{get;set;}}

Upvotes: 2

Related Questions