How do I connect my database tables to my website pages?

0
14
Asked By CuriousCoder42 On

I'm building my first website and I've hit a bit of a snag. I have a database filled with tables designed for easy data analysis, but they don't directly correspond to the pages on my site. It seems like someone has to bridge the gap between the database layout and the webpage layout, and I'm trying to figure out how that works.

Right now, I have two sets of models: half are for what each page needs from the database tables, and the other half represents the data needed for the pages. The controller is using the first set to pull data from the database and initialize the second set. This setup is causing all of the components (Context.cs, controller, and the model files) to be aware of both the database and page layouts, which feels messy. I'm wondering, what is the best way to handle the translation between the database and the web pages?

3 Answers

Answered By HelpfulHank35 On

I've gone through the same process, and you might need a bit more structure moving from database rows to creating individual pages. If you're feeling stuck, I’m here to guide you through the process. Feel free to reach out and we can go over it together!

Answered By TechieTina21 On

You're definitely not alone in feeling this way! What you're experiencing is a common issue, and the concept you're looking for is called 'separation of concerns.' Basically, the structures for your database and your pages don't have to match up perfectly. Normally, database models stay close to where the data is held, while page-specific models (or view models) exist separately.

The translation usually happens in the controller or a dedicated service layer, not in the models themselves. So, models representing your database tables shouldn't be aware of page needs, and vice versa. By implementing a middle layer for this mapping, everything will have a clearer structure and won't leak into one another. You're on the right track, just focus on adding that layer!

Answered By DevDude88 On

In typical setups, the controller or a service layer is responsible for translating data from your database models to the page-specific view models. This helps keep your views clean and focused, allowing you to manage data well without mixing concerns.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.