1: public class ContactData : IContactData
2: {
3: private Proxy simpleDBProxy;
4: public Proxy SimpleDBProxy
5: {
6: get
7: {
8: if (simpleDBProxy==null)
9: {
10: simpleDBProxy = new Proxy();
11: }
12: return simpleDBProxy;
13: }
14: set
15: {
16: simpleDBProxy = value;
17: }
18: }
19: const string DomainName = "Contacts";
20:
21: public Contacts GetContacts()
22: {
23: Contacts myContacts = new Contacts();
24:
25: SelectRequest request = new SelectRequest
26: {
27: SelectExpression = string.Format("SELECT * FROM {0} ", DomainName)
28: };
29: SelectResponse response = SimpleDBProxy.Service.Select(request);
30:
31: var contacts = from item in response.SelectResult.Item
32: select new Contact()
33: {
34: Email = item.Attribute.GetValueByName("Email"),
35: Name = item.Attribute.GetValueByName("Name"),
36: Phone = item.Attribute.GetValueByName("Phone"),
37: ID = item.Name
38: };
39: myContacts.AddRange(contacts);
40: return myContacts;
41: }
42:
43: public Contacts GetContactsByName(string contactName)
44: {
45: Contacts myContacts = new Contacts();
46:
47: SelectRequest request = new SelectRequest
48: {
49: SelectExpression = string.Format("SELECT * FROM {0} where Name='{1}' ", DomainName, contactName)
50: };
51: SelectResponse response = SimpleDBProxy.Service.Select(request);
52:
53: var contacts = from item in response.SelectResult.Item
54: select new Contact()
55: {
56: Email = item.Attribute.GetValueByName("Email"),
57: Name = item.Attribute.GetValueByName("Name"),
58: Phone = item.Attribute.GetValueByName("Phone"),
59: ID = item.Name
60: };
61: myContacts.AddRange(contacts);
62: return myContacts;
63: }
64:
65: public bool SaveContact(Contact contact)
66: {
67: List<ReplaceableAttribute> attributeList = new List<ReplaceableAttribute>
68: {
69: new ReplaceableAttribute().WithName("Email").WithValue(contact.Email),
70: new ReplaceableAttribute().WithName("Name").WithValue(contact.Name),
71: new ReplaceableAttribute().WithName("Phone").WithValue(contact.Phone)
72: };
73: contact.ID = Guid.NewGuid().ToString();
74: bool success = false;
75: try
76: {
77: if (!SimpleDBProxy.Domains.Contains(DomainName))
78: {
79: SimpleDBProxy.AddDomain(DomainName);
80: }
81: PutAttributesRequest action = new PutAttributesRequest
82: {
83: ItemName = contact.ID,
84: Attribute = attributeList,
85: DomainName = DomainName
86: };
87: PutAttributesResponse response = SimpleDBProxy.Service.PutAttributes(action);
88: success = true;
89: }
90: catch (Exception requestException)
91: {
92: success = false;
93: }
94:
95: return success;
96: }
97: }