如何创建一个返回具有不同参数的不同实例的工厂?

发布于 2024-10-04 01:49:39 字数 1391 浏览 9 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想你只要分分秒秒 2024-10-11 01:49:39

创建 3 为每种类型创建一个方法,传递所需的参数并创建实例,如下所示。

public FailResult CreateFailResult(int failcode)
{
    return _kernel.Get<FailResult>(new ConstructorArgument("failCode", failcode));
}

Create 3 Create methods one for each type, pass the required arguments and create the instance like this.

public FailResult CreateFailResult(int failcode)
{
    return _kernel.Get<FailResult>(new ConstructorArgument("failCode", failcode));
}
避讳 2024-10-11 01:49:39

您需要带有参数的构造函数注入。

请参阅下面的链接

带有参数的Ninject

You need a Constructor injection with parameters.

Please see below link

Ninject with parameters

薄荷梦 2024-10-11 01:49:39
public T Create<T>(params object[] parameters) 
{
    T instance = _kernel.Get<T>();
    if (typeof(T) == typeof(FailResult))
    {
        FailResult result = (FailResult)instance;
        result.ErrorCode = (int)parameters[0];
        return result;
    }
}
public T Create<T>(params object[] parameters) 
{
    T instance = _kernel.Get<T>();
    if (typeof(T) == typeof(FailResult))
    {
        FailResult result = (FailResult)instance;
        result.ErrorCode = (int)parameters[0];
        return result;
    }
}
乱了心跳 2024-10-11 01:49:39

您可以创建一个小提供程序来为您新建并初始化 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文