2、在设计数据实体时,如何才能将它直接绑定到界面上。这也是我在开发中一直不是很明白的地方,
-----------------------------
这个是使用反射和Attribute可以很做到的
下面的代码是asp.net中实体类绑定到webform
Class1是一个实体类,ControlIDAttribute表示实体类中的字段要绑定到控件的名字叫什么
Test的BindData(Page,Entity)方法把Entity绑定到页面上/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
[ControlID("StudentName")]
public string name;
[ControlID("StudentAge")]
public int age;public Class1(){}
}public class ControlIDAttribute:Attribute
{
public string _id;public ControlIDAttribute(string id)
{
_id=id;
}
}public class Test
{
public void  BindData( Control root,object entity)
{
object temp=null;
foreach(Control c in root.Controls)
{
temp=GetValueByControlID(c.ClientID,entity);if(c is TextBox)
{
((TextBox)c).Text=temp.ToString();
}
if(c.HasControls())
{
BindData(c,entity);
}
}
}public object GetValueByControlID(string controlID,object entity)
{
Type t = entity.GetType();
foreach(FieldInfo f in t.GetFields())
{
foreach(Attribute attr in Attribute.GetCustomAttributes(f))
{
if(attr is ControlIDAttribute && ((ControlIDAttribute)attr)._id == controlID )
{
return f.GetValue(entity);
}
}
}
return null;
}
}

解决方案 »

  1.   

    还是用现成的吧。
    NHB不是很好嘛。而且还有很多的商业ORM
      

  2.   

    谢谢 zjsen(星愿:我是一只小小~~~~菜鸟) 
    我的意思是在将实体绑到界面是,可能有些数据是实体之中所没有的那部分数据。
    如:入库时要用到一些供应商的资料时而入库这个实体之中又没有,我该怎么处理呀。也许像一楼说的一样,路还是比较长。我觉的做为一种实践也不错呀,即使结果是失败!
    最近花了二三个月的时间来学习uml,也想用于这次的开发。准备采用Borland 的 together做为建模工具,用C#语言来实现。还是希望大家能够提供一些帮助!谢谢
      

  3.   

    不如好好完善其他orm,nhb很好,你的工作最好做些他们没有涉及到的部分,比如你所说的邦定方式,这样做好之后有可能成功。
      

  4.   

    感觉好像表现层的东西比较少人关心,漫说实体类,即使是一个DataTable,如何把它的DataColumn通过一个很简单的步骤生成一堆表现层控件,也是很麻烦
    而实体类,老兄或可参考DevExpress的XPO,它是一个商业的ORM,而它在表现层提供了一个控件,大概叫“PersistentRepository”,就是为了实体类与窗体控件的绑定的,希望对你有帮助。
      

  5.   

    谢谢大家的支持!!!
    我也在参考其他人写的orm,主要看了一下Gentle.net,现在正准备资料。
    我在使用uml的过程中,不知道是直接画出类图还是将系统结构及功能先画出来。有点乱的感觉,
    有过这方面经验的能不能给个实例。