First we need to define the interface.
the following code snippet will be define the base interface. T will represent the
generic object. we will specific the object when we implement the provider for
those specfic object.
public interface IBaseInterface<T> { T ReadData(string spName, string itemID); IEnumerable<T> ReadList(string spName); int Delete(string spName, string itemID); }
second we will a provider class that will implement the interface.
public class ProcessProvider<T> :IBaseInterface<T> where T:new() { public T ReadData(string spName, string itemID) { MyDB myDBAccess = new MyDB (); T tObj = new T(); using (SqlDataReader tempDataReader = myDBAccess.LoadDataItem(spName,itemID)) { if (tempDataReader != null && tempDataReader.HasRows) { tempDataReader.Read(); for (int i = 0; i < tempDataReader.FieldCount; i++) { if (tempDataReader.GetValue(i) != DBNull.Value) { SciUtil.DataMapper.SetPropertyValue(tObj,
tempDataReader.GetName(i),
t empDataReader.GetValue(i)); } } tempDataReader.Close(); } } return tObj; } public IEnumerable<T> ReadList(string spName) { MyDB LeaseRetensionDBAccess = new MyDB(); List<T> tObjectList = new List<T>(); using (SqlDataReader tempDataReader = myDBAccess.LoadDataTable(spName)) { if (tempDataReader != null && tempDataReader.HasRows) { while (tempDataReader.Read()) { T tObject = new T(); for (int i = 0; i < tempDataReader.FieldCount; i++) { if (tempDataReader.GetValue(i) != DBNull.Value) { SciUtil.DataMapper.SetPropertyValue(tObject,
tempDataReader.GetName(i), tempDataReader.GetValue(i)); } } tObjectList.Add(tObject); } tempDataReader.Close(); } } return tObjectList; } public int Delete(string spName, string itemID) { MyDB myDBAccess = new MyDB (); return myDBAccess.DeleteDataItem(spName, itemID); } } }
Third we can implement each specific provider after you finished implemented
the generic object provider.
when we inherit the generic provider, we need to replace the Generic T with
the specifc MyObject. then the code will determine the Object at run time.
public class MyObjectProcessProvider:ProcessProvider<MyObject> { public MyObject GetCRMPortfolioData(string myObjectID) { return base.ReadData("My Stored Procedure", CRMPortfolioID); } public IEnumerable<MyObject> GetCRMPortfolioDataList() { return base.ReadList("My Stored Procedure"); } }
when we have a new entity. we only need to add the new object to the entity
component. then we only need to add a provider with above coding and replace
MyObject with new object. the Provider is ready to use.
No comments:
Post a Comment