Skip to content

Commit 52ca0dc

Browse files
committed
Angular
- remove unnecessary instance creation on services
1 parent b2639f2 commit 52ca0dc

File tree

3 files changed

+8
-37
lines changed

3 files changed

+8
-37
lines changed

Angular/Writers/AngularServiceWriter.cs

+4-28
Original file line numberDiff line numberDiff line change
@@ -86,37 +86,13 @@ public virtual void Write(AngularWriteConfiguration configuration, List<ITransfe
8686
List<HttpServiceActionParameterTransferObject> urlDirectParameters = action.Parameters.Where(x => !x.FromBody && !x.Inline && !x.AppendName).ToList();
8787
uri = urlParameters.Count > 0 ? $"{uri}?{urlParameters.First().Name}=" : urlDirectParameters.Count > 0 ? $"{uri}?" : uri;
8888
MultilineCodeFragment code = Code.Multiline();
89-
DeclareTemplate declareTemplate = null;
9089
bool hasReturnType = returnType.Name != "void";
91-
bool isPrimitive = this.IsPrimitive(returnType);
92-
if (returnType.Name == "Array")
90+
ExecuteMethodTemplate nextMethod = Code.Local("subject").Method("next");
91+
if (hasReturnType)
9392
{
94-
TypeTemplate type = ((GenericTypeTemplate)returnType).Types[0];
95-
ICodeFragment createModelCode = isPrimitive
96-
? (ICodeFragment)Code.Cast(type, Code.Local("entry"))
97-
: Code.InlineIf(Code.Local("entry").Equals().ForceNull().Or().Local("entry").Equals().Undefined(),
98-
Code.Undefined(),
99-
Code.New(type, Code.Local("entry"))
100-
);
101-
declareTemplate = Code.Declare(returnType, "list", Code.TypeScript("[]")).Constant();
102-
code.AddLine(declareTemplate)
103-
.AddLine(Code.TypeScript("for (const entry of result) {").BreakLine() /*.StartBlock()*/)
104-
.AddLine(Code.Local(declareTemplate).Method("push", createModelCode).Close())
105-
//.AddLine(Code.TypeScript("").EndBlock());
106-
.AddLine(Code.TypeScript("}").BreakLine());
93+
nextMethod.WithParameter(Code.Local("result"));
10794
}
108-
else if (hasReturnType)
109-
{
110-
ICodeFragment createModelCode = isPrimitive
111-
? (ICodeFragment)Code.Cast(returnType, Code.Local("result"))
112-
: Code.InlineIf(Code.Local("result").Equals().ForceNull().Or().Local("result").Equals().Undefined(),
113-
Code.Undefined(),
114-
Code.New(returnType, Code.Local("result"))
115-
);
116-
declareTemplate = Code.Declare(returnType, "model", createModelCode).Constant();
117-
code.AddLine(declareTemplate);
118-
}
119-
code.AddLine(Code.Local("subject").Method("next").WithParameter(declareTemplate.ToLocal()).Close())
95+
code.AddLine(nextMethod.Close())
12096
.AddLine(Code.Local("subject").Method("complete").Close());
12197
ChainedCodeFragment parameterUrl = Code.This().Field(serviceUrlField);
12298
if (inlineParameters.Count == 0)

Examples/Angular/src/app/services/values-core.service.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ export class ValuesCoreService {
2727
public get(httpOptions: {} = undefined): Observable<string[]> {
2828
let subject = new Subject<string[]>();
2929
this.http.get<string[]>(this.serviceUrl + "/api/values", httpOptions).subscribe(result => {
30-
const list: string[] = [];
31-
for (const entry of result) {
32-
list.push(<string>entry);
33-
}
34-
subject.next(list);
30+
subject.next(result);
3531
subject.complete();
3632
}, error => subject.error(error));
3733
return subject;
@@ -40,8 +36,7 @@ export class ValuesCoreService {
4036
public get2(id: number, httpOptions: {} = undefined): Observable<Value> {
4137
let subject = new Subject<Value>();
4238
this.http.get<Value>(this.serviceUrl + "/api/values/" + id, httpOptions).subscribe(result => {
43-
const model: Value = result === null || result === undefined ? undefined : new Value(result);
44-
subject.next(model);
39+
subject.next(result);
4540
subject.complete();
4641
}, error => subject.error(error));
4742
return subject;

Examples/AspDotNet/CoreApi/Controllers/ValuesController.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public void Put(int id, [FromBody] Value value)
3737
public void Delete(int id)
3838
{ }
3939

40-
// GET api/check
40+
// GET api/values/check
4141
[HttpGet("[action]")]
4242
public ActionResult Check()
4343
{
4444
return this.Ok();
4545
}
4646

47-
// GET api/checkinterface
47+
// GET api/values/checkinterface
4848
[HttpGet("[action]")]
4949
public IActionResult CheckInterface()
5050
{

0 commit comments

Comments
 (0)