Search code examples
c#containersclass-design

Class structure for the proposed data and its containers?


First I would like to wish a happy new year to everyone that may read this :)

I am having trouble on how to make a container for some data that I am importing into my application, and I am not sure on how to explain this very well and my english is not really a that good so I hope you can bear with my mistake and help me with some guidance.

Currently with a foreach I am importing the follow fields from the data I receive: guid, itemid, name, owner(optional, can be null), zoneid, titleid, subid, heading, x, y, z, typeA, typeB, typeC

From the above fields I need to store a Waypoint list of all coords a given item has moved to BUT for each guid I have a new list of waypoints.

And from the waypoint list the first entry is also my initial item start location which would be my item initial position (if you notice i have a separate list for it which I was not sure would be better or not) not all items have a waypoint list but all items have the first position.

So the first idea I came with to store this data was a list with a class with 2 inner classes with their list:

public List<ItemList> myList = new List<ItemList>();
public class ItemList
{
    public int GuID { get; set; }
    public int ItemID { get; set; }
    public string Name { get; set; }
    public int TitleID { get; set; }
    public itemType Status { get; set; }

    public class Waypoint
    {
        public int Zone { get; set; }
        public int SubID { get; set; }
        public int Heading { get; set; }
        public float PosX { get; set; }
        public float PosY { get; set; }
        public float PosZ { get; set; }
    }
    public List<Waypoint> Routes = new List<Waypoint>();
}

So here is an example of how I would add a new waypoint to a GUID that exists in the list

                    ItemList myItem = myList.Find(item => item.GuID == GUID);
                    if (myItem != null)
                    {
                        int lastDistance = 3;
                        ItemList.Waypoint nextWaypoint;

                            // Add new Coordinates to the given GUID
                            ItemList.Waypoint lastWaypoint = myItem.Routes.LastOrDefault();
                            if (lastWaypoint != null)
                            {
                                lastDistance = getDistance(posX, posY, posZ, lastWaypoint.PosX, lastWaypoint.PosY, lastWaypoint.PosZ);
                                if (lastDistance > 2)
                                {
                                    nextWaypoint = new ItemList.Waypoint();
                                    nextWaypoint.SubID = subID;
                                    nextWaypoint.Zone = zone;
                                    nextWaypoint.Heading = convertHeading(heading);
                                    nextWaypoint.PosX = posX;
                                    nextWaypoint.PosY = posY;
                                    nextWaypoint.PosZ = posZ;
                                    nextWaypoint.Rest = rest;
                                    myItem.waypoint.Add(nextWaypoint);
                                }
                            }
                    }

Then to register a new item I would take advantage of the itemExist above so I won't register the same GUID again:

                    ItemList newItem = new ItemList();
                    newItem.GuID = GUID;
                    newItem.ItemID = itemID;
                    newItem.Name = name;
                    newItem.Status = status;
                    newItem.TitleID = titleID;

                    // Item location
                    ItemList.Location itemLocation = new ItemList.Location();
                    itemLocation.SubID = subID;
                    itemLocation.Zone= zone;
                    itemLocation.Heading = convertHeading(heading);
                    itemLocation.PosX = posX;
                    itemLocation.PosY = posY;
                    itemLocation.PosZ = posZ;
                    newItem.position.Add(itemLocation);
                    myList.Add(newItem);

Could you help me with advices on how my class structure and lists should look like ?

Are there better ways to interate with the lists to get lastWaypoint of a GUID or verify wether an item exist or not ?

What else would you advise me in general ?

PS: If you have any questions or if there is something I missed to post please let me know and I will update it.

UPDATE: have changed the above to reflect what I currently have and if anyone else still have advices I would like to hear.


Solution

  • A few notes (after two glasses of champaign…):

    • You don't state any requirements for the data structures and their usage. Your design looks quite reasonable.
    • You can use the FirstOrDefault() method to check for existence (i.e. the return value != null) of an item and retrieve it from the list in one operation.
    • Your code has an apparent bug: there's no nextWaypoint != null check before the new waypoint is added and it need not exist. Likewise there shall be no lastWaypoint != null check necessary as the list won't contain any null values.
    • I'd recommend introducing constructors to your classes accepting values for the mandatory properties.
    • There's no clear point of having both a GUID and an integer ID identifying the ItemList — the search condition is then just overspecified.
    • The commonly used coding standard states that property names shall begin with an upper case letter (posXPosX etc.).
    • The waypoint and position lists should be properties as well; having a getter only and backed with a readonly field.

    P.S. Happy new year once it comes into your timezone and a lot of good code in 2011 ;-)