简体   繁体   English

在c#中,是否可以将两个不同的类作为返回类型从一层传递到另一层?

[英]In c#, Is it possible to pass two different classes as return type from one layer to other layer?

My application has n-tier architecture. 我的应用程序具有n层体系结构。 I have different layers(Business Logic & Data Link & GUI). 我有不同的层(业务逻辑和数据链接和GUI)。 I am using some common classes to pass data from one layer to the other. 我正在使用一些通用类将数据从一层传递到另一层。 I have a class(say RetrnValueCls ) which has just two variables Return value and Return Message . 我有一个类(比如RetrnValueCls ),它只有两个变量Return valueReturn Message While I am passing data from one layer to the other, I need to return this Return Value Class along with the other class(say MasterItemsCls ) which has other variables. 当我将数据从一层传递到另一层时,我需要返回此Return Value类以及具有其他变量的另一类(例如MasterItemsCls )。

Below are the methods 以下是方法

public MasterItemsCls GetMasterItemsMDL()
{
    /* Does some computations and assign them to
    attributes of MasterItemsCls and pass it other methods in other layers. */
}

public ReturnValueCls GetMasterItemsMDL()
{
    /* Does some computations and assign them to
    attributes of ReturnValueCls and pass it other methods in other layers. */
}

I want to return both the above classes( MasterItemsCls & ReturnValueCls ) as return type for the method GetMasterItemsMDL at once and also I don't want to combine both the classes. 我想立即返回上述两个类( MasterItemsClsReturnValueCls )作为方法GetMasterItemsMDL返回类型,而且我也不想合并这两个类。 Please let me know if there is any way I can do that. 请让我知道我是否有办法。

A function can only return a single value, so in that sense the answer is no. 一个函数只能返回一个值,因此从这个意义上讲,答案是否定的。

Functions can have out parameters, so you could return one of the two values via an out parameter. 函数可以具有out参数,因此您可以通过out参数返回两个值之一。

The cleanest way to handle this in my opinion is to use a third class to contain the other two. 在我看来,处理此问题的最干净方法是使用第三类来包含其他两个类。 Tuple<T1,T2> can be used for that purpose: Tuple<T1,T2>可以用于此目的:

public Tuple<MasterItemsCls, ReturnValueCls> MyFunction()
{
    // Do stuff
    return new 
    Tuple<MasterItemsCls, ReturnValueCls>(myMasterItemsCls, myReturnValueCls);
}

One disadvantage of Tuple<T1,T2> is that the values are accessed as the rather unintuitive Item1 and Item2 . Tuple<T1,T2>一个缺点是,将值作为不太直观的Item1Item2进行访问。

If you don't want to use Tuple<T1,T2> , it is easy to create your own class to contain MasterItemsCls and ReturnValueCls . 如果您不想使用Tuple<T1,T2> ,则可以轻松创建自己的类以包含MasterItemsClsReturnValueCls That is my preferred approach. 那是我的首选方法。

You don't need to combine both classes but a possible and direct approach is to generate a class that contains one instance of each class you're trying to return. 您不需要合并两个类,但是可能的直接方法是生成一个包含要返回的每个类的一个实例的类。

public class YouClassName
{
     public MasterItemsCls MasterItemsCls {get; set;}
     public ReturnValueCls ReturnValueCls {get; set;}
}

A method can only return one object. 一个方法只能返回一个对象。 You could possibly create some other class that has instances of each of these others. 您可能会创建其他一些类,这些类具有其他每个类的实例。

Generics may be helpful as a wrapper for your return element. 泛型作为您的return元素的包装可能会有所帮助。

You could do the following: 您可以执行以下操作:

public ReturnValueCls<MasterItemsCls> GetMasterItemsMDL()
{

}

Your ReturnValueCls<T> expects a type parameter for its return type. 您的ReturnValueCls<T>需要一个类型参数作为其返回类型。 So your retun type can would act as a wrapper for your desired return and be able to hold your return message. 因此,您的retun类型可以充当所需退货的包装,并能够保留您的退货消息。


Example of your class definition. 类定义的示例。 May require some constraints: 可能需要一些约束:

public class ReturnValueCls<T>
{
    public T Value { et; set;}
    public string Message {get; set;}
}   

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

相关问题 C#:将数据从 Controller 传递到应用层到 DBContext 层 - C#: Pass Data from Controller to Application Layer to DBContext Layer 如何在C#中将Linq查询结果从BLL层返回到UI层 - How to return Linq query result from BLL layer to UI layer in C# 如何将对象类型从业务层传递到数据层 - How to pass the object type from business layer to data layer 如何用c#制作两个透明层? - How to make two transparent layer with c#? 将信息从一层传递到另一层 - Pass information from one layer to another 如何在同一个类和其他类的不同对象之间传递信息,作为C#中的事件 - How to pass information between different objects from the same class and other classes as events in C# C# 可以覆盖派生类的返回类型吗? - C# Possible to override a return type on derived classes? 从C#中的db层调用存储过程 - calling sprocs from a db layer in c# 在ac#Webservice中使用与返回类型同名的不同类 - Using different classes with the same name as a return type in a c# webservice Linq查询的返回类型从数据访问层连接两个表 - Return type for Linq query Joining two tables from a Data Access Layer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM