Modelling class relations
I have a few classes:
Article
-------
Content
ID
Magazine
--------
Name
Code
And 3 tables in database:
Articles
Magazines
ArticlesInMagazines (two fields: IDArti开发者_StackOverflow中文版cle and CodeMagazine)
In my app, I've got a module to manage Articles, and a datagridview to relate their associated magazines
DataGridView has twofields:
MagazineCode
IsPublished (indicates articles which have been published in this magazine).
The same article can be in many magazines (1:n)
How would you implement on model ?
Article have to has a field : List ?? I am concerned because Magazine associates Articles
I am not entirely sure about your question, however, if you might need accessign Magazines in which a single article was published more than accessing articles in one magazine, then you might reverse the association. If that is not the case, you might use two associations, which will use much disk space and some work during creation and update of Articles or Magazines. The other way is to compute the other association on demand, which might include iteration over the whole set of Articles or Magazines, which might be reasonable e.g. when there are a few Magazines. However, this can be enhanced using memoization or caching. There is much written in literature about handling 1:n and m:n associations, the solution depends on your concrete setting.
精彩评论