Thursday, April 21, 2016

Windows Communication service

This is the code we wrote in class. It does not include the code generated by the ADO Data Entities

here is the interface and data contract

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ISampleBookReviewService" in both code and config file together.
[ServiceContract]
public interface ISampleBookReviewService
{
    [OperationContract]
    List<string> GetAuthors();

    [OperationContract]
    List<BookLite> GetBooks(string authorName);
}

[DataContract]
public class BookLite
{
    [DataMember]
    public string Title { set; get; }

    [DataMember]
    public string ISBN { set; get; }

    [DataMember]
    public string AuthorName { set; get; }

    [DataMember]
    public DateTime EntryDate { set; get; }

}


Here is the service code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SampleBookReviewService" in code, svc and config file together.
public class SampleBookReviewService : ISampleBookReviewService
{
    BookReviewDbEntities db = new BookReviewDbEntities();
    public List<string> GetAuthors()
    {
        var auth = from a in db.Authors
                   orderby a.AuthorName
                   select new { a.AuthorName };
        List<string> authors = new List<string>();
        foreach (var au in auth)
        {
            authors.Add(au.AuthorName.ToString());
        }
        return authors;
    }

    public List<BookLite> GetBooks(string authorName)
    {
        var bks = from b in db.Books
                  from a in b.Authors
                  orderby b.BookTitle
                  where a.AuthorName.Equals(authorName)
                  select new {
                      b.BookTitle,
                      a.AuthorName,
                      b.BookISBN,
                      b.BookEntryDate
                  };
        List<BookLite> books = new List<BookLite>();

        foreach (var bk in bks)
        {
            BookLite bl = new BookLite();
            bl.Title = bk.BookTitle;
            bl.AuthorName = bk.AuthorName;
            bl.ISBN = bk.BookISBN;
            bl.EntryDate = bk.BookEntryDate;
            books.Add(bl);
        }
        return books;
    }
}

No comments:

Post a Comment