Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:15:49+00:00 2026-06-15T11:15:49+00:00

My controllers need to get data that is passed back to the view. Where

  • 0

My controllers need to get data that is passed back to the view. Where should I open a connection to pass to the repository? Here is an example. This is a part of one of my controllers.

using (var connection = this.GetActiveConnection())
{
    var repository = new RefRepository(connection);
    var codes = repository.GetPoACodes();    
}

Is it bad practice to be opening a connection in the controller? If I don’t pass it via the controller, where should I pass the connection to the repository?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:15:50+00:00Added an answer on June 15, 2026 at 11:15 am

    Actually Repositories must handle connection themselves, it shouldn’t be controller concern.
    Controller class must remain thin, and if it gets fat, it would be a code smell.

    It would a very good practice if you can use a Dependency Injection Framework (like Ninject, StructureMap, …) to wire those dependencies , to handle DbContext or Session and SessionFactory (EF or NHibernate), Transaction or namely Unit of Work pattern, Exception Handling and Logging if you want to go thus far.

    If you’re using Visual Studio then in Project Templates there is an option which create repositories also, you can create a sample project, read the code, and learn how the code is organized.

    In this article, under Web API header, you can find out how to do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple view controllers that need to get the user's location, so I
I am uploading files using SWFUpload. To get the data I need passed along
I need to load a HTML wrapper dynamically depending on data that gets populated
Here is my situation: I have a search page that pulls data from a
On my main edit view I have 3 partial views that contain child data
I need some help here. I am using razor view in MVC3. I have
Inside Controller I need to get a rendered string and do some actions with
Just need get some vals located in application.ini(main ini) in the Controller plugin I
I need to get(retrieve) updated cell value in controller. (MVC) So I tried this,
I need to get an instance of the application's MembershipProvider from within a controller.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.