自定义代码开发指导规范
更新时间: 2024-10-21 浏览次数: {{ hits }}


1. 不能使用过时的类或方法([Obsolate]标识)


 说明:C#中对于标记过时的方法,有可能会在新版本的.Net Framework或官方接口中剔除,因此强制不再使用此类或方法。


2.Object 的Equals方法容易抛空引用异常,应使用常量或确定有值得对象来调用Equals。


// 反例:
user.Equals("Jack");
// 推荐写法
"Jack".Equals(user);


3.不允许循环里调用Engine接口

请尽量减少服务间的通信次数:跨进程的接口调用大约是普通接口调用的耗时和消耗CPU的10,000倍。通常如果需要多次调用,需要改用批量的接口

//如批量提交
 H3.DataModel.BulkCommit   commit = new H3.DataModel.BulkCommit();
   H3.DataModel.BizObjectSchema schema = this.Request.Engine.BizObjectManager.GetPublishedSchema("D0001001");
   for(int i = 0;i < 1000; i++) {
           H3.DataModel.BizObject obj = new   H3.DataModel.BizObject(this.Request.Engine,schema,this.Request.UserContext.UserId   );
           obj["F000001"] = 1;
           obj.Create(commit); // 如果已经存在对象,不需要新建,只需Update就可以:obj.Update(commit);
                      }
    string errorMsg = null;
    commit.Commit(this.Request.Engine.BizObjectManager,out errorMsg);

4.代码中禁止使用反射

GetType().Get*()方法,是只能单线程执行的。在一个并发环境中,使用这样的代码,是无法发挥多CPU线程的优势的。反射的执行速度大概比普通代码慢10倍。

5.禁止使用以下引用和方法

"Finalize",
"MemberwiseClone",
// 这个会很消耗CPU,用户做逻辑计算是不会需要这个方法的
"GetType",
 "Clone",
"GetHashCode",
"ResetObject",
"AfterDeserialize",
"AfterSerialize",
"AfterUpdateDirtyProperties",
"ClearDirtyProperties",
"DirtyProperties",
"GenerateObjectId",
"GetPropertyDirty",
"ParentIndex",
"ParentObjectId",
"ParentPropertyName",
"Serialized",
"SetPropertyDirty",
"SetPropertyUndirty",

// 回调类
"System.MulticastDelegate",
"System.IAsyncResult",
"System.AsyncCallback",
"System.IAsyncResult",

// 这个比较危险,这个是Disposable的
"H3.Data.Database.ICommand",
"H3.Data.Database.ICommandFactory",
"H3.Data.Serialization.ISerializer",
// 反射类
"System.Type",
"System.Reflection.PropertyInfo",
"System.RuntimeFieldHandle",
"System.RuntimeTypeHandle",
"System.Activator",
"System.Reflection.MethodInfo",
"System.Reflection.MethodBase",
"System.Reflection.MemberInfo",
"System.ComponentModel.MarshalByValueComponent",
// 下面这个访问是没有意义的,因为我们是分布式架构,其实无法Monitor的,而且这个会阻塞掉线程,实际上又没有用处
"System.Threading.Monitor",
"System.Collections.Concurrent.ConcurrentDictionary",
"System.Threading.Interlocked",
"System.Console",
"System.Environment",
"System.Timers.Timer",
"System.Timers.ElapsedEventArgs",
"System.Timers.ElapsedEventHandler",
"System.Reflection.Assembly",
"System.OperatingSystem",
"System.Version",
"System.AppDomain",
"System.TypeCode",
"System.IO.TextWriter",
"System.ComponentModel.Component",
"System.Data.SqlClient.SqlConnection",
"System.Data.SqlClient.SqlCommand",
"System.Data.SqlClient.SqlConnectionStringBuilder",
"System.Data.SqlClient.SqlDataReader",
"System.Data.SqlClient.SqlDataAdapter",
"System.Data.SqlClient.SqlTransaction",
"System.Data.SqlClient.SqlBulkCopy",
"System.Data.SqlClient.SqlBulkCopyOptions",
"System.Data.SqlClient.SqlParameterCollection",
"System.Data.SqlClient.SqlParameter",
"System.Data.Common.DbConnection",
"System.Data.Common.DbCommand",
"System.Data.Common.DbDataReader",
"System.Data.Common.DbDataAdapter",
"System.Data.Common.DbTransaction",
// 动态编译
"Microsoft.CSharp.CSharpCodeProvider",
"System.CodeDom.Compiler.ICodeCompiler",
"System.CodeDom.Compiler.CompilerParameters",
"System.CodeDom.Compiler.CompilerResults",
"System.Collections.Specialized.StringCollection",
"System.CodeDom.Compiler.CodeDomProvider",
"System.CodeDom.Compiler.CompilerError",
"System.CodeDom.Compiler.CompilerErrorCollection"

6.禁止对数据库进行DDL操作

数据库的DDL操作指的是数据定义语言(Data Definition Language)的操作,主要用于定义和管理数据库的结构。DDL操作包括创建(CREATE)、删除(DROP)、修改(ALTER)数据库和表等结构。

7.禁止对数据库大数据量的操作,如一次性读取上千条数据;

8.禁止循环调用数据库的操作;

9.不要通过SQL直接对数据库业务数据进行增删改的操作,需要通过接口操作;

10.前端代码禁用循环调用服务端接口,构造一次性调用,返回一次性处理

11.前端子表行添加时,尽量采用批量添加

12.【建议】对人员、部门、关联数据的赋值,尽量不只是赋值Id,采用赋值对象

// 前端demo
// 往子表填充,构造数据
            var rows = [];
            for (var i = 0; i< 300; i++) {
                rows.push({
                    // 文本
                    'D000183F2181ec713b534bb497018f221ca13b05.F0000003': 'abc',
                    // 关联数据
                    'D000183F2181ec713b534bb497018f221ca13b05.F0000005': {
                        ObjectId: '4e65f4cb-ffdc-408b-9ebd-19985031e437',
                        Name: '123',
                        F0000001: 'abc', // 关联表字段值
                    },
                    // 人员
                    'D000183F2181ec713b534bb497018f221ca13b05.F0000004': [{
                        ObjectId: '02a6d7f7-10bd-4fa7-bb20-a3724b262a30',
                        UnitId: '02a6d7f7-10bd-4fa7-bb20-a3724b262a30',
                        Name: '詹益肖',
                        DisplayName: '詹益肖',
                        ParentId: '18f923a7-5a5e-426d-94ae-a55ad1a4b240',
                        Type: 4
                    }],
                    // 部门
                    'D000183F2181ec713b534bb497018f221ca13b05.F0000009': [{
                        DisplayName: "研发中心1",
                        ParentId: "18f923a7-5a5e-426d-94ae-a55ad1a4b240",
                        State: 0,
                        Type: 2,
                        UnitID: "e57259cc-b481-43ef-90c9-79d7601e43db"
                    }],
                    // 图片、附件
                    'D000183F2181ec713b534bb497018f221ca13b05.F0000007': [{
                        "Code": "8b882f25-59ec-4056-8ac4-e6428b831a2b",
                        "Name": "8F03C85188BC298C770B3C65752D3338.png.JPG",
                        "Description": null,
                        "Url": "/Form/Download/?AttachmentID=8b882f25-59ec-4056-8ac4-e6428b831a2b",
                        "ThumbnailUrl": "https://h3yuntestthumbnail.oss-cn-shenzhen.aliyuncs.com/mi4x54jcr54b0p8hwoad4wxo3/8b882f25-59ec-4056-8ac4-e6428b831a2b",
                        "Size": 152523,
                        "SortKey": 1
                    }],
                    
                });
            }
            this.D000183F2181ec713b534bb497018f221ca13b05.BatchAddRows(rows);