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):
Web Service (Entry Point)
namespace Web.Services
{
public bool AddForm(XmlDocument form, string formName)
{
IKernel kernel = new StandardKernel(new FormsModule());
var ctx = kernel.Get<IPFormDataContext>(formName);
return ctx.DoWork(form);
}
}
Ninject Related Things
namespace Core.Modules
{
public class FormsModule : NinjectModule
{
public override void Load()
{
Bind<IPFormDataContext>().ToSelf().Named("FormA");
Bind<IPFormDataContext>().ToSelf().Named("FormB");
// Snip
Bind<IPFormDataStrategy>().To<FormAStratgey>()
.WhenParentNamed("FormA");
Bind<IPFormDataStrategy>().To<FormBStrategy>()
.WhenParentNamed("FormB");
// Snip
}
}
}
Pattern Related Things
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;
}
}
}
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.
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 bad thing. 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.
Is what I'm doing ... wrong? Could I get pointed in the right direction?
IPFormDataContext<T> where T : IPFormDataStrategy
to clean it up? I'm not really that familiar with ninject. – Cisneros