这可能是一个非常愚蠢的问题,但是;
如果我正在创建数据集,请说,
[WebMethod(Description = "Returns a Fruit XML")]
public XmlElement GetALLFruits()
{
Fruit fruit = new Fruit();
fruit.Name = "Mango La Ka Buma";
fruit.expiryDate = "11/11/1911";
getFruitCrateDetails(fruit);
return fruit.xml; //just giving example syntax might be wrong
}
public void getFruitCrateDetails(Fruit fruit)
{
FruitCrate crate = new FruitCrate();
crate.id = 999;
Fruit.Crate.add(crate);
// Now here do I need to return "Fruit" object or not ?
}
我有10或20种方法,我应该把它们放在方法中还是用一种大方法结合起来。
答案 0 :(得分:1)
[WebMethod(Description = "Returns a Fruit XML")]
public XmlElement GetALLFruits()
{
Fruit fruit = new Fruit();
fruit.Name = "Mango La Ka Buma";
fruit.expiryDate = "11/11/1911";
fruit = getFruitCrateDetails(fruit);//This is call to method 1. Don't worry your values will not be lost fruit.Name will remain as it is.
return fruit.xml; //just giving example syntax might be wrong
}
public Fruit getFruitCrateDetails(Fruit fruit)
{
FruitCrate crate = new FruitCrate();
crate.id = 999;
fruit.crate.Add(crate);//Now no other method should set crateValues
return fruit;
}
public Fruit getFruitCrateDetails1(Fruit fruit)
{
SomeNewProperty = "test";
fruit.PropertyName = SomeNewProperty;//Now no other method should set value for SomeNewProperty
return fruit;
}
请阅读评论。我还没有测试过代码。因此,您可能无法获得所需的输出。我尽力向你解释。
答案 1 :(得分:0)
这通常是一种风格选择,但大多数开发人员对大型方法不满意。
通过划分方法,你可以免费获得这些东西;
您最终得到的是一个干净的模块化系统,其中包含一小块逻辑,可以阅读和理解,而不会让文字墙混乱。
此外,以“get”为前缀的方法应返回一个值。如果没有,请考虑将其命名为“添加”。这是另一种风格,但如果您在团队中工作,则有助于将您的方法名称与方法签名相匹配。