Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could supply the connection string as a constructor argument when binding the <code>SqlProductRepository</code> to the <code>IProductRepository</code> interface.</p> <pre><code>public class LinqToSqlModule : NinjectModule { public override void Load() { Bind&lt;IProductRepository&gt;().To&lt;SqlProductRepository&gt;() .WithConstructorArgument(connectionString, "connectionstring"); } } </code></pre> <p>I would suggest a slightly different approach. First of all, you might want to create a binding for the <code>DataContext</code> class in the kernel. You could do so by using a provider class to create your <code>DataContext</code> passing the connection string as an argument to its constructor. Then you bind the <code>DataContext</code> to the <code>DataContextProvider</code>.</p> <pre><code>public class DataContextProvider : Provider&lt;DataContext&gt; { protected override DataContext CreateInstance(IContext context) { string connectionString = "connectionstring"; return new DataContext(connectionString); } } public class LinqToSqlModule : NinjectModule { public override void Load() { Bind&lt;DataContext&gt;().ToProvider&lt;DataContextProvider&gt;(); Bind&lt;IProductRepository&gt;().To&lt;SqlProductRepository&gt;(); } } </code></pre> <p>Next modify the constructor of <code>SqlProductRepository</code> class to accept a <code>DataContext</code> object instead.</p> <pre><code>public class SqlProductRepository : IProductRepository { private readonly DataContext context; public ProductRepository(DataContext context) { this.context = context; } public IQueryable&lt;Product&gt; Products { get { return context.GetTable&lt;Product&gt;(); } } } </code></pre> <p>By the way you don't have to decorate your constructor with the <code>Inject</code> attribute. Ninject will select the constructor with the most parameters by default.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload