2010-10-14 30 views
0

我有班的国家有城市集合。我有很好的静态法的感觉

在客户端我用的WebMethod

[WebMethod] 
public void AddCity(string countryCode,string name) 
{ 
MyFacade.AddCity(countryCode,name); 
} 

在门面我有方法

public void AddCity(string countryCode,string name) 
{ 
Country.AddCity(countryCode,name); <-in this method is simple sql operation 
} 

和我的问题的核心:

public class Country 
{ 
public static void AddCity(string countryCode, string cityName) 
{ 
//insert into table cities new city 
} 
} 

这没关系?或者我必须创建objectCountry,并且有非静态方法AddCity?

而另一个问题:

更好地利用:

City[] cities= Country.GetAllCities(countryCode) 

City[] cities= new Country(countryCode).GetAllCities() 

回答

1

同时接受countryCodecityName作为参数是罚款,数据访问层,但我不看到任何应该是静态的方法。

相反AddCity应该是DataConnection或其他一些非静态成员,以便您可以轻松地嘲笑它,替换数据库等,而无需更改调用接口。

+0

我认为无论是'MyFacade.AddCity'和'Country.AddCity'是静态的。 – 2010-10-14 16:57:41

+0

是的,Facade是一个静态类,MyFacade.both MyFacade.AddCity和Country.AddCity是静态的。 – user278618 2010-10-14 17:02:13

+1

这就是我不同意的。这不是'Country'类型需要有状态和多态,而是数据库连接。 – 2010-10-14 17:13:05

1

你想使用模拟框架来测试你的代码吗?

大厦本的答案,同一个接口更换门面:

[WebMethod] 
public void AddCity(string countryCode, string name) 
{ 
    ICountryDataAccess dao = GetDAOFromDI(); // basically get a DI framework to manage this object instance. 
    dao.AddCity(countryCode, name); 
} 

public interface ICountryDataAccess 
{ 
    void AddCity(string countryCode, string name); 
    ICollection<City> GetAllCities(string countryCode); 
    // OR ! 
    Country Retrieve(string countryCode); 
    // using an ORM or something Country then as a list of cities 
} 

public Country 
{ 
    public virtual string CountryCode {get;set;} 
    public virtual ICollection<City> Cities {get; protected set;} 
} 
相关问题