我正在努力放置业务逻辑的最佳位置。我现在可以正常使用,但感觉不正确,因此请多多指教。
我有一个代表我的实体的类
export class Order extends EntityBase {
static initializer(entity: Order) { }
constructor() {
super()
}
isRush: boolean
// Generated code. Do not place code below this line.
id: number;
description: string;
installDate: Date;
masterId: number;
master: Master;
}
我创建了放置业务逻辑的服务
@Injectable()
export class OrderLogicService {
constructor() {}
public calculateRush(start, end, holidays: Holiday[]) {
//business rules here
return true;
}
}
我目前在订单服务中使用它,我从api中获取数据。我的直觉告诉我这是错误的,但是我不确定将其放置在什么地方,而不必多次调用。
@Injectable()
export class OrderService {
urlBase: string = environment.urlBaseOld;
private _em: EntityManager = new EntityManager(environment.urlBaseBreeze);
constructor(private http: Http,
private orderLogic: OrderLogicService) {
config.initializeAdapterInstance("uriBuilder", "json");
RegistrationHelper.register(this._em.metadataStore);
}
getMaster(id: number): Promise<Master> {
let promise = new Promise<Master>((resolve, reject) => {
let query = EntityQuery.from("Master").where("id", "==", id);
let strategy: FetchStrategySymbol;
strategy = FetchStrategy.FromServer;
this._em.executeQuery(query.using(strategy)).then(
queryResult => {
let holidays = this.getHolidays()
let master = <Master>queryResult.results[0]
// ************* order.isRush field is set here using my business logic service
master.orders.forEach(order => {
order.isRush = this.orderLogic.calculateRush(
order.master.submittedToIaOn, order.installDate, holidays)
})
//**************************
resolve(master);
},
error => reject(error)
);
});
return promise;
}
}
答案 0 :(得分:1)
服务是放置业务逻辑的最佳位置。为了使您的勇气平静,您可以重构代码并使用带有嵌套箭头功能的描述性名称提取一些私有方法。除此之外,nothig的代码和方法恕我直言是错误的。