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();
我的問題的方法,再經過是,它是緊的方式遵守或不遵守
你想跨應用程序的國家的單個對象? –
是的,我想單個國家的對象,雖然出了應用程序也讓我知道我必須使用的場景或我不得不使用這種模式的場景 – naval
你需要那個接口嗎,你正在使這兩個方法抽象抽象類 –