Note that there are some explanatory texts on larger screens.

plurals
  1. PONinject binding based on string
    primarykey
    data
    text
    <p>I have a web service that's going to do things with some data being passed in (specifically InfoPath xml from a SharePoint doc library). I'm currently using Ninject to handle what form data "strategy" to load. Here's some code (question follows):</p> <h1>Web Service (Entry Point)</h1> <pre><code>namespace Web.Services { public bool AddForm(XmlDocument form, string formName) { IKernel kernel = new StandardKernel(new FormsModule()); var ctx = kernel.Get&lt;IPFormDataContext&gt;(formName); return ctx.DoWork(form); } } </code></pre> <h1>Ninject Related Things</h1> <pre><code>namespace Core.Modules { public class FormsModule : NinjectModule { public override void Load() { Bind&lt;IPFormDataContext&gt;().ToSelf().Named("FormA"); Bind&lt;IPFormDataContext&gt;().ToSelf().Named("FormB"); // Snip Bind&lt;IPFormDataStrategy&gt;().To&lt;FormAStratgey&gt;() .WhenParentNamed("FormA"); Bind&lt;IPFormDataStrategy&gt;().To&lt;FormBStrategy&gt;() .WhenParentNamed("FormB"); // Snip } } } </code></pre> <h1>Pattern Related Things</h1> <pre><code>namespace Core.Forms { public class IPFormDataContext { private IPFormDataStrategy _ipFormDataStrategy; public IPFormDataContext(IPFormDataStrategy strategy) { _ipFormDataStrategy = strategy; } public bool DoWork(XmlDocument form) { return _ipFormDataStrategy.DoWork(form); } } public abstract class IPFormDataStrategy { public abstract bool DoWork(XmlDocument form); } } namespace Core.Forms.FormStrategies { class FormAStrategy : IPFormDataStrategy { public override bool DoWork(XmlDocument form) { // Deserialize form using (xsd.exe generated) FormAData // and perform some operation on the resulting data. return resultOfWork; } } } </code></pre> <p>FormBStrategy is much the same, as is the 7 other strategies I didn't list. I'm trying to find a way to pass in a form xml to the webservice and call the correct form deserialization based on the form type that's coming in.</p> <p>The code above "works"; but it feels like I'm doing some sort of service location in Ninject, which from what I'm reading is a <strong>bad thing</strong>. But I can't think of a proper way to accomplish this. I'm not dead set on using Ninject, or any IOC/DI framework for that matter.</p> <p>Is what I'm doing ... wrong? Could I get pointed in the right direction?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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