简体   繁体   English

从方法返回接口类型的正确方法是什么?

[英]What is the correct way to return an Interface Type from a Method?

Ok say I have the following simple classes: 好吧,我有以下简单的课程:

public interface IResult<T>
{
    T Results { get; set;}
    string Message { get; set;}
}

public class Result<T> : IResult<T>
{
    T Results { get; set;}
    string Message { get; set;}
}

public class Result2<T> : IResult<T>
{
    T Results { get; set;}
    string Message { get; set;}
}

Now when I want to call some method to return a IResult, which way is correct? 现在,当我想调用某个方法以返回IResult时,哪种方法正确?

public class TestClass
{
    public bool TryDoSomething(out IResult<bool> result)
    {
        // Do stuff
        result.Message = "Out!";
        return true; // for example
    }

    public IResult<bool> DoSomething(IResult<bool> result)
    {
        // Do Stuff
        result.Message = "Sent in!";
        result.Result = true;
        return result;
    }

    public IResult<bool> DoSomething()
    {
        // Do Stuff
        IResult<bool> result = new Result<bool>();
        result.Message = "I'm a Result!";
        result.Result = true;
        return result;
    }
}

Now say I actually want a Result2, is it better to create it and send it in as the parameter (methods #1 or #2) or create a new Result but recast it as IResult and then to Result2 as in method #3? 现在说我实际上想要一个Result2,是更好地创建它并将其作为参数(方法1或#2)发送或创建一个新的Result但将其重铸为IResult然后像方法3一样重铸到Result2吗?

public class GetResults
{
    public void GetResults()
    {
        Result2<bool> results = new Result2<bool>();
        if (TryDoSomething(out results))
        {
            Debug.WriteLine(results.Message);
        }
        results = DoSomething(results);
        if (results.Result) Debug.WriteLine(results.Message);
        results = DoSomething();
        if (results.Result) Debug.WriteLine(results.Message);
    }
}

So is it better to create my interface implementation in the Method or send it in as a parameter? 那么在Method中创建接口实现或将其作为参数发送更好吗?

You should only use out parameters as a last resort - unless you really want to effectively return two values from the same method, and you've got a good reason not to encapsulate them together (or use Tuple ), I definitely wouldn't suggest the first approach. 你应该只使用out参数作为最后的手段-除非你真的想有效地从同样的方法返回两个值,并且你已经有了一个很好的理由不来封装在一起(或使用Tuple ),我绝对不会建议第一种方法。 (What's the bool meant to be for?) bool是什么意思?)

If the aim is to manipulate an existing object, then the second method is appropriate - but if the aim is to create a result (which is more common, IMO) then the third approach is the most appropriate one. 如果目的是操纵现有对象,则第二种方法是合适的;但是,如果目的是创建结果(IMO更常见),则第三种方法是最合适的方法。

It's hard to give concrete advice when we've got so little to go on, but personally I favour immutable types where practical, which rules out the second method anyway. 当我们做得很少时,很难给出具体的建议,但是我个人更倾向于在可行的情况下使用不可变类型,这仍然排除了第二种方法。

In terms of your question about "I actually want a Result2 " - what wants a Result2 ? 就您有关“我实际上想要一个Result2 ”的问题而言,是什么想要Result2 The calling code, or the method returning the result? 调用代码还是返回结果的方法? Usually if you're creating a result then it's more appropriate for the "creating" code to know which implementation it wants to use. 通常,如果您要创建结果,则“创建”代码更了解要使用的实现更为合适。

Following on from your comment to Jon's answer, the caller can specify what type is desired without creating the object, assuming the class has a public parameterless constructor: 在您对Jon的回答发表评论之后,假设类具有公共的无参数构造函数,调用者可以在不创建对象的情况下指定所需的类型:

public TResult DoSomething<TResult, U>() where TResult : IResult<U>, new()
{
    // Do Stuff     
    TResult result = new TResult();     
    result.Message = "I'm a Result!";     
    result.Result = true;     
    return result;     
}

usage: 用法:

Result<bool> result1 = DoSomething<Result<bool>, bool>();
Result2<bool> result2 = DoSomething<Result2<bool>, bool>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM