0

以下では、1 つの学生/グループ サービスを実行していますが、ここでは、「グループ」を保持するサービスと「学生」を保持するサービスの 2 つのサービスを実行したいと考えています。しかし、生徒をグループに追加したいときに混乱が生じます。どうすればよいcross serviceですか?

たとえば、このコードは学生をグループに追加し、残りの Web サービスはすべて 1 つにします。

    public void AddStudentToGroup(string group, string studentID, string firstName, string lastName)
    {
        var result = Groups.Where(n => String.Equals(n.GroupName, group)).FirstOrDefault();
        var result1 = students.Where(n => String.Equals(n.StudentID, studentID)).FirstOrDefault();
        if (result != null)
        {
            result.Groupsz.Add(new Student() { StudentID = studentID, FirstName = firstName, LastName = lastName });
        }
        if (result1 != null)
        {
            result1.StudentGroup.Add(new Group() { GroupName = group });
        }

    } 

したがって、私の質問は、サービスをグループを保持するサービスと学生を保持するサービスの 2 つに分割した場合、上記のコードで行ったこと (グループに学生を追加する) を 2 つのサービスにまたがって達成するにはどうすればよいかということです。そして、これを達成しようとして、クライアントはどこに POST しますか?

私の 1 つのサービス全体は以下のコードのように見えますが、グループを取り出して学生情報のみを残し、グループを独自の安らかな Web サービスに変えることを考えています。上記を達成しようとしているときに、あるサービスから別のサービスにどのように通信するのかわかりませんか?

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using System.IO;
using System.Net;
using System.Drawing;
using System.Runtime.Serialization;
using System.Linq;

namespace WcfServiceLibrary1
{
    [ServiceContract(Namespace = "http://tempuri.org")]
    [XmlSerializerFormat]
    public interface IReceiveData
    {
        #region DELETE
        [OperationContract]
        [WebInvoke(Method = "DELETE", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student/{studentID}")]
        void removeStudent(string studentID);
        #endregion

        #region POST
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Group")]
        void AddGroup(Group group);
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student")]
        void AddStudent(Student student);
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/AddStudentToGroup/{group}/{studentID}/{firstName}/{lastName}")]
        void AddStudentToGroup(string group, string studentID, string firstName, string lastName);
        #endregion

        #region GET
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "picture/{width}/{height}")]
        Stream GetImage(string width, string height);
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student")]
        List<Student> GetStudentCollection();
        [OperationContract] 
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Group")]
        List<Group> GetGroups();
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/GetStudentCollectionByGroup/{anything}")]
        List<Group> GetStudentCollectionByGroup(string anything);
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/GetGroupCollectionByStudent/{groupName}")]
        List<Student> GetGroupCollectionByStudent(string groupName);
        #endregion 
    }
    #region Data Contacts
    [DataContract(Name="Student")]
    public class Student
    {
        public Student()
        {
            StudentGroup = new List<Group>();
        }
        [DataMember(Name = "StudentID")]
        public string StudentID { get; set; }
        [DataMember(Name = "FirstName")]
        public string FirstName { get; set; }
        [DataMember(Name = "LastName")]
        public string LastName { get; set; }
        [DataMember(Name = "TimeAdded")]
        public DateTime TimeAdded;
        public string TimeAddedString
        {
            get
            {
                return this.TimeAdded.ToString("dd/MM/yyyy hh:mm:ss");
            }
        }
        public List<Group> StudentGroup { get; set; }
    }
    [DataContract(Name = "Group")]
    public class Group
    {
        public Group() 
        {
            Groupsz = new List<Student>();
        }
        [DataMember(Name = "GroupName")]
        public string GroupName { get; set; }
        public List<Student> Groupsz { get; set; }
    }
    #endregion 

    #region Service work
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
    public class Service : IService
    {
        #region Lists
        List<Student> students = new List<Student>();
        List<Group> Groups = new List<Group>();
        int eCount = 0;
        #endregion 

        #region Do Work
        public void AddStudent(Student student)
        {
            student.StudentID = (++eCount).ToString();
            student.TimeAdded = DateTime.Now;
            students.Add(student);
        }
        public void UpdateStudent(Student student) 
        {
            var findStudent = students.Where(s => s.StudentID == student.StudentID).FirstOrDefault();

            if (findStudent != null)
            {
                ///findStudent values with student values --- like
                findStudent.FirstName = student.FirstName;
                findStudent.LastName = student.LastName;
            }

        }
        public void AddGroup(Group group)
        {
            Groups.Add(group);
        }
        public void removeStudent(string studentID)
        {
            students.RemoveAll(s => s.StudentID == studentID);
            Groups.ForEach(g => g.Groupsz.RemoveAll(gs => gs.StudentID == studentID));
        }

    public void AddStudentToGroup(string group, string studentID, string firstName, string lastName)
    {
        var result = Groups.Where(n => String.Equals(n.GroupName, group)).FirstOrDefault();
        var result1 = students.Where(n => String.Equals(n.StudentID, studentID)).FirstOrDefault();
        if (result != null)
        {
            result.Groupsz.Add(new Student() { StudentID = studentID, FirstName = firstName, LastName = lastName });
        }
        if (result1 != null)
        {
            result1.StudentGroup.Add(new Group() { GroupName = group });
        }

    } 
        public List<Student> GetStudentCollection()
        {
            return students;
        }
        public List<Group> GetGroups()
        {
            return Groups;
        }
        public List<Student> GetGroupCollectionByStudent(string groupName)
        {
            List<Student> student = (from g in students
                                  where
                                      (from t in g.StudentGroup where t.GroupName == groupName select t).Count() > 0
                                  select g).ToList();
            return student;
        }
        public List<Group> GetStudentCollectionByGroup(string anything)
        {

            List<Group> groups = (from g in Groups
                                  where
                                      (from t in g.Groupsz where
                                              string.Equals(t.StudentID, anything, StringComparison.CurrentCultureIgnoreCase)
                                           || string.Equals(t.FirstName, anything, StringComparison.CurrentCultureIgnoreCase)
                                           || string.Equals(t.LastName, anything, StringComparison.CurrentCultureIgnoreCase)
                                       select t).Count() > 0
                                  select g).ToList();
            return groups;
        }

        #endregion
    }
}
    #endregion
4

0 に答える 0