关于接口与抽象类的感悟

时间:2023-01-02 12:08:30

当有属性需要给外部实现时使用抽象类比接口更好;

1 using KPIWebAPI.ViewModels;
2
3 namespace KPIWebApi.Utils
4 {
5 interface ICanAssembleFormula
6 {
7 KPIFormula AssembleFormula();
8 }
9 }

 

 1 using KPIWebApi.Utils;
2 using KPIWebAPI.ViewModels;
3
4 namespace XKPI.Utils
5 {
6 public abstract class AbsAssembleFormula : ICanAssembleFormula
7 {
8 public int KPI_ID;
9 public AbsAssembleFormula(int kpiid) { KPI_ID = kpiid; }
10 public abstract KPIFormula AssembleFormula();
11 }
12 }

  

 1 using System.Collections.Generic;
2 using System.Linq;
3 using KPIWebAPI.ViewModels;
4 using XKPI.Models;
5 using KPIWebAPI.Models;
6
7 namespace XKPI.Utils
8 {
9 class UPDemo : AbsAssembleFormula
10 {
11 public UPDemo(int kpiid):base(kpiid) { }
12 public override KPIFormula AssembleFormula()
13 {
14 using (var db = new KPIContext())
15 {
16 EP_KPI_SET body = db.EP_KPI_SET.FirstOrDefault(r => r.KPI_ID == KPI_ID);
17 List <EP_KPI_PARAM> list = db.EP_KPI_PARAM.Where(r => r.KPI_ID == KPI_ID).ToList();
18 return new KPIFormula(body, list);
19 }
20 }
21 }
22 }

假如只提供接口给外部,接口方法 KPIFormula AssembleFormula();就需要传参kpiid变成KPIFormula AssembleFormula(kpiid);外部类实现的时候就体现不出封装;

  1 using IronPython.Hosting;
2 using KPIWebApi.Utils;
3 using KPIWebAPI.ViewModels;
4 using Microsoft.Scripting.Hosting;
5 using System;
6 using System.Collections.Generic;
7 using XKPI.Utils;
8
9 namespace KPIWebAPI.Utils
10 {
11 public class UsingPython
12 {
13 ScriptEngine engine ;
14 ScriptScope scope ;
15 ScriptSource source;
16 private UsingPython() { }
17
18 /// <summary>
19 /// 传参公式字符串初始化引擎
20 /// </summary>
21 /// <param name="ScriptString"></param>
22
23 protected UsingPython(string ScriptString)
24 {
25 engine = Python.CreateEngine();
26 scope = engine.CreateScope();
27 source = engine.CreateScriptSourceFromString(ScriptString);
28 }
29
30 /// <summary>
31 /// 传参公式实体初始化引擎
32 /// </summary>
33 /// <param name="formula"></param>
34 public UsingPython(KPIFormula formula) : this(formula.KPIScript)
35 {
36
37 }
38
39 public UsingPython(AbsAssembleFormula ican) : this(ican.AssembleFormula())
40 {
41
42 }
43
44
45
46 public object ExcuteScriptString(string pyContent, List<Param> paramList)
47 {
48 try
49 {
50 var engine = Python.CreateEngine();
51 var scope = engine.CreateScope();
52 var source = engine.CreateScriptSourceFromString(pyContent);
53 foreach (var item in paramList)
54 {
55 scope.SetVariable(item.Name, item.FixValue);
56 }
57 source.Execute(scope);
58 return scope.GetVariable("result").ToString();
59 }
60 catch (Exception ex)
61 {
62 throw ex;
63 }
64 }
65
66 public object ExcuteScriptFile(List<Param> paramList)
67 {
68 try
69 {
70 scope.SetVariable("result", "");
71 foreach (var item in paramList)
72 {
73 if (item.FixValue==null)
74 {
75 scope.SetVariable(item.Code.Trim(), 0);
76 //throw new Exception(item.Code.Trim()+" is null");
77 }
78 else
79 {
80 scope.SetVariable(item.Code.Trim(), item.FixValue);
81 }
82
83 }
84 source.Execute(scope);
85 return scope.GetVariable("result").ToString();
86 }
87 catch (Exception ex)
88 {
89 throw ex;
90 }
91 }
92
93 /// <summary>
94 /// 简要传参数,执行KPI算法
95 /// </summary>
96 /// <param name="paramList"></param>
97 /// <returns></returns>
98 public object ExcuteScriptFile(List<SimpleParam> paramList)
99 {
100 try
101 {
102 scope.SetVariable("result", "");
103 foreach (var item in paramList)
104 {
105 if (item.FixValue == null)
106 {
107 throw new Exception(item.Code.Trim() + " is null");
108 }
109 scope.SetVariable(item.Code.Trim(), item.FixValue);
110 }
111 source.Execute(scope);
112 return scope.GetVariable("result").ToString();
113 }
114 catch (Exception ex)
115 {
116 throw ex;
117 }
118 }
119
120
121 public string GetDemo() {
122 return @"using System;
123 using System.Collections.Generic;
124 using System.Linq;
125 using System.Text;
126 using System.Threading.Tasks;
127 using KPIWebAPI.ViewModels;
128 using XKPI.Models;
129 using KPIWebAPI.Models;
130
131 namespace XKPI.Utils
132 {
133 class UPDemo : AbsAssembleFormula
134 {
135 public UPDemo(int kpiid):base(kpiid) { }
136 public override KPIFormula AssembleFormula()
137 {
138 using (var db = new KPIContext())
139 {
140 EP_KPI_SET body = db.EP_KPI_SET.FirstOrDefault(r => r.KPI_ID == KPI_ID);
141 List <EP_KPI_PARAM> list = db.EP_KPI_PARAM.Where(r => r.KPI_ID == KPI_ID).ToList();
142 return new KPIFormula(body, list);
143 }
144 }
145 }
146 }
147 ";
148 }
149 }
150 }