Node.js从Generic BaseDAO调用方法到BaseService类

时间:2019-05-25 10:47:16

标签: node.js typescript generic-programming

我是Node.js的新手,正在尝试为服务和dao类实现泛型。

我在BaseDAO类中有一个findOne方法,我需要从BaseService类中调用它。我没有任何编译错误,但也没有任何结果。下面是我的BaseDAO,BaseService,UserDAO和UserService的代码。

BaseDAO.ts `从“ typeorm”导入{Repository};

导出抽象类BaseDAO扩展存储库{

abstract getRepository(): Repository<T>;

async search(data: any) {
    return await this.find(data);
}

async findOne(data: any) {
    return await this.getRepository().findOne(data);
}

}`

BaseService.ts `从“ ../repository/BaseDAO”导入{BaseDAO};

导出抽象类BaseService {

protected sessionInfo: any;
protected dao: BaseDAO<T>;

constructor(dao: BaseDAO<T>) {
    this.dao = dao;
}

async search(item: any) {
    try {
        return  await this.dao.search(item);
    } catch (error) {
        throw error;
    }
}

async findOne(data: any) {
    console.log(this.dao);
    return await this.dao.findOne(data);
}

}`

UserDAO.ts `从“ ./BaseDAO”导入{BaseDAO}; 从“ ../entity/User”导入{User}; 从“ typeorm”导入{getRepository,Repository};

导出类UserDAO扩展了BaseDAO {

getRepository() {
    return getRepository(User);
}

}`

UserService.ts `从“ ./BaseService”导入{BaseService}; 从“ ../repository/UserDAO”导入{UserDAO}; 从“ ../entity/User”导入{User};

导出类UserService扩展了BaseService {

constructor() {
    super(new UserDAO());
}

}`

0 个答案:

没有答案