Target: define a library to manage database, support Mysql, Mongodb, Mssql.
Require: function inside Mysql, Mongodb, Mssql should be same, with add, update, delete and get methods.
Interface: to define a regulation, constrain the behavior
Generics: to solve the repeat of class, interface and method.
In order to implement a Generic interface, the class needs to be also Generic
class MysqlDb<T> implements DBI<T>{
add(info:T): boolean {
throw new Error('');
}
update(info:T, id:number): boolean {
throw new Error('');
}
delete(id:number): boolean {
throw new Error('');
}
get(id:number): any[] {
throw new Error('');
}
}
class MsSqlDb<T> implements DBI<T>{
add(info:T): boolean {
throw new Error('');
}
update(info:T, id:number): boolean {
throw new Error('');
}
delete(id:number): boolean {
throw new Error('');
}
get(id:number): any[] {
throw new Error('');
}
}
Instance
class User{
username: string | undefined;
password: string | undefined;
}
var u = new User();
u.username = 'shawn';
u.userpassword = '123';
var oMssql = new MsSqlDb<User>();
oMssql.add(u);
Target
: define a library to manage database, support Mysql, Mongodb, Mssql.Require
: function inside Mysql, Mongodb, Mssql should be same, withadd
,update
,delete
andget
methods.Interface
: to define a regulation, constrain the behaviorGenerics
: to solve the repeat of class, interface and method.Define Generic Interface
Define Database
In order to implement a
Generic interface
, the class needs to be alsoGeneric
Instance