通过类的静态实例调用该方法

时间:2012-10-27 13:25:25

标签: c# asp.net design-patterns

我创建了一个名为ICountry的接口

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

    }

然后我创建了一个名为CountryProvider的Abstract类,其中我实现了ICountry接口的方法,就像这样

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);

    }

然后在我创建了一个名为Country的简单类并继承了CountryProvider类并覆盖了抽象方法

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();

我的问题是,这是否采取紧迫措施

1 个答案:

答案 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因为每次你想要归还国家的内容。所以,Country类应该直接实现接口,因为其他两个方法都是抽象的,无论如何你都要在类Country中重写。