为自己的C# ORM 写一个代码自动生成器

来源:互联网 发布:淘宝购买小考作弊神器 编辑:程序博客网 时间:2024/04/29 12:49

 

现在做一个项目要用到以前写的一个ORM 框架,因为写的时候没有用反射,所以代码会有一些格式,

用JS写一个代码自动生成工具,来加快开发.这里用到了自己之前写的一个链表,和Array,及json等

 

 

<html>
<head>
<title>
SnakeORM Tools
</title>
<style type="text/css">
body {text-align:center;font-family:"宋体",Arial Narrow,arial,serif;background:#ffffff;font-size:9px;}
table,td,tr,th{font-size:12px;}
#content{
  position:relative;
  left:50%;
  width:805px;
  height:650px;
  margin-left:-100%;
  margin-top:50px;
  padding-top:10px;
  background-color: #efefef;
  /*padding:0px;*/
  border:1px dotted #434343;
  /*border:1px solid #000000;*/

}
#he{
width:805px;
height:100px;
font-size:12px;
}
#left{
margin:3px 3px 3px 3px;
overflow:hidden;
float:left;
width:200px;
height:590px;
background-color: #ffffff;
border:1px dotted #434343;
}
#right{
text-align:left;
overflow:hidden;
float:right;
width:580px;
height:590px;
background-color: #ffffff;
/*border:1px dotted #434343;*/
}
</style>
</head>
<body>
<div id="content">
<div id="he">
这是为SnakORM 写的一个实体类代码生成器<br>
blog.csdn.net<br>
shmilyhe@163.com<br>
</div>
<div id="left">
<form action="#">
<table>
<tr><td>类名</td><td><input type="text" name="cla" id="cls" onblur="setname(this)"></td></tr>
<tr><td>属性名:</td><td><input type="text" name="a_name" id="a_name"></td></tr><tr><td>
属性类型:</td><td><select name="a_type" id="a_type">
<option value="S.INT">int</option>
<option value="S.STRING">string</option>
<option value="S.DATETIME">DateTime</option>
<option value="S.DOUBLE">double</option>
<option value="S.TEXT">text</option>
<option value="S.LONGTEXT">LONGTEXT</option>
</select>
<button onclick="add()">增加</button></td></tr>
<tr><td>注释:</td><td><input type="text" name="a_commont" id="a_commont"></td></tr>
<tr><td colspan="2"><button onclick="clear1()">清除</button></td></tr>
</table>
</form>
</div>
<div id="right">
<TEXTAREA ID="txt" rows="39" cols="80" onblur="te(this)">
</TEXTAREA>
</div>
</div>
</body>
</html>

<script>
function g(id){
return document.getElementById(id);
}
var CsFile={
head:"using System;/nusing Snake.Data;/n",
cl_head:"public class ",
cl_name:"Product",
cl_name_end:" : S_Serialize/n{/n",
cl_1:"/tprivate static string[] fields ={",

cl_static_fields:"",

cl_2:"};/n",
cl_3:"/tprivate static int[] types ={",

cl_static_types:"",
cl_4:"};/n",
cl_attribs:"",
cl_attribs_end:"/n/tpublic ",
cl_constructor:"Product",
cl_5:"(){}/n/t#region S_Serialize 成员/n/tpublic string[] getFields()/n/t{/n/t/treturn fields;/n/t}/n/tpublic int[] getTypes()/n/t{/n/t/treturn types;/n/t}/n",
cl_6:"/tpublic object[] getDatas()/n/t{/n/t/treturn new object[] {",
cl_datas:"",
cl_7:"};/n/t}/n",
cl_8:"/tpublic string getName()/n/t{/n/t/treturn /"",
cl_cla_name:"",
cl_9:"/";/n/t}/n",
cl_10:"/tpublic void setUp(System.Collections.Hashtable map)/n/t{/n",
cl_convert:"",
cl_11:"/n/t}/n/t#endregion/n}"
}
function List(){
this.head=null;
this.end=null;
this.curr=null;
}
List.prototype.add=function(o){
var tem={ob:o,next:null};
if(this.head){
this.end.next=tem;
this.end=tem;
}else{
this.head=tem;
this.end=tem;
this.curr=tem;
}
}

List.prototype.del=function(inde){
var n=this.head;
for(var i=0;i<inde;i++){
n=n.next;
}
n.next=n.next.next?n.next.next:null;
}
List.prototype.next=function(){
var te=null;
if(this.curr){
te=this.curr.ob; this.curr=this.curr.next;}
return te;
}

List.prototype.hasnext=function(){
if(this.curr==null)return false;
if(this.curr.ob!=null&&this.curr.ob!=undefined)return true;
return false;
}
List.prototype.reset=function(){
this.curr=this.head;

}
List.prototype.clear=function(){
this.head=null;
this.end=null;
this.curr=null;
}
function getAtt(a_name,a_type,a_comment){
return {at_name:a_name,at_type:a_type,at_comment:a_comment};
}
var list=new List();
function te(ev){
var str="";
for(var i in CsFile){
str+=CsFile[i];
}
ev.value=str;
}
function add(){
list.add(getAtt(g('a_name').value,g('a_type').value,g('a_commont').value));
FileAtt();
te(g('txt'));
}
function FileAtt(){
var str="";
var str1="";
var att=new Array();
var types=new Array();
var datas=new Array();
for(var i=0;list.hasnext();i++){
var aa=list.next();
str+="/tpublic "+gettype(aa.at_type)+" "+aa.at_name+";//"+aa.at_comment+"/n";
str1+='/t/t'+aa.at_name+' = '+getconvert(aa.at_type,i);
att[i]='/"_'+aa.at_name+'/"';
types[i]=aa.at_type;
datas[i]=aa.at_name;

}
list.reset();
CsFile.cl_static_fields=att.join(',');
CsFile.cl_static_types=types.join(',');
CsFile.cl_convert=str1;
CsFile.cl_datas=datas.join(',');
CsFile.cl_attribs=str;
}

function gettype(t){
switch(t){
case 'S.INT':return 'int';
case 'S.STRING':return 'string';
case 'S.DOUBLE':return 'double';
case 'S.TEXT':return 'string';
case 'S.LONGTEXT':return 'string';
case 'S.DATETIME':return 'DateTime';
}
}
function getconvert(t,_index){
switch(t){
case 'S.INT':return 'Convert.ToInt32(map[fields['+_index+']]);/n';
case 'S.STRING':return 'Convert.ToString(map[fields['+_index+']]);/n';
case 'S.DOUBLE':return 'Convert.ToDouble(map[fields['+_index+']]);/n';
case 'S.TEXT':return 'Convert.ToDouble(map[fields['+_index+']]);/n';
case 'S.LONGTEXT':return 'Convert.ToDouble(map[fields['+_index+']]);/n';
case 'S.DATETIME':return 'Convert.ToDateTime(map[fields['+_index+']]);/n';
}
}
function setname(obj){
CsFile.cl_name=obj.value;
CsFile.cl_constructor=obj.value;
CsFile.cl_cla_name='_'+obj.value;
}
function clear1(){
list.clear();
FileAtt();
te(g('txt'));
}
</script> 

 

 

 

 

 

原创粉丝点击