张磊 Larry.Zhang

Stay hungry, stay foolish.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Silverlight创建继承INotifyPropertyChanged接口的类会产生大量如下代码:

代码
/// <summary>
/// 说明
/// </summary>
public string description
{
get { return _description; }
set
{
if (_description != value)
{
_description
= value;
NotifyPropertyChanged(
"description");
}
}
}

虽然VS的CodeSnippets已经能够简化大量代码输入
但用惯了Codematic从数据库直接生成Model类,感觉CodeSnippets还是效率太低
于是修改了Codematic的BuilderModel组件,使生成Model代码支持Silverlight的需求
感谢李天平大哥对Codematic的开源:)

 

插件下载地址:https://files.cnblogs.com/zhlei616/BuilderModelSilverlight.rar

下载后将解压包里的LTP.BuilderModel.dll覆盖Codematic安装目录下的同名文件即可
覆盖前请先备份原文件,需要时再替换回来

 

动软.NET代码生成器官方网站:http://www.maticsoft.com/

 

BuilderModel修改后的代码
public class BuilderModel : IBuilder.IBuilderModel
{
#region 公有属性
protected string _modelname=""; //model类名
protected string _namespace = "Maticsoft"; //顶级命名空间名
protected string _modelpath="";//实体类的命名空间
protected List<ColumnInfo> _fieldlist;

/// <summary>
/// 顶级命名空间名
/// </summary>
public string NameSpace
{
set { _namespace = value; }
get { return _namespace; }
}
/// <summary>
/// 实体类的命名空间
/// </summary>
public string Modelpath
{
set { _modelpath = value; }
get { return _modelpath; }
}
/// <summary>
/// model类名
/// </summary>
public string ModelName
{
set { _modelname = value; }
get { return _modelname; }
}
/// <summary>
/// 选择的字段集合
/// </summary>
public List<ColumnInfo> Fieldlist
{
set { _fieldlist = value; }
get { return _fieldlist; }
}

#endregion

public BuilderModel()
{
}

#region 生成完整Model类
/// <summary>
/// 生成完整sModel类
/// </summary>
public string CreatModel()
{
StringPlus strclass
= new StringPlus();
strclass.AppendLine(
"using System;");
strclass.AppendLine(
"using System.ComponentModel;");
strclass.AppendLine(
"namespace " + Modelpath);
strclass.AppendLine(
"{");
strclass.AppendSpaceLine(
1, "/// <summary>");
strclass.AppendSpaceLine(
1, "/// 实体类" + _modelname + " 。(属性说明自动提取数据库字段的描述信息)");
strclass.AppendSpaceLine(
1, "/// </summary>");
strclass.AppendSpaceLine(
1, "[Serializable]");
strclass.AppendSpaceLine(
1, "public class " + _modelname + " : INotifyPropertyChanged");
strclass.AppendSpaceLine(
1, "{");
strclass.AppendSpaceLine(
2, "#region 实现INotifyPropertyChanged");
strclass.AppendSpaceLine(
2, "public event PropertyChangedEventHandler PropertyChanged;");
strclass.AppendSpaceLine(
2, "public void NotifyPropertyChanged(string propertyName)");
strclass.AppendSpaceLine(
2, "{");
strclass.AppendSpaceLine(
3, "if (this.PropertyChanged != null)");
strclass.AppendSpaceLine(
3, "{");
strclass.AppendSpaceLine(
4, "this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));");
strclass.AppendSpaceLine(
3, "}");
strclass.AppendSpaceLine(
2, "}");
strclass.AppendSpaceLine(
2, "#endregion");
strclass.AppendSpaceLine(
2, "public " + _modelname + "()");
strclass.AppendSpaceLine(
2, "{}");
strclass.AppendLine(CreatModelMethod());
strclass.AppendSpaceLine(
1, "}");
strclass.AppendLine(
"}");
strclass.AppendLine(
"");

return strclass.ToString();
}
#endregion

#region 生成Model属性部分
/// <summary>
/// 生成实体类的属性
/// </summary>
/// <returns></returns>
public string CreatModelMethod()
{

StringPlus strclass
= new StringPlus();
StringPlus strclass1
= new StringPlus();
StringPlus strclass2
= new StringPlus();
strclass.AppendSpaceLine(
2, "#region Model");
foreach (ColumnInfo field in Fieldlist)
{
string columnName = field.ColumnName;
string fieldName=" _" + columnName.ToLower();
string columnType = field.TypeName;
bool IsIdentity = field.IsIdentity;
bool ispk = field.IsPK;
bool cisnull = field.cisNull;
string deText = field.DeText;
columnType
= CodeCommon.DbTypeToCS(columnType);
string isnull = "";
if (CodeCommon.isValueType(columnType))
{
if ((!IsIdentity) && (!ispk) && (cisnull))
{
isnull
= "?";//代表可空类型
}
}
strclass1.AppendSpaceLine(
2, "private " + columnType + isnull + fieldName + ";");//私有变量
strclass2.AppendSpaceLine(2, "/// <summary>");
strclass2.AppendSpaceLine(
2, "/// " + deText);
strclass2.AppendSpaceLine(
2, "/// </summary>");
strclass2.AppendSpaceLine(
2, "public " + columnType + isnull + " " + columnName);//属性
strclass2.AppendSpaceLine(2, "{");
strclass2.AppendSpaceLine(
3, "get{return " + fieldName + ";}");

strclass2.AppendSpaceLine(
3, "set");
strclass2.AppendSpaceLine(
3, "{");
strclass2.AppendSpaceLine(
4, "if("+fieldName+"!=value)");
strclass2.AppendSpaceLine(
4, "{");
strclass2.AppendSpaceLine(
5, fieldName+"=value;");
strclass2.AppendSpaceLine(
5, "NotifyPropertyChanged(\""+columnName+"\");");
strclass2.AppendSpaceLine(
4, "}");
strclass2.AppendSpaceLine(
3, "}");

strclass2.AppendSpaceLine(
2, "}");
}
strclass.Append(strclass1.Value);
strclass.Append(strclass2.Value);
strclass.AppendSpaceLine(
2, "#endregion Model");

return strclass.ToString();
}

#endregion
}