1

Hi my site has the following database structure:

Sections

  • Id (PK, Identity)
  • Name

Documents

  • Id (PK, Identity)
  • SectionId (FK to Sections)

Articles

  • Id (PK, FK to Documents)
  • Title
  • Content

Links

  • Id (PK, FK to Doucments)
  • Title
  • SomeOtherField

MetaComponent

  • Id (PK, FK to Documents)
  • MetaKeywords
  • MetaDescription

SectionComponents

  • SectionId (FK to Sections)
  • ComponentName

The site contains multiple sections (Articles and Links in the above structure). The Articles and Links tables extend the Document with the extra fields that only apply for that particular section. Each section can optionally have additional Components added to them. The MetaComponent table is an example of one particular Component which includes the search engine meta information.

I was wondering if it's possible to map my application so i can query all Articles and join it to get the meta Information for that particular article. I know i could add a Meta property against my Article class but i want to be able to easily switch on and off which components apply to particular sections. I guess i would have to do something a little fancier to retrieve this information and would appreciate it if someone could help.

I hope i've explained things clear enough.

Thanks

nfplee
  • 7,643
  • 12
  • 63
  • 124

1 Answers1

0

firstly- one of the biggest advantages of nHib (or any ORM, for that matter) is that it enables you to view your app domain as entities rather than tables.
So in your case, I think it would be more correct, and more understandable, to look at your Document entity as being a base-class, of which Link and Article are sub-classes.
And when we look at it like that, the solution to your problem is obvious- mapping of subclasses:
* fluent nHib docs for mapping inheritance,
* more detailed info can be found in the nHib docs.

J. Ed
  • 6,692
  • 4
  • 39
  • 55
  • Hi thanks that's the easy bit. The real problem is how do i get access the component information? – nfplee Jun 20 '11 at 08:08
  • if I understand you correctly (if not- please explain), that's easy too: since an Article *is a* Document, it has all of Document's properties (including Section, MetaComponent etc.) – J. Ed Jun 20 '11 at 10:47
  • Not quite, the MetaComponent only applies to certain sections (defined in the SectionComponents table). I guess these properties would need to be mapped at runtime but i also need to be able to query against them. – nfplee Jun 20 '11 at 12:00
  • I think it would be much clearer if you posted the code to your actual *classes*, and then we could see how to query them (since in nHib you query against your entities, and not against tables) – J. Ed Jun 20 '11 at 13:33
  • See http://stackoverflow.com/questions/6581589/map-one-to-one-relationship-at-run-time for further information which may help. – nfplee Aug 11 '11 at 21:57