开发者

How to implement primary and foreign key in class structure?

开发者 https://www.devze.com 2022-12-10 08:50 出处:网络
here\'s what I want to achieve.I have a collection (A..n) with a member of type collection (myLIST). In c开发者_开发问答ollection B, I have an \"Alias\" field which we can represent as primary key.

here's what I want to achieve. I have a collection (A..n) with a member of type collection (myLIST). In c开发者_开发问答ollection B, I have an "Alias" field which we can represent as primary key.

example

A collection myLIST[0].Alias = 001
A collection myLIST[1].Alias = 002
A collection myLIST[2].Alias = 003
A collection myLIST[3].Alias = 001

In another collection B. my member type collection (myLIST) has the following data

B collection myLIST[0].Alias = 010
B collection myLIST[1].Alias = 001
B collection myLIST[2].Alias = 005

I want that my myLIST has a reference (as class reference) to other collections (A..n) myLIST with the same Alias key. if i change any public property value of A collection myLIST[0].Alias = 001 the changes will propagate to other's myLIST collectin with the same Alias of "001" (as reference). with my sample above, the changes to the public property will propagate to

A collection myLIST[3].Alias = 001
B collection myLIST[1].Alias = 001

can any one help me with this?

Thanks


Keys are a hack.

A relational database must use keys because of the restrictions placed on the locations of rows within a table. It can't use direct references because no such reference can possibly exist in a proper relational database.

An object can contain a direct reference to another object without resorting to keys or key lookup or any such foolishness.


It's not clear to me what the relationship is between your objects. +1 to identify the language you're using. Instead of thinking in terms of collections, think of how objects are related. For example:

  1. If each A is associated with 0...n B's, then A has a collection of B's, and each B has a direct reference to an A (B has a many-to-one relationship to A).
  2. If an A can be associated with 0...n B's, and a B can be associated with 0...m A's, then A has a collection of B's, and B has a collection of A's. This is a many-to-many relationship between A and B. In this case, you may need to create a joining entity, AB, to represent the many-to-many relationship.

Do not store multiple copies of the same A, or look things up using aliases, if you can straighten out your object graph to use direct references. Depending on the lanuage you are using, and your data source, you may find a tool like Hibernate or LINQ to be helpful.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号