Known Types in WCF

Total Page:16

File Type:pdf, Size:1020Kb

Known Types in WCF

Known Types in WCF

 Continuing with the older scenario in which we simply saved or retrieve students from an online portal which was connected to a server. We assumed that we have two types of enrolled students 4 year bachelor program students and the short course diplomas students. As both of the types have the same base class “Student”, now we want to access the types of the student by their types rather than by there base class, as they both are inherited from the same class. Now we want to pass(save) and access(get) the objects of 4year program and Diploma program students by their types. We will do it programmatically by using KnownType Attributes.

 Before staring we need to update our database as we want to know the student type and Credits (CGPA) and their Hour Worked along with the previous data of our student. We will alter the student table and the procedures of our database. Alter table Student Add StudentType int, SemesterCredits int, AnnualCredits int, HoursWorked int

Alter procedure spGetStudent @Id int as Begin Select Id, Name, Department, DateOfBirth, StudentType, AnnualSalary, HourlyPay, HoursWorked from tblStudent where Id = @Id End

Alter procedure spSaveStudent @Id int, @Name nvarchar(50), @Department nvarchar(50), @DateOfBirth DateTime, @StudentType int, @SemesterCreditsint = null, @AnnualCreditsint = null, @HoursWorked int = null as Begin Insert into Student values (@Id, @Name, @Department, @DateOfBirth, @StudentType, @AnnualSalary, @HourlyPay, @HoursWorked) End

 Add a Class file to the Student Service project and name it as 4YearProgram i.e the type of our student.

4YearsProgram.cs namespace StudentService : { public class 4YearsProgram : Student { public int SemesterCredits{ get; set; } } }

 Add a class file of DiplomaProgram and Enter the following code

DiplomaProgram.cs namespace StudentService { public class DiplomaProgram : Student { public int AnnualCredits{ get; set; } public int HoursWorked { get; set; } } }

 Now We need to introduce the type property to our student class which will identify which type of student i.e 4year program or diploma program. Now we need to associate these Types as known types which we will mention in our student.cs as student is our base class we will do it by using the keyword KnowType

Student.cs using System; using System.Runtime.Serialization;

namespace StudentService { [KnownType(typeof(4YearsProgram))] [KnownType(typeof(DiplomaProgram))] [DataContract(Namespace = "http://pragimtech.com/Student")] public class Student { private int _id; private string _name; private string _Department; private DateTime _dateOfBirth;

[DataMember(Order = 1)] public int Id { get { return _id; } set { _id = value; } }

[DataMember(Order = 2)] public string Name { get { return _name; } set { _name = value; } }

[DataMember(Order = 3)] public string Department { get { return _Department; } set { _Department = value; } }

[DataMember(Order = 4)] public DateTime DateOfBirth { get { return _dateOfBirth; } set { _dateOfBirth = value; } }

[DataMember(Order = 5)] public StudentType Type { get; set; } }

public enum StudentType { 4YearsProgram = 1, DiplomaProgram = 2 } }

 Now as we want to access the student by its type rather than through it base class student we will have to built-up logic to check either it is a 4year program student or a diploma student. We need to build up that logic in the studentService class. We don’t need the employee object so we will update it to null. StudentService.cs using System; using System.Data; using System.Data.SqlClient; using System.Configuration;

namespace StudentService { public class StudentService : IStudentService { public Student GetStudent(int Id) { Student Student = null; string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spGetStudent", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter parameterId = new SqlParameter(); parameterId.ParameterName = "@Id"; parameterId.Value = Id; cmd.Parameters.Add(parameterId); con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { if ((StudentType)reader["StudentType"] == StudentType.4YearsProgram) { Student = new 4YearsProgram { Id = Convert.ToInt32(reader["Id"]), Name = reader["Name"].ToString(), Department = reader["Department"].ToString(), DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]), Type = StudentType.4YearsProgram, SemesterCredits= Convert.ToInt32(reader["AnnualSalary"]) }; } else { Student = new DiplomaProgram { Id = Convert.ToInt32(reader["Id"]), Name = reader["Name"].ToString(), Department = reader["Department"].ToString(), DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]), Type = StudentType.DiplomaProgram, AnnualCredits= Convert.ToInt32(reader["HourlyPay"]), HoursWorked = Convert.ToInt32(reader["HoursWorked"]), }; } } } return Student; } public void SaveStudent(Student Student) { string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spSaveStudent", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter parameterId = new SqlParameter { ParameterName = "@Id", Value = Student.Id }; cmd.Parameters.Add(parameterId);

SqlParameter parameterName = new SqlParameter { ParameterName = "@Name", Value = Student.Name }; cmd.Parameters.Add(parameterName);

SqlParameter parameterDepartment = new SqlParameter { ParameterName = "@Department", Value = Student.Department }; cmd.Parameters.Add(parameterDepartment);

SqlParameter parameterDateOfBirth = new SqlParameter { ParameterName = "@DateOfBirth", Value = Student.DateOfBirth }; cmd.Parameters.Add(parameterDateOfBirth);

SqlParameter parameterStudentType = new SqlParameter { ParameterName = "@StudentType", Value = Student.Type }; cmd.Parameters.Add(parameterStudentType); if (Student.GetType() == typeof(4YearsProgram)) { SqlParameter parameter SemesterCredits= new SqlParameter { ParameterName = "@SemesterCredits", Value = ((4YearsProgram)Student). SemesterCredits }; cmd.Parameters.Add(parameter SemesterCredits); } else { SqlParameter parameter AnnualCredits= new SqlParameter { ParameterName = "@ AnnualCredits ", Value = ((DiplomaProgram)Student). AnnualCredits }; cmd.Parameters.Add(parameter AnnualCredits);

SqlParameter parameterHoursWorked = new SqlParameter { ParameterName = "@HoursWorked", Value = ((DiplomaProgram)Student).HoursWorked }; cmd.Parameters.Add(parameterHoursWorked); }

con.Open(); cmd.ExecuteNonQuery(); } } } }

Recommended publications