(1)一个UI表单
(2)相应的实体类,加上了映射特性
public class UserEntity
{
……
[Member] //表示实体类与窗体类成员Age的值想对应。
public int Age;
[Control] //表示实体类与窗体类控件成员Name的值映射。
public string Name
{
get { return _name; }
set { _name = value; }
}
[Control("CardNo.Text")] //表示实体类与窗体类控件成员的Text属性相映射,注意,可以指定Path格式。
public string CardNo
{
get { return _cardNo; }
set { _cardNo = value; }
}
……
}
public class EmployeeEntity
{
……
[Reference(typeof(UserEntity))] //引用了UserEntity的映射。
public UserEntity User
{
get { return _user; }
set { _user = value; }
}
[Control] //控件
public float PostSalary
{
get { return _postSalary; }
set { _postSalary = value; }
}
……
}
(3)在窗体类中调用ObjectEngine实现OO映射。窗体就是一个继承Form的类,因此,O-O Mapper即可以支持普通映射,也可以支持UI to Entity映射。
1)实现表单类与实体类映射
private void Map_Click(object sender, System.EventArgs e)
{
this.o = CZB.ObjectMapper.ObjectEngine.
Map(this,typeof(EmployeeEntity)) as EmployeeEntity;
}
2)实现实体类与表单类的映射
private void InverseMap_Click(object sender, System.EventArgs e)
{
this.o.User.Name = "c.z.b in";
this.o.User.Age = 19;
this.o.CompoInsurance = 0;
CZB.ObjectMapper.ObjectEngine.InverseMap(this,o);
}
这个小组件功能非常简单也很有限,仅供一些参考。Demo如附件。/Files/baihmpgy/CZB.ObjectMapper.Test.rar
最新评论