0

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

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

    }

然后在我创建了一个名为 CountryProvider 的抽象类之后,我在其中实现了 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();

我的问题是是否遵循严格的方法

4

1 回答 1

0

如果您想要跨应用程序的单个实例,则需要使用单例模式-

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 类中重写了它们。

于 2012-10-27T13:31:17.730 回答