views:

97

answers:

6

Hi all,

I'm sorry if my question isn't clear.

I have a class contain some properties and one of them is "Parent" which the same type of this class.

when i read data from Database i set the suitable value for each property. But how could i put the "Parent" property when i read it from database as "ParentID".

If it's not clear I'll put Example.

thank you very much in advanced.

This's my class:

class smsPart
    {
        private int _id;
        private smsPart _parent;
        private string _name;


        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        public smsPart Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }



        public smsPart()
        {
            this._id = 0;
            this._parent = null;
            this._name = null;
        }

        public smsPart(int pID, smsPart pParent, string pName, smsType pType)
        {
            this._id = pID;
            this._parent = pParent;
            this._name = pName;
        }
    }

and when i read data from database i fill "ID" as int and "Name" as string.

but how i would set "Parent" as "smsPart" when i read it from database as "ParentID" int.??

+1  A: 

If you want to use lazy evaluation, one way is to create a private field for the ParentId, which you populate on creation, a private Parent field and a public Parent property:

public class YourClassHere
{
    private YourClassHere _parent;
    private int _parentId;

    public YourClassHere Parent
    {
        get 
        { 
            if (_parent == null)
            {
                _parent = DBWrapper.GetMyClassById(_parentId)
            }
            return _parent
        }
    }
}
Stuart Dunkeld
That would be very good but i want to get information from database just one time could i do it with more efficient way??.
Wahid Bitar
A: 

Then you read the parent data from its ID and create an object (the same class) then pass the data to it to initialize the properties (exactly like you do with the child).

The above is just an example. The way to do it depends on how you handle the current object (the child) and you didn't give us any code to see.

From what I've done. It looks similar to this:

class Data {
    public Data(int pDataID) {
        // Load data of pDataID
        // Assign the loaded data

        this.parent = new Data(ParentID);
    }
}

I hope this shows enough for you to adapt it to fit yours.

NawaMan
A: 

You probably need to do something like this

Parent = GetParentById(ParentID);

Although it really depends on how you have implemented your Data Access Layer

Damian
A: 

You'll have to recursively retrieve data from the database in order to properly materialize your object, until the last record's ParentID is null.

Joepro
+1  A: 

I suppose you generated your database classes manually...

You could use a dictionary ID -> object as follows:

using System;
using System.Collections.Generic;

class Program {

    class Outer {

        static Dictionary<int, Outer> _ids = new Dictionary<int, Outer>();

        public Outer(int id) {
            _ids[id] = this;
        }

        public class Inner {
            public Outer Parent {
                get;
                set;
            }

            public Inner(int parentId) {
                Parent = Outer._ids[parentId];
            }

        }
    }

    static void Main(string[] args) {
        Outer o = new Outer(1);
        Outer.Inner i = new Outer.Inner(1);
    }
}

You might also consider using linq to sql.

Paolo Tedesco
A: 

Hi,

thank you for helping me i did something like this:

    Collection<smsPart> list = new Collection<smsPart>();
    Dictionary<int, smsPart> dic = new Dictionary<int, smsPart>();

    using (conn)
    {
        conn.Open();
        SqlDataReader dr = command.ExecuteReader();
        while (dr.Read())
        {
            smsPart xPart = new smsPart();

            xPart.ID = int.Parse(dr["ID"].ToString());
            if (!string.IsNullOrEmpty(dr["Parent"].ToString()))
                xPart.ParentID = int.Parse(dr["Parent"].ToString());
            xPart.Name = dr["Name"].ToString();

            dic.Add(xPart.ID, xPart);
        }
    }
    foreach (smsPart childPart in dic.Values)
    {
        if (childPart.ParentID > 0)
        {
            childPart.Parent = dic[childPart.ParentID];
        }
        list.Add(childPart);
    }
    return list;

So is this good in your opinion??.

Wahid Bitar