[英]method returning same object which was passed as parameter
Is it acceptable practice to pass an object into a method, then return the same object rather than creating a new object inside of the method itself? 将对象传递给方法然后返回相同的对象而不是在方法本身内部创建新对象是否可以接受?
As an example: if have an entity class as follows: 举个例子:如果有一个实体类如下:
class UserDetails {
int UserID { get; set; }
string UserName { get; set; }
string UserAge { get; set; }
}
And then I pass an instance of this class to a method, as follows: 然后我将此类的实例传递给方法,如下所示:
UserDetails UserInfo = new UserDetails();
UserInfo = Get_Details(UserInfo);
Is it reasonable for the method to do the following? 该方法执行以下操作是否合理?
public UserDetails Get_Details(UserDetails user) {
// SQL Operations...
user.age = 32;
return user;
}
IMO, there is no need to return the object . IMO, 没有必要返回对象 。 Since it is passed to the method by reference , the caller already has a reference to the same object (with the updated values after the method completes).
由于它 通过引用 传递给方法,因此调用者已经具有对同一对象的引用(在方法完成后具有更新的值)。
On the other hand, what can be useful in some situations is a fluent-interface, where instance-methods of a class return the instance again, eg: 另一方面,在某些情况下可能有用的是一个流畅的接口,其中类的实例方法再次返回实例,例如:
class X
{
public X DoThis(int number)
{
// do something
return this;
}
public X DoThat(string name)
{
// do something else
return this;
}
}
This allows to write very readable code, such as: 这允许编写非常易读的代码,例如:
var x = new X().DoThis(23).DoThat("asdf");
This can be useful with the builder pattern (when you want to build a complex object step by step). 这对于构建器模式非常有用(当您希望逐步构建复杂对象时)。
As a very bad example: 作为一个非常糟糕的例子:
class FooBuilder {
FooBuilder WithAge(int age);
FooBuilder WithUrl(Url url);
Foo ToFoo();
}
new FooBuilder().WithAge(12).WithUrl(new Url("http://www.happybirthday.com/").ToFoo();
In your particular case, I'd prefer to initialize everything in one go with the initializer syntax. 在您的特定情况下,我更倾向于使用初始化语法一次性初始化所有内容。
new User { Age = 45, UserName = "Bob", Id = 101 };
There is nothing horribly wrong with this but a couple of observations; 这有什么可怕的错误,但有几点意见;
get
perhaps load
is more appropriate. get
的方法的详细信息,也许load
更合适。 UserDetails
because you want the id for your then the parameter should just be id
instead. UserDetails
因为你想要你的id,那么参数应该只是id
。 This keeps the interface cohesive. This is a possible approach and when you have only ONE item to work one, the best, too. 这是一种可能的方法,当你只有一个项目可以工作时,也是最好的。 You might also consider to use
ref
, which creates a reference to the passed parameter 您可能还会考虑使用
ref
,它会创建对传递参数的引用
public void Get_Details(ref UserDetails user)
{
// SQL Operations. . .
user.age= 32;
}
this way, you don't pass a copy, but reference the object you passed in. But this can become quite obscure and is unnecessary in your case. 这样,您不会传递副本,而是引用您传入的对象。但这可能会变得非常模糊,在您的情况下是不必要的。 See here for an insight.
请参阅此处以获取洞察力。
You might do well to look up the concepts of the Repository Pattern and OOD. 您可能会查找存储库模式和OOD的概念。 In general, I prefer projections or fully loaded entities.
一般来说,我更喜欢投影或完全加载的实体。
public UserDetailsProjection GetDetailsByUserId(Guid userID)
{
// Code goes here
return user;
}
Note: ref is not required, because all objects are passed by reference. 注意:ref不是必需的,因为所有对象都是通过引用传递的。
Doing it like that is rather pointless, as the assignment that you do doesn't change anything. 这样做是没有意义的,因为你做的任务不会改变任何东西。
Calling it like this: 这样称呼它:
UserInfo = Get_Details(UserInfo);
gives the same result as calling it and ignoring the return value: 给出与调用它并忽略返回值相同的结果:
Get_Details(UserInfo);
Returning the reference may only be confusing, leading someone to believe that the method returns a new instance, as that would be the only logical reason to return a reference. 返回引用可能只会令人困惑,导致某人认为该方法返回一个新实例,因为这将是返回引用的唯一逻辑原因。
It would make more sense to have that method in the class, so that you call it as: 在类中使用该方法会更有意义,因此您将其称为:
UserInfo.Get_Details();
If your method is supposed to initialise the object, you would rather put the code it the constructor than calling it after creating the instance: 如果你的方法应该初始化对象,你宁愿把代码放在构造函数中,而不是在创建实例后调用它:
class UserDetails {
int UserID { get; set; }
string UserName { get; set; }
string UserAge { get; set; }
public UserDetails() {
Get_Details(this);
}
}
Then you just create the instance, and the constructor loads the data: 然后你只需创建实例,构造函数加载数据:
UserDetails UserInfo = new UserDetails();
You can fill your entity in its constructor method or another method inside entity class. 您可以在构造函数方法或实体类中的其他方法中填充实体。 It will be ready to use when created.
它将在创建时使用。
public class SomeClass
{
public string Field_1;
public int Field_2;
public SomeClass(int ID)
{
// Sql operations by ID or another value
// set fields
}
public AnotherMethod(int ID)
{
// Sql operations by ID or another value
// set fields
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.