2实体的组织(续)
最后更新于:2022-04-01 16:24:10
3)数据行DynamicDataRow.cs
~~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MEntities
{
[Serializable]
public partial class DynamicDataRow
{
public List<DynamicDataField> DataFields { get; private set; }
public DynamicDataRow()
{
DataFields = new List<DynamicDataField>();
}
}
}
~~~
共享代码部分:DynamicDataRow.Shared.cs
~~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MEntities
{
public partial class DynamicDataRow
{
public DynamicDataField this[string FieldName]
{
get
{
DynamicDataField theField = null;
foreach (var fld in DataFields)
{
if (fld.FieldName == FieldName)
{
theField = fld;
break;
}
}
return theField;
}
}
public DynamicDataField this[int Index]
{
get
{
return DataFields[Index];
}
}
}
}
~~~
4)数据表:
~~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MEntities
{
[Serializable]
public partial class DynamicDataTable
{
public List<DynamicDataRow> Rows { get; private set; }
public string TableName { get; set; }
public List<DynamicDataColumn> Columns { get; private set; }
public DynamicDataTable()
{
Rows = new List<DynamicDataRow>();
Columns = new List<DynamicDataColumn>();
}
}
}
~~~
数据表共享代码部分:DynamicDataTable.Shared.cs
~~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MEntities
{
public partial class DynamicDataTable
{
public DynamicDataColumn this[string FieldName]
{
get
{
DynamicDataColumn theCol = null;
foreach (var col in Columns)
{
if (col.FieldName == FieldName)
{
theCol = col;
break;
}
}
return theCol;
}
}
public DynamicDataColumn this[int Index]
{
get
{
return Columns[Index];
}
}
}
}
~~~
实体的组织原则:
A)尽可能简单,外部程序集依赖应尽可能少,这样任何其它层都可以引用它,也便于穿越通信层,毕竟实体只是数据的载体;
B)索引器无法自动到达客户端,索引器构建主要是为了客户端绑定的时候提供一致的语法和语义;
C)如果实体有继承体系,那么索引器可能无法共享到客户端,这个时候可以直接把代码添加到客户端中即可,注意命名空间要保持与服务器一致。