如何创建一个返回具有不同参数的不同实例的工厂?
我将 ASP.NET MVC 与 Ninject 结合使用,并尝试创建一个 ActionResult 工厂。假设我有以下 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) { ... }
}
因此对于每个 ActionResult
,参数类型和参数数量都会不同。我创建了一个 ActionResultFactory
,如下所示:
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?
}
}
我将如何编写工厂,以便它可以接受参数并将它们传递给对象的构造函数?或者我应该这样做:
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";
每个属性都公开公开,并在对象创建后分配它们?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建 3 为每种类型创建一个方法,传递所需的参数并创建实例,如下所示。
Create 3 Create methods one for each type, pass the required arguments and create the instance like this.
您需要带有参数的构造函数注入。
请参阅下面的链接
带有参数的Ninject
You need a Constructor injection with parameters.
Please see below link
Ninject with parameters
您可以创建一个小提供程序来为您新建并初始化 ActionResult 对象。此处解释:
请参阅 使用 Ninject 创建实例构造函数中的附加参数
You could create a little provider to new up and initialize the ActionResult objects for you. Explained here:
See Creating an instance using Ninject with additional parameters in the constructor