Design Question (Class vs Structs)
-
Hi Everyone, I have a "one to many" sort of situation. I am using a Key to get subset data. I started out using a class like this:
public class Contract { public int ContractID {get;set;} public int CustomerID {get;set;} public string CustomerName {get;set;} public string Organization {get;set;} public int TermsID {get;} } //Then data mapper public class ContractDataMapper { public static List<Contract> GetContractData(int contractID) { List<Contract> returnValue = new List<Contract>(); ... /*data retrieval logic*/ while(reader.Read()) { Contract contract = new Contract(); contract.CustomerID = Convert.ToInt32(reader["CustomerID"]); contract.CustomerName = reader["CustomerName"].ToString(); ... if(!returnValue.Contains(contract)) returnValue.Add(contract); } reader.Close(); command.Dispose(); return returnValue(); } }
But I soon realized that this is bad because I had to check to see if this ContractID has any TermsID associated with it and the way it currently is I would have to do something like:
if(ContractDataMapper.GetContractData(1234)[0].TermsID > 0) // Do something
Ideally I should have one class Contract and have other properties in a subclass. Now my question is ... do you think it would be better to have a struct or a subclass or something else?
// Something like this Contract contract = new Contract(1234); List<SubsetData> subset = contract.SubsetData;
I would appreciate your help.
-
Hi Everyone, I have a "one to many" sort of situation. I am using a Key to get subset data. I started out using a class like this:
public class Contract { public int ContractID {get;set;} public int CustomerID {get;set;} public string CustomerName {get;set;} public string Organization {get;set;} public int TermsID {get;} } //Then data mapper public class ContractDataMapper { public static List<Contract> GetContractData(int contractID) { List<Contract> returnValue = new List<Contract>(); ... /*data retrieval logic*/ while(reader.Read()) { Contract contract = new Contract(); contract.CustomerID = Convert.ToInt32(reader["CustomerID"]); contract.CustomerName = reader["CustomerName"].ToString(); ... if(!returnValue.Contains(contract)) returnValue.Add(contract); } reader.Close(); command.Dispose(); return returnValue(); } }
But I soon realized that this is bad because I had to check to see if this ContractID has any TermsID associated with it and the way it currently is I would have to do something like:
if(ContractDataMapper.GetContractData(1234)[0].TermsID > 0) // Do something
Ideally I should have one class Contract and have other properties in a subclass. Now my question is ... do you think it would be better to have a struct or a subclass or something else?
// Something like this Contract contract = new Contract(1234); List<SubsetData> subset = contract.SubsetData;
I would appreciate your help.
Probably a class.