Question

how i can use Self-Tracking Entities in WCF Services to implement ServiceContract

this my ServiceContract in IPbService.cs

namespace PhoneBookService
{
     [ServiceContract]
     public interface IPbService
     {
          [OperationContract]
          List<User> GetAllUser();

          [OperationContract]
          string AddUser(User user);

          [OperationContract]
          string DeleteUser(int id);

          [OperationContract]
          string UpdateUser(User user);

          [OperationContract]
          List<Contact> GetContactsByUser(int id);

          [OperationContract]
          string AddContact(Contact contact, string userName);

          [OperationContract]
          string DeleteContact(int id);

          [OperationContract]
          string UpdateContact(Contact contact);


     }
}

and this is my implemention class in PbService.svc.cs

namespace PhoneBookService
{
     public class PbService : IPbService
     {
          readonly PhoneBookDBEntities _context = new PhoneBookDBEntities();

          public List<User> GetAllUser()
          {
                return _context.Users.OrderBy(u => u.Name).ToList();
          }

          public string AddUser(User user)
          {
            try
            {
                _context.Users.AddObject(user);
                 _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }
          }

          public string DeleteUser(int id)
          {
            try
            {
                User user = _context.Users.FirstOrDefault(u => u.UserID == id);
                _context.Users.DeleteObject(user);
                _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }

          }

          public string UpdateUser(User user)
          {
            try
            {
                if (user == null) throw new ArgumentNullException("user");
                User oldUser = _context.Users.FirstOrDefault(u => u.UserID == user.UserID);
                if (oldUser != null)
                {
                    oldUser.Name = user.Name;
                    oldUser.Password = user.Password;
                    oldUser.UserName = user.UserName;
                    oldUser.Email = user.Email;
                    oldUser.CreationDate = DateTime.Now;
                }
                _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }

          }

          public List<Contact> GetContactsByUser(int id)
          {
                User user = _context.Users.FirstOrDefault(u => u.UserID == id);
                if (user == null) throw new ArgumentNullException("id");
                return user.Contacts.OrderBy(c=>c.Name).ToList();
          }

          public string AddContact(Contact contact, string userName)
          {
            try
            {
                User user = _context.Users.FirstOrDefault(u => u.UserName == userName);
                if (user != null) user.Contacts.Add(contact);
                _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }

          }

          public string DeleteContact(int id)
          {
            try
            {
                Contact contact = _context.Contacts.FirstOrDefault(c => c.ContactID == id);
                _context.Contacts.DeleteObject(contact);
                _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }
          }

          public string UpdateContact(Contact contact)
          {
            try
            {
                Contact oldContact = _context.Contacts.FirstOrDefault(c => c.ContactID == contact.ContactID);
                if (oldContact != null)
                {
                    oldContact.Name = contact.Name;
                    oldContact.PhoneNumber = contact.PhoneNumber;
                    oldContact.Email = contact.Email;
                    oldContact.Mobil = contact.Mobil;
                    oldContact.Address = contact.Address;
                }
                _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }
          }

     }
}

enter image description here

Was it helpful?

Solution

In Your WCF project you just need to reference your EntityClasses project. That simple?! I guess so...

Your Self-Tracking Entities are already equipped with the right DataMember attributes for the properties it carries.

Another thing...I see you use distinct methods for Add, Update and Delete. I always use a single Persist method that something goes like this:

using(Entities context = new Entities())
{
  try
  {
    context.ApplyChanges(user);
    context.SaveChanges();
  }
  catch
  {
    ...
  }
}

The Self-Tracking Entities context "knows" how to apply changes made to entities based on the ChangeTracker the STE is carrying. So you don't need seperate methods at all. Very easy to maintain.

So when you create a new entity in some client-application the ChangeTracker.State will be ObjectChangeTrackerState.Add and when modifying an existing entity it will be Modified and Deleted when you use entity.MarkAsDeleted().

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top