在休息的关系?(relationships in rest?)

2019-07-28 20:32发布

我想一个人添加到社区和IM不知道如何做?

我datacontract看起来是这样的:

[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>");

希望这是一个我想更清晰的两个问题。

Answer 1:

我觉得你的方法,根据你说的话,可能是这样的:

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>


Answer 2:

你总是可以添加一个方法来一个人添加到社区:

public void AddPersonToCommunity(Person person, Community community)...

要么

public void AddPersonToCommunity(string personName, string communityName)...


文章来源: relationships in rest?