public abstract class DataAccessLayerBase{
private string _ConnectionString;
public DataLayerBase(string ConnectionString){
_ConnectionString = ConnectionString;
}
Derived DataAccessLayers then Explicitly Implement the Interface and Inherit the Base class.
as such:
public class HighschoolDataLayer: IDataAccessLayer, DataAccessLayerBase{
public HighschoolDataLayer(string ConnectionString): base(ConnectionString){
}
Highschool Get(int ID){
// ...
}
public void Save(HighSchool s){
//...
}
public void Delete(HighSchool s){
//...
}
// This is the explicit interface implementation. these methods will only be accessable
// if this object has been cast into an IDataAccessLayer. We still need to call the
// Real versions in case someone does cast into an IDataAccessLayer
Object IDataAccessLayer.Get(Object value){
if(value is Int32)
return this.Get((Int32)value);
}
void IDataAccessLayer.Save(Object value){
if(value is Highschool)
return this.Save((Highschool)value);
}
void IDataAccessLayer.Delete(Object value){
if(value is Highschool)
return this.Delete((Highschool)value);
}
}
If the answer I provided is useful or informative please check the "answer" button.
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
Contributor
3938 Points
3276 Posts
Re: Converting to Generics
Oct 12, 2005 02:05 PM|JeffreyABecker|LINK
First I split DataLayerBase into an interface and an Abstract class
interface IDataAccessLayer{
Object Get(Object PrimaryKey);
void Save(Object value);
void Delete(Object value);
}
public abstract class DataAccessLayerBase{
private string _ConnectionString;
public DataLayerBase(string ConnectionString){
_ConnectionString = ConnectionString;
}
protected String ConnectionString{
get{return _ConnectionString;}
}
}
Derived DataAccessLayers then Explicitly Implement the Interface and Inherit the Base class.
as such:
public class HighschoolDataLayer: IDataAccessLayer, DataAccessLayerBase{
public HighschoolDataLayer(string ConnectionString): base(ConnectionString){
}
Highschool Get(int ID){
// ...
}
public void Save(HighSchool s){
//...
}
public void Delete(HighSchool s){
//...
}
// This is the explicit interface implementation. these methods will only be accessable
// if this object has been cast into an IDataAccessLayer. We still need to call the
// Real versions in case someone does cast into an IDataAccessLayer
Object IDataAccessLayer.Get(Object value){
if(value is Int32)
return this.Get((Int32)value);
}
void IDataAccessLayer.Save(Object value){
if(value is Highschool)
return this.Save((Highschool)value);
}
void IDataAccessLayer.Delete(Object value){
if(value is Highschool)
return this.Delete((Highschool)value);
}
}
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.