0

if i say have code that works like this:

    private static void LoadFromAssemblies(IKernel kernel)
    {
        string appPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);

        kernel.Scan(a =>
        {
            a.FromAssembliesInPath(string.Format(@"{0}\Extensions", appPath));
            a.AutoLoadModules();
            a.BindWithDefaultConventions();
            a.InRequestScope();
        });
    }

and just assume that each class defined in the target assembly has a string argument in the constructor, how would i go about passing in the string argument from the code above?

Do i instead use an Interceptor?

Thanks in advance, John

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • Passing in *what* "string argument", from where, to where? – Aaronaught Aug 14 '11 at 23:37
  • For future reference, it's best practice to use `Path.Combine` instead of `string.Format(@"{0}\{1}")`. The way you used is susceptible to typos and might not work on Mono ;). – Squirrelsama Apr 09 '12 at 23:28

1 Answers1

0

In my project into some repositories I pass ISession (nHibernate) and to others connectionString for DataContext(Linq2SQL)

To pass the connection string I have created LinqConfiguration class

public class LinqConfiguration : ILinqConfiguration
{
    private readonly string _connectionString;

    public LinqConfiguration(string connectionString)
    {
        _connectionString = connectionString;
    }

    public string GetConnectionString()
    {
        return _connectionString;
    }
}

My repository looks like this:

   public class WebClientRepository : IWebClientRepository
    {
        private readonly WebClientDataClassesDataContext datacontext;
        private ILinqConfiguration _linqconfig;


        public WebClientRepository(ILinqConfiguration linqconfig)
        {

                _linqconfig = linqconfig;
                datacontext = new WebClientDataClassesDataContext(_linqconfig.GetConnectionString());
        }

    //....
    }

and binding using Conventions:

public class LinqRepositoryModule: NinjectModule
    {
        public override void Load()
        {
            Bind<ILinqConfiguration>()
            .To<LinqConfiguration>()
            .WithConstructorArgument("connectionString",
          ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString
            );

            IKernel ninjectKernel = this.Kernel;

            ninjectKernel.Scan(kernel =>
            {
                kernel.FromAssemblyContaining<IWebClientRepository>();
                kernel.FromAssemblyContaining<WebClientRepository>();
                kernel.Where(t => t != typeof(LinqConfiguration)); // interface is in the same assembly and it is already binded
                kernel.BindWithDefaultConventions();
                kernel.AutoLoadModules();
                kernel.InRequestScope();
            });

        }
    }
Artur Kedzior
  • 3,994
  • 1
  • 36
  • 58