我创建了一个名为 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();
我的问题是是否遵循严格的方法