0

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

Tarek Saied
  • 6,482
  • 20
  • 67
  • 111
  • The PbModel.Context.tt suggests you already applied the ADO.net Self-tracking Entity Generator, so can we assume that your question is about how to correctly use these entities? – Gert Arnold May 21 '12 at 12:12
  • @GertArnold yes i asked about how i can use self-tracking in PbService.svc.cs file.. to re-implement those methods – Tarek Saied May 21 '12 at 12:28

1 Answers1

2

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.

Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
  • thanks @YoupTube how i can use ApplyChanges(user) instead my methods?just like your code?? – Tarek Saied May 21 '12 at 16:21
  • 1
    @tarek11011 yeah just like my code. The context knows what to do with the Self Tracking Entities whether when you apply changes, if they need to be inserted (State = Added), updated (State = Modified) or deleted (state = Deleted). This info is maintained in the ChangeTracker of the Entity. – Youp Bernoulli May 22 '12 at 10:22