Really newbie question approaching. It is possible to standard (or good) way to cope with not requiring all the information that the database table consists of loaded into every connected object. I am thinking poor webpages where you are only likely to make use of the objects to construct just one page instead of a credit card applicatoin with longer resided objects.
For instance, allows if you have articles table that contains id, title, author, date, summary and fullContents fields. You do not need the fullContents to become loaded in to the connected objects if you are just showing a webpage that contains a listing of articles using their summaries. However if you are exhibiting a particular article you may want every area loaded for your one article and perhaps only the game titles for that other articles (e.g. for display inside a recent articles sidebar).
Some techniques I'm able to think about:
- Don't be concerned about this, just load from the database each time.
- Have a number of different, possibly inherited, courses of instruction for each table and make the right one for that situation (e.g. SummaryArticle, FullArticle).
- Play one class but set unused qualities to null at creation in the event that area isn't needed and become careful.
- Provide the objects accessibility database to allow them to load some fields when needed.
- Another thing?
The suggestions above appear to possess fairly major disadvantages.
I am a newcomer to programming, very a new comer to OOP and completely new to databases and so i may be completely missing the apparent answer here. :)
(1) Loading the entire object is, regrettably what ORMs do, automatically. That's why hands updated SQL works better. But many objects have no need for this optimisation, and you will always delay optimisation until later. Don't optimize prematurely (but do write good SQL/HQL and employ good DB design with indexes). But generally, the ORM projects I have seen resultin lots of lazy approaches, tugging or upgrading a lot more data than needed.
2) Different Types (Organizations), based on operation. I favor that one. Will add more classes towards the object domain, but in my experience, is cleanest to cause better performance and security (particularly if you are serializing to AJAX). I sometimes play one model for serializing an item to some client, and the other for internal procedures. If you are using inheritance, this can be done well. For instance CustomerBase -> Customer. CustomerBase may have an ID, title and address. Customer can extend it to include other info, even things like passwords. For list procedures (list all clients) you are able to return CustomerBase having a custom query however for individual CRUD procedures (Create/Retrieve/Update/Remove), make use of the full Customer object. Even so, be cautious by what you serialize. Most frameworks have whitelists of characteristics they'll and will not serialize. Rely on them.
3) Harmful, special cases may cause bugs in your body.
4) Harmful to performance. Hit the database once, not for every area (Aside from BLOBs).
You have many techniques to resolve your problem.
- Use Saved Methods inside your database to get rid of the rows or posts you wouldn't want. This could work great but occupies some space.
- Make use of an ORM of some type. For .Internet you should use Entity Framework, NHibernate, or Subsonic. You will find a number of other ORM tools for .Internet. Ruby has it built-in with Rails. Java uses Hibernate.
- Write embedded queries inside your website. Be sure to parametrize them or else you will open yourself as much as cyber-terrorist. This method is generally frowned upon due to the interacting of SQL and code. Also, it's the simplest to interrupt.