Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I have a question about class design. Currently I am working with following tables:

=======================
| Forms
=======================
| FormID (PK)
| CommitmentID (FK)
| ...
---------------------------

=====================
| Commitments
=====================
| CommitmentID (PK)
| CommitDesc
| ...
---------------------------

=====================
| Pricings
=====================
| PricingID (PK)
| Desc
| CommitmentID (FK)
| ...
---------------------------

=====================
| PricingTiers
=====================
| TierID (PK)
| PricingID (FK)
| Desc
| ...
---------------------------

MAPPING:
Forms <-one to one-> Commitment
Commitment <-one to many-> Pricing
Pricing <-one to many-> PricingTiers

When designing the classes what would be considered a best practice ... for instance at the moment I have 4 classes "Form", "Commitment", "Pricing", and "PricingTier" respectively.

Form class has a public property Commitment which represents an instance of the Commitment class.

Commitment class has a public property List<Pricing> which represents the one to many (Commitment - Pricing) relationship.

Also, Pricing class has a public property List<PricingTier> which represents the one to many (Pricing - PricingTier).

After designing these classes the very first problem that I noticed was that I can't use a PK to access the objects instead I will have to loop over the objects to find the one I am looking for (this is because I have exposed one to many relatioship using List<T>).

OR access these objects using indexes:
Forms frms = Forms(55);
PricingTiers pt = frms.Commitment.Pricing[45].PricingTiers[0];


Ideally I would like to be able to access "Pricing" or "PricingTier" objects using the Primary Key (PricingID or PricingTierID).

I thought about exposing these properties using generic Dictionary<int, Pricing> where the PricingID would be the Key. Would that be a good idea?

Do you guys have any suggestions? or can any of you point me in the right direction?

Thanks!
Posted
Comments
TheyCallMeMrJames 7-Oct-10 15:12pm    
Have you thought about using an ORM, LINQ to SQL or the Entity Framework to take care of all of this (and more) for you already? What version of the .NET framework are you on?
Khaniya 9-Oct-10 3:00am    
If there is one to one mapping between
"MAPPING:
Forms <-one to one-> Commitment"

then why are you not using single table ?

1 solution

XML
u can use delegate on the List<T> to find the required object using  properties corresponding to Pk<br />
consider, we have a class named Product and it has properties named Id,CategoryId, Name,Price ,etc.<br />
suppose you Got Data Form Some Source using following code:
List<Product> lstProduct=GetProducts();<br />
now u have list of products in the list lstProducts.
Then to get a product with CategoryId=1 and Id=7  , u can use delegate as shown below<br />
Product product=lstProducts.Find(
                                   delegate(Product p)
                                  {
                                    return p.CategoryId==1 && p.Id==7 ;
                                  }
                                 );



Hope You Got the easy solution
 
Share this answer
 
Comments
shanjeev 13-Oct-10 2:08am    
well done. keep it up :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900