I'm using ASP.NET MVC
with Ninject
and I'm trying to create an ActionResult
factory. Let's say I have the following ActionResults
:
public class SuccessResult : ActionResult
{
public string SuccessMessage { get; set; }
public SuccessResult(string successMessage) { ... }
}
public class FailResult : ActionResult
{
public int FailCode { get; set; }
public FailResult(int failCode) { ... }
}
public class DataResult : ActionResult
{
public object Data { get; set; }
public string MimeType { get; set; }
public DataResult(object dataToSerialize, string mimeType) { ... }
}
So for each ActionResult
, the parameter types and number of parameters will be different. I created an ActionResultFactory
that looks like this:
public class ActionResultFactory
{
private readonly IKernel _kernel;
public ActionResultFactory(IKernel kernel)
{
_kernel = kernel;
}
public T Create<T>() where T : ActionResult
{
return _kernel.Get<T>(); // how do I pass the parameters?
}
}
How would I write the factory so that it can take parameters and pass them to the object's constructor? Or should I do it like this instead:
var result = factory.Create<SuccessResult>();
result.SuccessMessage = "Success!";
var result = factory.Create<FailResult>();
result.FailCode = 404;
var result = factory.Create<DataResult>();
result.Data = file;
result.MimeType = "text/plain";
where each property is publically exposed and I assign them after object creation?