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 778345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:44:57+00:00 2026-05-14T19:44:57+00:00

I’ve literally just started using the Unity Application Blocks Dependency Injection library from Microsoft,

  • 0

I’ve literally just started using the Unity Application Blocks Dependency Injection library from Microsoft, and I’ve come unstuck.

This is my IoC class that’ll handle the instantiation of my concrete classes to their interface types (so I don’t have to keep called Resolve on the IoC container each time I want a repository in my controller):

public class IoC
{
    public static void Intialise(UnityConfigurationSection section, string connectionString)
    {
        _connectionString = connectionString;
        _container = new UnityContainer();
        section.Configure(_container);
    }

    private static IUnityContainer _container;
    private static string _connectionString;

    public static IMovementRepository MovementRepository
    {
        get { return _container.Resolve<IMovementRepository>(); }
    }
}

So, the idea is that from my Controller, I can just do the following:

_repository = IoC.MovementRepository;

I am currently getting the error:

Exception is:
InvalidOperationException – The type
String cannot be constructed. You must
configure the container to supply this
value.

Now, I’m assuming this is because my mapped concrete implementation requires a single string parameter for its constructor. The concrete class is as follows:

public sealed class MovementRepository : Repository, IMovementRepository
{
    public MovementRepository(string connectionString) : base(connectionString) { }
}

Which inherits from:

public abstract class Repository
{
    public Repository(string connectionString)
    {
        _connectionString = connectionString;
    }

    public virtual string ConnectionString
    {
        get { return _connectionString; }
    }
    private readonly string _connectionString;
}

Now, am I doing this the correct way? Should I not have a constructor in my concrete implementation of a loosely coupled type? I.e. should I remove the constructor and just make the ConnectionString property a Get/Set so I can do the following:

public static IMovementRepository MovementRepository
{
   get
   {
      return _container.Resolve<IMovementRepository>(
         new ParameterOverrides
         {
            { 
               "ConnectionString", _connectionString 
            }
         }.OnType<IMovementRepository>() );
   }
}

So, I basically wish to know how to get my connection string to my concrete type in the correct way that matches the IoC rules and keeps my Controller and concrete repositories loosely coupled so I can easily change the DataSource at a later date.

EDIT 09:52:

Just to re-iterate what I’m after. I want to know the correct way to pass the ConnectionString or an IRepositoryConfiguration object (prefer that idea, thanks Mc) to a concrete class from Unity. I’m not too fussed on what I pass, just how I pass it whilst maintaining loose coupling.

  • 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-05-14T19:44:57+00:00Added an answer on May 14, 2026 at 7:44 pm

    Probably the most straight forward way to do this is to set up a constructor section for your mapped type in the unity configuration and inside the constructor section have a parameter element for the connection string that passes in a name value for a connection string you have defined in the connectionStrings section of your web configuration.

    Inside your constructor code for the Repository class, have some code that uses the name value of the connection string to get the full connection string from the connectionStrings section.

    EDIT:

    Here’s an example for you using Unity 2.0

    In your web.config, specify the connection string and a mapping for unity to map an IRepository<T> to a SqlRepository<T>. Based on your other questions, we’ll assume that IRepository<T> is in your model project and SqlRepository<T> is in your DAL project.

    <?xml version="1.0"?>
    <configuration>
        <configSections>
            <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
        </configSections>
        <connectionStrings>
            <add name="SqlConnection" connectionString="data source=(local)\SQLEXPRESS;Integrated Security= SSPI; Initial Catalog= DatabaseName;" providerName="System.Data.SqlClient"/>
        </connectionStrings>
        <unity>
            <containers>
                <container>
                    <types>
                        <type type="ModelProject.IRepository`1, ModelProject" mapTo="DALProject.SqlRepository`1, DALProject">
                            <constructor>
                                <param name="connectionString">
                                    <value value="SqlConnection" />
                                </param>
                            </constructor>
                        </type>
                    </types>
                </container>
            </containers>
      </unity>
    </configuration>
    

    Now for the IRepository<T> interface in the model project. In this example, I’m also going to be using LINQ to SQL to return objects from the SQL Database

    namespace ModelProject
    {
        /// <summary>
        /// Interface implemented by a Repository to return
        /// <see cref="IQueryable`T"/> collections of objects
        /// </summary>
        /// <typeparam name="T">Object type to return</typeparam>
        public interface IRepository<T>
        {
            IQueryable<T> Items { get; }
        }
    }
    

    And the SQLRepository<T> class in the DAL project

    namespace DALProject
    {
        /// <summary>
        /// Generic class for returning an <see cref="IQueryable`T"/>
        /// collection of types
        /// </summary>
        /// <typeparam name="T">object type</typeparam>
        public class SqlRepository<T> : IRepository<T> where T : class
        {
            private Table<T> _table;
    
            public SqlRepository(string connectionString)
            {
                // use the connectionString argument value to get the
                // connection string from the <connectionStrings> section
                // in web.config
                string connection = ConfigurationManager.ConnectionStrings[connectionString].ConnectionString;
    
                _table = (new DataContext(connection)).GetTable<T>();
            }
    
            /// <summary>
            /// Gets an <see cref="IQueryable`T"/> collection of objects
            /// </summary>
            public IQueryable<T> Items
            {
                get { return _table; }
            }
        }
    }
    

    Let’s also use a custom controller factory to allow unity to return controllers for us. This way, unity will inject any dependencies that the controllers have

    In global.asax

    namespace WebApplicationProject
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                // your routes
            }
    
            protected void Application_Start()
            {
                RegisterRoutes(RouteTable.Routes);
                ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory());
            }
        }
    
        public class UnityControllerFactory : DefaultControllerFactory
        {
            private IUnityContainer _container;
    
            public UnityControllerFactory()
            {
                _container = new UnityContainer();
    
                var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                      where typeof(IController).IsAssignableFrom(t)
                                      select t;
    
                foreach (Type t in controllerTypes)
                    _container.RegisterType(t, t.FullName);
    
                UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
    
                section.Configure(_container);
            }
    
            protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
            {
                // see http://stackoverflow.com/questions/1357485/asp-net-mvc2-preview-1-are-there-any-breaking-changes/1601706#1601706
                if (controllerType == null) { return null; }
    
                return (IController)_container.Resolve(controllerType);
            }
        }
    
    }
    

    And Here’s a controller example. PageSize might be defined on a base controller or on the controller as a property.

    namespace WebApplicationProject.Controllers
    {
        public class CustomersController : Controller
        {
            private IRepository<Customer> _customerRepository;
            public int PageSize { get; set; }
    
            public CustomersController() { }
    
            public CustomersController(IRepository<Customer> customerRepository)
            {
                this._customerRepository = customerRepository;
                // let's set it to 10 items per page.
                this.PageSize = 10; 
            }
    
            public ViewResult List(string customerType, int page)
            {
                var customerByType = (customerType == null) ?
                    customerRepository.Items : customerRepository.Items.Where(x => x.CustomerType == customerType);
    
                int totalCustomers = customerByType.Count();
                ViewData["TotalPages"] = (int)Math.Ceiling((double)totalCustomers/ PageSize);
                ViewData["CurrentPage"] = page;
                ViewData["CustomerType"] = customerType;
    
                // get the right customers from the collection
                // based on page number and customer type.    
                return View(customerByType
                    .Skip((page - 1) * PageSize)
                    .Take(PageSize)
                    .ToList()
                );
            }
    
        }
    }
    

    When the customers list controller action is invoked, unity will correctly instantiate an instance of SqlRepository<Customer> for the controller and inject this into the constructor. the connectionString string used for SqlRepository<T> is set in the unity configuration and is passed into the constructor for a typed SqlRepository<T>.

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

Sidebar

Ask A Question

Stats

  • Questions 529k
  • Answers 529k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer f.addToList and f.displayList do not invoke the methods addToList and… May 16, 2026 at 11:11 pm
  • Editorial Team
    Editorial Team added an answer Target settings override Project settings. Project settings are valid for… May 16, 2026 at 11:11 pm
  • Editorial Team
    Editorial Team added an answer Use base_64 encoding with a private key. http://php.net/manual/en/function.base64-encode.php Check the… May 16, 2026 at 11:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.