[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Class Design and Generics

I'm getting a little confused on class design and wanted to input.

Is it a good practice to have the methods that work with the class, in the class instead of another class.
For example List()

public class StoreItem : Product
{
    private int _itemID;

    public int ItemID
    {
        get { return _itemID; }
        set { _itemID = value; }
    }

    public StoreItem()
    {

    }

    public StoreItem(int itemID, string description)
    {
        _itemID = itemID;
        base.Description = description;
    }

    public List<StoreItem> List()
    {
        List<StoreItem> list = new List<StoreItem>();
        //I am hard coding values for testing, in a real world example
        //I would make a call to the database and return a datareader
        //and populate the values from the reader
        list.Add(new StoreItem(1, "test item 1"));
        return list;
    }

}

public class StoreProvider
{
    public static List<StoreItem> DisplayProducts()
    {
        StoreItem i = new StoreItem();
        return i.List();
    }
}


Or another way I just thought of...

public class StoreItem : Product
{
    private int _itemID;

    public int ItemID
    {
        get { return _itemID; }
        set { _itemID = value; }
    }

    public StoreItem()
    {

    }

    public StoreItem(int itemID, string description)
    {
        _itemID = itemID;
        base.Description = description;
    }

    public List<StoreItem> List(IDataReader reader)
    {
        List<StoreItem> list = new List<StoreItem>();
        while (Reader.Reader()
        // read the data from the datareader that was passed in
        //instead of calling the DAL from here
        list.Add(new StoreItem((int)dr["id"], dr["desc"].ToString()));
        return list;
    }

}

//talk to the DAL in this class
//and then pass the IDataReader
public class StoreProvider
{
    public static List<StoreItem> DisplayProducts()
    {
        StoreItem i = new StoreItem();
      //DAL code goes here
        return i.List(dr);
    }
}


Or am I just way off and need to think about something else? haha
0
JRockFL
Asked:
JRockFL
  • 3
  • 2
1 Solution
 
JimBrandleyCommented:
JRockFL - Yes, it is best to have a class contain the data, behavior and business rules surrounding the object that class represents. However, it is unusual to have a class that deals with a list or collection of the same object. For example, a car class would know things about a car, but not a parking lot full of cars.

Usually, a collection of a specific class lives in a collection class. It knows how to add items, remove items, find items, enumerate or iterate over the items, and construct its content based on specific criteria. For example, load the list from some datasource, say a DB, of all red cars, or all sport cars.

Jim
0
 
JRockFLAuthor Commented:
Hey Jim,
Thanks for your help I again. The car and parking lot makes sense. Could you show me a small example of what you mean with the code I posted? That might help me more.
0
 
JimBrandleyCommented:
This is like a shopping cart, so let's call it the Cart class. Then here's a quick look at what it might contain:
      public class Cart
      {
            private List<StoreItem> mList = null;

            public int ItemCount
            {
                  get
                  {
                        return mList.Count;
                  }
            }
            public Cart()
            {
                  mList = new List<StoreItem>(100);
            }
            public StoreItem FindProcuct(int itemID)
            {
                  // search for this product - return it or null if not in list
            }
            public void AddProduct(StoreItem item)
            {
                  // Search for this item. If there, increment the count. If not add it
            }
            public void RemoveProduct(StoreItem item)
            {
                  // Search for this item. If there, delete it
            }
            public StoreItem GetProcuctAt(int index)
            {
                  if (index < mList.Count)
                        return mList[index];
                  else
                        return null; // or throw exception.
            }
            public List<StoreItem> GetBackorderedItems()
            {
                  //Create a new list and populate with all on backorder.
            }
      }
0
 
JRockFLAuthor Commented:
Great example. Thank you.
0
 
JimBrandleyCommented:
My pleasure. Good luck.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now