2012-10-27 93 views
0

我已經創建了名爲ICountry調用通過類的靜態實例

public interface ICountry 
    { 
     List<CountryProperty> GetAllCountry(); 
     List< CountryProperty> GetCountryById(int countryid); 

    } 

然後我就創建了一個名爲作爲CountryProvider我在其中已經實施的方法的一個抽象類後的接口中的方法這樣

public abstract class CountryProvider:ICountry 
    { 

     public static CountryProvider Instance 
     { 
      get 
      { 
       return new Country(); 
      } 
     } 


     public abstract List<CountryProperty> GetAllCountry(); 


     public abstract List<CountryProperty> GetCountryById(int countryid); 

    } 

我已經創建了一個名爲作爲國家的一個簡單的類,並繼承類CountryProvider並重寫抽象方法再經過ICountry接口

public class Country: CountryProvider 
    { 
     private DbWrapper _objDataWrapper; 
     private DataSet _dataSet; 


     public override List<CountryProperty> GetAllCountry() 
     { _dataSet=new DataSet(); 
      _objDataWrapper = new DbWrapper(Common.CnnString, CommandType.StoredProcedure); 
      var objCountryList = new List<CountryProperty>(); 
      try 
      { 
      _dataSet=_objDataWrapper.ExecuteDataSet("Aj_Proc_GetCountryList"); 
       objCountryList = BindCountryObject(_dataSet.Tables[0]); 

      } 
      catch (Exception ex) 
      { 

      } 
      return objCountryList; 
     } 

     public override List<CountryProperty> GetCountryById(int countryid) 
     { 
      _dataSet = new DataSet(); 
      _objDataWrapper = new DbWrapper(Common.CnnString, CommandType.StoredProcedure); 
      var objCountryList = new List<CountryProperty>(); 
      try 
      { 
       _objDataWrapper.AddParameter("@CountryId", countryid); 
       _dataSet = _objDataWrapper.ExecuteDataSet("Aj_Proc_GetCountryList"); 
       objCountryList = BindCountryObject(_dataSet.Tables[0]); 

      } 
      catch (Exception ex) 
      { 
       var err = ex.Message; 

      } 
      return objCountryList; 
     } 

     private List <CountryProperty> BindCountryObject(DataTable dataTable) 
     { 
      if (dataTable == null) throw new ArgumentNullException("dataTable"); 
      var objCountryList = new List<CountryProperty>(); 


     try 
      { 
       if(dataTable.Rows.Count>0) 
       { 

        for(var j=0;j<dataTable.Rows.Count;j++) 
        { 
         var objCountry = new CountryProperty 
              { 
               CountryId = Convert.ToInt32(dataTable.Rows[j]["AjCountryId"]), 
               CountryCode = Convert.ToString(dataTable.Rows[j]["AjCountryCode"]), 
               CountryName = Convert.ToString(dataTable.Rows[j]["AjCountryName"]) 
              }; 
         objCountryList.Add(objCountry); 
        } 
       } 
      } 
      catch (Exception ex) 
      { 


      } 
      return objCountryList; 
     } 
    } 

我打電話這樣

var data = CountryProvider.Instance.GetAllCountry(); 

我的問題的方法,再經過是,它是緊的方式遵守或不遵守

+0

你想跨應用程序的國家的單個對象? –

+0

是的,我想單個國家的對象,雖然出了應用程序也讓我知道我必須使用的場景或我不得不使用這種模式的場景 – naval

+0

你需要那個接口嗎,你正在使這兩個方法抽象抽象類 –

回答

0

您需要使用您想要的單個實例的情況下,Singleton Pattern跨應用程序 -

private static Country country; 
public static CountryProvider Instance 
{ 
    get 
    { 
     if(country == null) 
     { 
     country = new Country(); 
     } 
     return country; 
    } 
} 

don't need an abstract class因爲每次你想回到一個國家的intance。所以,Country類應該直接實現接口,因爲無論如何,其他兩個方法都是抽象的,你在類Country中重寫。

相關問題