locked
Class Design RRS feed

  • Question

  • User1091955703 posted

    I wanted to check if there is a better way of doing this.

    In my object classes, I have several properties for storing Id values that ultimately get readfrom/inserted into the database. For example, my Employees class has a property for SpecializationId. This value gets set when the person selects from a databound dropdown list containing all the Specializations. When we view the record, we just set the selected value of that dropdown list to this Id value. No problems.

    Now we have to implement a search feature and results grid. We thought about populating a collection of matching Employees, and binding the collection to the grid with the properties from the Employees class. However, the grid needs to show the actual "Name" of the Specialization (Chemical), not the database Id (4)

    So my question is, for each of the grid cells that have to show the actual name instead of the Id, do I have to add an additional property to my class?

    // existing
    public int SpecializationId {...}

    // new addition?
    public string Specialization {...}

    This binds to the grid ok and shows the name, but when I have to transform 10+ Id type properties to Names, it seems like it is just bloating the class for just this single purpose. My gut tells me there is a better way of doing this?



    Friday, January 8, 2010 12:50 PM

Answers

  • User854688209 posted

    As you have mentioned you need ID and Name both.

    You need to create both, there is no other go due to simple fact that there needs to be property in the class to hold it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2010 2:38 PM
  • User1753744165 posted

    It will bind. Just have to qualify it as Specialization.Name.

     

    Nick 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2010 2:47 PM

All replies

  • User1753744165 posted

     

    Normally I would put both fields into the Class. But if your field names are static and no new ones are added, you could use an enum for the values.

    public enum Specializations : int {

    Chemical = 4,

    Mechanical = 5,

    }

    Then in your code, you can just use the value (4) and it will be understood to be Chemical. You can also convert back to the int value from the name with Enum.Parse(typeof(Specializations), "Chemical", true).

     

    Nick

    Friday, January 8, 2010 2:30 PM
  • User854688209 posted

    As you have mentioned you need ID and Name both.

    You need to create both, there is no other go due to simple fact that there needs to be property in the class to hold it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2010 2:38 PM
  • User1091955703 posted

    True. Unfortunately the lists are subject to change (new ones added, some deleted), so the enum probably won't work.

    I am going to try setting up a new class with two properties

    public class ValueList
    {
        public string Name {... }

        public int DatabaseId { ... }
    }

    Then in the Employee class I should be able to set up a property:

    public ValueList Specalization { ... }

    Depending on what I want to use, I can call Employee.Specialization.Name or Employee.Specialization.DatabaseId

    But, I don't think this field going to bind very well without some intervention. Thanks for the suggestion and I'll continue to try some things.

    Friday, January 8, 2010 2:44 PM
  • User1753744165 posted

    It will bind. Just have to qualify it as Specialization.Name.

     

    Nick 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2010 2:47 PM
  • User1091955703 posted

    As I suspected, it fails if you create a BoundField and try to set the DataField = "Specialization.Name"


    I had to follow the approach here using Template Fields:

    http://www.apterasoftware.com/Blog/Post/09-12-02/Declarative_Databinding_of_Nested_Object_Properties_to_GridView_Columns.aspx


    Seems to work ok thus far. I will probably go this route unless I see a better way. Thanks for all the help.


    Friday, January 8, 2010 4:01 PM