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

  • SEARCH
  • Home
  • 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 1070417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:33:51+00:00 2026-05-16T20:33:51+00:00

If I had the code below what are the best practise / design considersations

  • 0

If I had the code below what are the best practise / design considersations for replacing it with IoC (we are looking to use Castle Windsor). As the “using” statement is responsible for creating the connection object, you can’t inject that directly into the constructor or the method. Note: Using SQL connection as what appears to be a clean example, the main benefit here is mocking/unit testing

public void CustomerRespository
{
    ....
    public void Save(Customer customer)
    {
       using (var cn = new SqlConnection(connectionString))
       {
           using (var cm = new SqlCommand(commandString, cn))
           {
              ....
              cn.Open();
              cm.ExecuteNonQuery();
           }
       }
    }
}

I believe there would be at least a few options, but as we’re just starting out with IoC I’m not confident that they wouldn’t cause us problems later on and/or fly in the face of IoC concepts. My favoured approach would be too modify the approach as follows, can anyone highlight potential issues with it?

public interface IDatabase
{
    IDbConnection Connection(string connectionString);
    IDbCommand Command(string text, IDbConnection conn);
}

public class SqlDB : IDatabase
{
    IDbConnection Connection(string connectionString)
    { return new SqlConnection(connectionString); }

    IDbCommand Command(string text, IDbConnection conn)
    { return new SqlCommand(text, conn); }
}

public interface ICustomerRespository
{
   void Save(Customer customer)
}

public class CustomerRespository : ICustomerRespository
{
    public IDatabase DB{get; private set;}

    public CustomerRespository( IDatabase db)
    {
       DB = db;
    }

    ....
    public void Save(Customer customer)
    {
       using (var cn = DB.Connection(connectionString))
       {
           using (var cm = DB.Command(commandString, cn))
           {
              ....
              cn.Open();
              cm.ExecuteNonQuery();
           }
       }
    }
}
  • 1 1 Answer
  • 2 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-05-16T20:33:51+00:00Added an answer on May 16, 2026 at 8:33 pm

    I have used IoC but not Castle although they are all similar so here is my catch on this.

    I think you are on the right track – although I might use a separate factory for connection and command or in fact leave the connection opening and command running to another class so repository does not have to know this detail. Just have IDatabase in the constructor of your class so that it is injected (or use property if you are using property-based injection). Replace SqlConnection and SqlCommand in your code with IDbConnection and IDbCommand.

    UPDATE

    They do inherit/implement IDisposable so you CAN use using statement. Sorry for my mistake.

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

Sidebar

Related Questions

How to Remove Badge from the tabbar item i had use below code but
I had an access to modified closure error in the code below foreach (var
I was wondering if anyone had an idea as to why the code below
I had a weird problem here. Please check the code below. I do not
I had used the below code to get the row index of the UIPickerView
Given below is program for encrypting a string. I had taken this code from
Answer solved in edit below I had this piece of code Dictionary<Merchant, int> remaingCards
In old mysql code, I had a query below which worked perfectly which is
I am using vb.net. I am having below code in vb.net <tr> <td >
I had a doubt which is the following: When I use the following code:

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.