I've got a philosophical programming problem. Let's say I have a class named Employees. Employees has business members that get set from a dataTable. In order to fill this, I use a method that takes an instance of the employee class, loops through a dataTable, and sets the members of the instance passed into it. For instance:
我有一个哲学编程问题。假设我有一个名为Employees的类。员工拥有从dataTable设置的业务成员。为了填补这一点,我使用了一个方法,该方法接受雇员类的实例,循环访问dataTable,并设置传递给它的实例的成员。例如:
public void GetEmployees(objEmployee)
{
//the function I am calling returns a dataTable of all the employees in the db.
dim dt as DataTable = dbEmployees.GetEmployees();
foreach(DataRow drow in dt.rows)
{
objEmployee.Name = drow["Name"].ToString();
objEmployee.ID = drow["ID"].ToString();
}
}
Then I would call the code like this in my UI logic:
然后我会在我的UI逻辑中调用这样的代码:
public void GetEmployees()
{
Employees employee = new Employees();
employee.GetEmployees(employee);
}
My question is, is it acceptable to pass in my class instance into a method and change the properties like I am doing, or would it be more object-oriented to do it through a function like this:
我的问题是,将我的类实例传递给一个方法并改变我正在做的属性是否可以接受,或者通过这样的函数来实现它是否更加面向对象:
public Employees GetEmployees()
{
Employees objEmployee = new Employees();
//the function I am calling returns a dataTable of all the employees in the db.
dim dt as DataTable = dbEmployees.GetEmployees();
foreach(DataRow drow in dt.rows)
{
objEmployee.Name = drow["Name"].ToString();
objEmployee.ID = drow["ID"].ToString();
}
return objEmployee
}
And then I would call it like this:
然后我会这样称呼它:
private void GetEmployees()
{
Employees employee;
employee = employee.GetEmployees();
}
Is there any advantage of using a function over a method? Thanks!
使用函数而不是方法有什么优势吗?谢谢!
3 个解决方案
#1
Both things are methods (also known as functions). The difference is that the first one "returns by reference" while the second one "returns a reference".
两者都是方法(也称为函数)。不同之处在于第一个“按引用返回”,而第二个“返回引用”。
There is no advantage in returning by reference in C# because in the simpler, natural case where you merely return a reference, no copying is done (unlike in C++).
在C#中通过引用返回没有任何优势,因为在更简单,自然的情况下,您只返回一个引用,不进行复制(与C ++不同)。
Returning a reference is, thus, to be always preferred as it's the easiest, and it allows great syntactic flexibility at the call site (such as nesting expressions: manager.Fire(GetEmployee()) without the need for a separate statement).
因此,返回引用始终是首选,因为它是最简单的,并且它允许在调用站点具有很强的语法灵活性(例如嵌套表达式:manager.Fire(GetEmployee()),而不需要单独的语句)。
#2
Sounds to me like you ought to make GetEmployees() a static method off of Employee. You shouldn't have to instantiate an employee to get a list of them.
听起来像你应该让GetEmployees()成为一个关于Employee的静态方法。您不应该实例化员工以获取他们的列表。
Also, your first example is only going to set your objEmployee to whatever comes up last in your data. While it loops through all the employees, it stops looping when it reaches the last one, which is the data you'll get returned.
此外,您的第一个示例只是将您的objEmployee设置为数据中最后出现的内容。当它遍历所有员工时,它会在到达最后一个时停止循环,这是您将返回的数据。
Also, does the "Employees" class refer to one employee or to many? Name it accordingly. If "Employees" represents one "Employee" then perhaps you should rename it to "Employee" and return a List from the GetEmployees method, which, as I stated above, ought to be static, so you can simply call something like "Employee.GetEmployees()".
此外,“员工”类是指一名员工还是多名员工?相应地命名。如果“Employees”代表一个“Employee”,那么你可能应该将它重命名为“Employee”并从GetEmployees方法返回一个List,正如我上面所说的那样,它应该是静态的,所以你可以简单地调用类似“Employee”的东西。装getEmployees()”。
That being said, I'm never too fond of the architecture where you provide data access capabilities to your business object. It tends to couple the data access layer and the business object layer too tightly.
话虽这么说,我从不太喜欢为业务对象提供数据访问功能的架构。它往往过于紧密地耦合数据访问层和业务对象层。
You may want to consider creating a Data Access interface that accepts parameters for searching for employees and returns actual Employee objects as it's result. Once you do that, you would want to create an implementation of this Data Access Layer that you would then use to generate the actual instances. The advantage to this would be that you could quickly change your implementation of the Data Access Layer without having to change the business objects as well. You would program your business objects off of the Interface then, and you might be able to use dynamic assembly loading or some other method to dynamically determine the implementation of your data access.
您可能需要考虑创建一个数据访问接口,该接口接受用于搜索员工的参数,并返回实际的Employee对象作为结果。完成后,您可能希望创建此数据访问层的实现,然后使用该实现来生成实际实例。这样做的好处是您可以快速更改数据访问层的实现,而无需更改业务对象。您可以将业务对象编程为接口,然后您可以使用动态程序集加载或其他方法来动态确定数据访问的实现。
#3
public void GetEmployees(objEmployee)
{
//the function I am calling returns a dataTable of all the employees in the db.
dim dt as DataTable = dbEmployees.GetEmployees();
foreach(DataRow drow in dt.rows)
{
objEmployee.Name = drow["Name"].ToString();
objEmployee.ID = drow["ID"].ToString();
}
}
In your first approach what would you do if there is no Employees? You created Employe object and pass the method to fill it and then you want to check if there are employees. But when you want to check there will be never null value because you sent created object.I think second one is better and more understanable.
在您的第一个方法中,如果没有员工,您会怎么做?您创建了Employe对象并传递方法以填充它,然后您想检查是否有员工。但是当你想要检查时,因为你发送了创建的对象,所以永远不会有空值。我认为第二个更好,更难以理解。
Employees employee = new Employees();
employee.GetEmployees(employee);
if(employee==null)//but employee is not null??
DoSomething();
#1
Both things are methods (also known as functions). The difference is that the first one "returns by reference" while the second one "returns a reference".
两者都是方法(也称为函数)。不同之处在于第一个“按引用返回”,而第二个“返回引用”。
There is no advantage in returning by reference in C# because in the simpler, natural case where you merely return a reference, no copying is done (unlike in C++).
在C#中通过引用返回没有任何优势,因为在更简单,自然的情况下,您只返回一个引用,不进行复制(与C ++不同)。
Returning a reference is, thus, to be always preferred as it's the easiest, and it allows great syntactic flexibility at the call site (such as nesting expressions: manager.Fire(GetEmployee()) without the need for a separate statement).
因此,返回引用始终是首选,因为它是最简单的,并且它允许在调用站点具有很强的语法灵活性(例如嵌套表达式:manager.Fire(GetEmployee()),而不需要单独的语句)。
#2
Sounds to me like you ought to make GetEmployees() a static method off of Employee. You shouldn't have to instantiate an employee to get a list of them.
听起来像你应该让GetEmployees()成为一个关于Employee的静态方法。您不应该实例化员工以获取他们的列表。
Also, your first example is only going to set your objEmployee to whatever comes up last in your data. While it loops through all the employees, it stops looping when it reaches the last one, which is the data you'll get returned.
此外,您的第一个示例只是将您的objEmployee设置为数据中最后出现的内容。当它遍历所有员工时,它会在到达最后一个时停止循环,这是您将返回的数据。
Also, does the "Employees" class refer to one employee or to many? Name it accordingly. If "Employees" represents one "Employee" then perhaps you should rename it to "Employee" and return a List from the GetEmployees method, which, as I stated above, ought to be static, so you can simply call something like "Employee.GetEmployees()".
此外,“员工”类是指一名员工还是多名员工?相应地命名。如果“Employees”代表一个“Employee”,那么你可能应该将它重命名为“Employee”并从GetEmployees方法返回一个List,正如我上面所说的那样,它应该是静态的,所以你可以简单地调用类似“Employee”的东西。装getEmployees()”。
That being said, I'm never too fond of the architecture where you provide data access capabilities to your business object. It tends to couple the data access layer and the business object layer too tightly.
话虽这么说,我从不太喜欢为业务对象提供数据访问功能的架构。它往往过于紧密地耦合数据访问层和业务对象层。
You may want to consider creating a Data Access interface that accepts parameters for searching for employees and returns actual Employee objects as it's result. Once you do that, you would want to create an implementation of this Data Access Layer that you would then use to generate the actual instances. The advantage to this would be that you could quickly change your implementation of the Data Access Layer without having to change the business objects as well. You would program your business objects off of the Interface then, and you might be able to use dynamic assembly loading or some other method to dynamically determine the implementation of your data access.
您可能需要考虑创建一个数据访问接口,该接口接受用于搜索员工的参数,并返回实际的Employee对象作为结果。完成后,您可能希望创建此数据访问层的实现,然后使用该实现来生成实际实例。这样做的好处是您可以快速更改数据访问层的实现,而无需更改业务对象。您可以将业务对象编程为接口,然后您可以使用动态程序集加载或其他方法来动态确定数据访问的实现。
#3
public void GetEmployees(objEmployee)
{
//the function I am calling returns a dataTable of all the employees in the db.
dim dt as DataTable = dbEmployees.GetEmployees();
foreach(DataRow drow in dt.rows)
{
objEmployee.Name = drow["Name"].ToString();
objEmployee.ID = drow["ID"].ToString();
}
}
In your first approach what would you do if there is no Employees? You created Employe object and pass the method to fill it and then you want to check if there are employees. But when you want to check there will be never null value because you sent created object.I think second one is better and more understanable.
在您的第一个方法中,如果没有员工,您会怎么做?您创建了Employe对象并传递方法以填充它,然后您想检查是否有员工。但是当你想要检查时,因为你发送了创建的对象,所以永远不会有空值。我认为第二个更好,更难以理解。
Employees employee = new Employees();
employee.GetEmployees(employee);
if(employee==null)//but employee is not null??
DoSomething();