I am designing a project in asp.net 4.0, i am using MVC 3. I have table in my database (sql server ) . I have a table named “employee”. I want to display whole information whose name is SANJAY. So please suggest me how and where i should write query ? Means i should write query in controller class or model class..?
Share
Really you want to hide your actual data access from your MVC application entirely. So just like the MVC pattern means that the view only needs to know about the model and doesn’t care how it is constructed logically, your MVC application shouldn’t need to know about what queries are performed or what database they are performed against.
There are many design patterns that handle this separation of concerns, but you might want to start with the Repository Pattern. You can read about the pattern here: http://martinfowler.com/eaaCatalog/repository.html – or just search for “Repository Pattern”.
Ideally, your repository queries the database and returns a domain object. You would map the domain object to a much simpler Model in your MVC application. This not only protects your model from becoming bloated by changes to the domain object, it also means your model can contain things your domain object doesn’t contain and therefore stops the domain object from becoming polluted by things being added to the model.