即使我通过为类ActivityType返回ActivityType.Code属性值,也在DataContract中覆盖了ToString方法。我还没有在客户端上设置ActivityType.Code属性值以绑定grid.can让我知道ToString方法如何对客户端可用。
这是代码库
数据合同
[DataContract]
public class Activity
{
[DataMember]
public ActivityType ActivityTypeId { get; set; }
[DataMember]
public string Code { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public EntityStatus Status { get; set; }
}
[DataContract]
public enum EntityStatus
{
[EnumMember]
Active = 0,
[EnumMember]
Inactive = 1
}
[DataContract]
public class ActivityType
{
[DataMember]
public string Code { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
public override string ToString()
{
if (Code != null)
{
return Code.ToString(CultureInfo.InvariantCulture);
}
else
{
return base.ToString();
}
}
}
服务实施
public class TimeSheetManagementService : ITimeSheetManagementService
{
public List<BusinessEntities.Activity> GetActivities()
{
TimeSheetManagementDataController controller = new TimeSheetManagementDataController();
var activities = controller.GetActivities();
var activitiesresult = activities.Select(activity => new BusinessEntities.Activity()
{
ActivityTypeId =
new BusinessEntities.ActivityType()
{
Code = activity.ActivityType.Code,
Description =
activity.ActivityType.
Description,
Name = activity.ActivityType.Name
},
Code = activity.Code,
Description = activity.Description,
Status = (EntityStatus) Enum.Parse(typeof(EntityStatus),Convert.ToString((activity.Status==true)?1:0))
});
return activitiesresult.ToList();
}
}
客户端实施
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField runat="server" DataField="Code"/>
<asp:BoundField runat="server" DataField="Description"/>
<asp:BoundField runat="server" DataField="Status"/>
<asp:BoundField runat="server" DataField="ActivityTypeId"/>
</Columns>
</asp:GridView>
输出
Code Description Status ActivityTypeId
ACT2 Requirement Feasibility Study Inactive TimeSheetManagementServiceRef.ActivityType
ACT1 Requirement analysis Inactive TimeSheetManagementServiceRef.ActivityType
答案 0 :(得分:9)
数据合同用于传输数据 - 而不是用于传输逻辑。使用添加服务引用时,添加Web引用,xsd.exe,wsdl.exe或svcutil.exe仅描述属性 - 在服务器上声明的所有方法都将丢失。有两种方法可以解决这个问题:
ToString
方法显然属于客户端,因为它与数据表示有关,因此在客户端创建合同类的第二部分部分,并在该类中声明ToString
方法。