c# - 休息关系?

标签 c# wcf web-services rest

我想将一个人添加到社区,但我不确定如何操作?

我的数据合约如下所示:

[DataContract(Name = "Community")]
public class Community
{

    public Group() 
    {
        People = new List<Person>();
    }
    [DataMember(Name = "CommunityName")]
    public string CommunityName { get; set; }
    public List<Person> People { get; set; }

}
[DataContract(Name = "Person")]
public class Person
{
    [DataMember(Name = "PersonName")]
    public string PersonName { get; set; }
}

在我的服务中,我可以像这样添加一个人或一个社区:

List<Community> Communitys = new List<Community>();
List<Person> people = new List<Person>();
public void AddCommunity(Community community)
{
     Communitys.Add(community);
}
public void AddPerson(Person person)
{
     people.Add(person); 
}

public void AddPersonToCommunity(Person person, Community community)
{
    //not sure what to do here
} 

但我不确定如何将一个人加入社区

public void AddPersonToCommunity(Person person, Community community)
{
    //community.CommunityName(Person.Add);?
} 

我有一个 Windows 窗体,当我发布一个人或一个社区时,它是这样完成的:

{
    StringBuilder Community = new StringBuilder();
    Community.AppendLine("<Community>");
    Community.AppendLine("<CommunityName>" + this.textBox4.Text + "</CommunityName>");
    Community.AppendLine("</Community>");

但是您将如何(一旦完成)使用 AddPersonToCommunity 并创建一个字符串生成器以便将一个人添加到社区中?

    {
        StringBuilder CommunityAddPerson = new StringBuilder();
        CommunityAddPerson.AppendLine("<Community&{1}>");
        CommunityAddPerson.AppendLine ......
        CommunityAddPerson.AppendLine("<CommunityName>" + this.textBox4.Text + "</CommunityName>");
        CommunityAddPerson.AppendLine("</Community>");

希望这两个问题合二为一更清楚。

最佳答案

我认为根据您所说的,您的方法可能如下所示:

public void AddPersonToCommunity(string person, string communityName)
{
    var result = communities.Where(n => String.Equals(n.CommunityName, communityName)).FirstOrDefault();
    if (result != null)
    {
        result.Add(new Person() { Name = person });
    }
} 

您的 POST 数据可能如下所示:

<Community>
  <CommunityName>CrazyTown</CommunityName>
  <People>
     <Person>Moe Howard</Person>
  </People>
</Community>

关于c# - 休息关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10046196/

相关文章:

c# - 简单注入(inject)器从命名空间注册所有服务

c# - 多线程奇点

wcf - DLL 与 WCF : Performance vs Extensibility (WCF just in case? )

java - Eclipse 生成的 Web 服务客户端非常慢

java - 我可以从运行时将 XmlAdapter 添加到 JAXB(不带注释)吗?

web-services - 在 SOAPUI 中测试安全的 Web 服务

c# - 进程交互(c#和纯c++)

c# - 可以在 C# 中向下转换为没有数据成员的派生类吗?

c# - 在运行时添加通用约束?

c# - WCF 自定义端点行为中字典的 JQuery 并发问题