结合构造,我有以下代码:我可以在C#
public AccountService(ModelStateDictionary modelStateDictionary, string dataSourceID)
{
this._modelState = modelStateDictionary;
this._accountRepository = StorageHelper.GetTable<Account>(dataSourceID);
this._productRepository = StorageHelper.GetTable<Product>(dataSourceID);
}
public AccountService(string dataSourceID)
{
this._accountRepository = StorageHelper.GetTable<Account>(dataSourceID);
this._productRepository = StorageHelper.GetTable<Product>(dataSourceID);
}
有一些办法,我可以简化构造所以每个不必做StorageHelper电话?
我也需要指定这个。 ?
只是为了确认。我可以删除哪些“这个”这个词? –
不完全清楚你要问什么,但如果你询问'this._'代码,你可以删除所有这些代码。例如,'this._accountRepository'也可以写成'_accountRepository'。我所指的'this'与构造函数声明位于同一行(滚动到右边看它)。 –
@RichardM:几乎所有的人。除非你传入一个具有相同名字的变量,否则''this.'几乎总是可以在上下文中被推断出来。有些人喜欢它,引用可读性(更明确)。其他人也出于同样的原因不喜欢它(冗余信息)。 –