[英]Is it bad practice to only create static methods that take instances of that type
Take this class for example: 以此类为例:
public class Account
{
public string Code { get; set; }
public string Description { get; set; }
public DateTime CreatedOn { get; private set; }
public void Create()
{
// Invoke with new Account({...}).Create();
}
//Or
public static void Create(Account account)
{
// Invoke with Account.Create(new Account({...}));
}
}
Both Create()
methods will both do the same thing, but as you can see they are invoked differently. 这两个
Create()
方法都将执行相同的操作,但是如您所见,它们的调用方式有所不同。 Is one better practice over the other? 一种方法比另一种更好吗? Is there a term for writing code like this?
有写这样的代码的术语吗?
In this case I reccomend you don't use a static method, because you should create a lot of accounts and every account will have its own different properties. 在这种情况下,我建议您不要使用静态方法,因为您应该创建很多帐户,并且每个帐户都有其自己的不同属性。 But I think that the method
Create()
has no sense, beacuse you can use directly the constructor to set the account. 但是我认为
Create()
方法没有意义,因为您可以直接使用构造函数来设置帐户。 So here is how I would do: 所以这是我会怎么做:
public class Account
{
public string Code { get; set; }
public string Description { get; set; }
public DateTime CreatedOn { get; private set; }
public Account()
{
Description = string.Empty;
CreatedOn = DateTime.Now;
//Code = ...
}
public Account(string Description)
{
this.Description = Description;
CreatedOn = DateTime.Now;
//Code = ...
}
}
Ant then I would create another class to manage the accounts: Ant,然后我将创建另一个类来管理帐户:
class AccountsManagement
{
public List<Account> Accounts
{
get;
set;
}
public AccountsManagement()
{
Accounts = new List<Account>();
}
//...
public void Create()
{
Accounts.Add(new Account();
}
public void Create(string Description)
{
Accounts.Add(new Account(Description);
}
//Or
public void AddAccount(Account account)
{
Accounts.Add(account);
}
//Find(), Delete()...
}
So continuing the speech it isn't bad practice use static methods, but they should be used only when opportune. 因此,继续演讲不是坏习惯,而是使用静态方法,但只有在适当时才使用它们。
In general, I don't know that this is either good or bad practice. 总的来说,我不知道这是好事还是坏事。 However, I would lean towards 'bad practice' in the example you have given (and I can't off hand think of any useful reasons to pass an instance of a type to a static method declared on that type).
但是,在您给出的示例中,我倾向于“不好的做法”(而且我无法想到将任何类型的实例传递给在该类型上声明的静态方法的任何有用的理由)。
Certainly in your example, you are (IMO) creating an unclear API. 当然,在您的示例中,您(IMO)正在创建一个不清楚的API。 I'm not sure whether;
我不确定是否;
Another consideration is testing - static methods can introduce difficulties when unit testing anything that calls them, as they may not be easy to mock or stub. 另一个考虑因素是测试-静态方法在对任何调用它们的内容进行单元测试时可能会带来困难,因为它们可能不容易模拟或存根。
Apart from those, there is nothing inherently 'wrong' about this, technically. 除了这些,从技术上讲,这没有天生的“错误”。 It affects your design in other ways though (eg; no access to instance members as it is not an instance).
但是,它以其他方式影响您的设计(例如,由于它不是实例,因此无法访问实例成员)。
So, I think this approach can lead to some confusion, however, it might also be entirely appropriate for your situation! 因此,我认为这种方法可能会引起一些混乱,但是,它也可能完全适合您的情况!
If both the static and instance versions of the method will do the same thing then I would definitely use the instance method version. 如果该方法的静态版本和实例版本都将执行相同的操作,那么我肯定会使用实例方法版本。 For a few reasons:
有几个原因:
Account.Create(accountObj);
Account.Create(accountObj);
or just accountObj.Create();
accountObj.Create();
? accountObj.
accountObj.
and get the drop down menu to see what methods it uses. Account.Create()
method. Account.Create()
方法。 The only reason, in my humble opinion, to use the static version over the instance is if you need keep the Create()
method from having access to private members of the account object. 以我的拙见,在实例上使用静态版本的唯一原因是,您是否需要使
Create()
方法不能访问帐户对象的私有成员。
The only reason i can think of to use such static methods as your Create, is the cases where you make ur default constructor private, and wish to control the creation of instances only via ur helper method. 我可以想到使用诸如Create之类的静态方法的唯一原因是,您将默认构造函数设为私有,并希望仅通过helper方法来控制实例的创建。
Such design is actually used by microsofts expression trees framework, where u create the instances via static methods of th expression class, however they also have a more complicated hierarchy of abstract classes in between 这种设计实际上是由Microsoft表达式树框架使用的,其中您是通过表达式类的静态方法创建实例的,但是它们之间还具有更复杂的抽象类层次结构
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.