My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Linq;
using System.Xml.Linq;
using NUnit.Framework;
using Sakila.ServiceModel.Version100.Operations.SakilaService;
using Sakila.ServiceModel.Version100.Types;
using ServiceStack.Common.Extensions;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceModel.Extensions;
using ServiceStack.UsageExamples.Support;
using ServiceStack.UsageExamples.Support.Translators;

namespace ServiceStack.UsageExamples
{
/// <summary>
/// Examples on accessing the service using the Service Model classes from ProductService.Service.Model.dll assembly.
/// DB Recommends this approach for accessing internally hosted services.
/// Since the Assembly only contains POCO dto classes, there is no risk of it becoming a c# fat client.
///
/// The <see cref="UsingDtoFromXsd">alternate approach</see> would be to:
/// use the XSD > To Generate DTO assembly > and use the generated classes instead,
/// which would require more code, time and effort.
/// </summary>
///
[TestFixture]
public class UsingDtoFromAssembly : TestBase
{
/// <summary>
/// Simple request, using xlinq to extract one field.
/// </summary>
[Test]
public void Get_customers_using_dto_from_assembly_and_parse_with_xlinq()
{
using (var client = new Soap12ServiceClient(base.WsSyncReplyUri))
{
var request = new GetCustomers { CustomerIds = new ArrayOfIntId { CustomerId } };
var response = client.Send(request);
var el = XNode.ReadFrom(response.GetReaderAtBodyContents()) as XElement;
var customers = el.AnyElement("Customers").AllElements("Customer").ToList();

Assert.AreEqual(1, customers.Count);
Assert.AreEqual(CustomerId, customers[0].GetInt("Id"));
}
}

/// <summary>
/// Simple request, using xlinq to extract one field.
/// </summary>
[Test]
public void Get_customers_using_dto_from_assembly_and_parse_with_xlinq_BasicHttp()
{
using (var client = new Soap11ServiceClient(base.BasicHttpSyncReplyUri))
{
var request = new GetCustomers { CustomerIds = new ArrayOfIntId { CustomerId } };
var response = client.Send(request);
var el = XNode.ReadFrom(response.GetReaderAtBodyContents()) as XElement;
var customers = el.AnyElement("Customers").AllElements("Customer").ToList();

Assert.AreEqual(1, customers.Count);
Assert.AreEqual(CustomerId, customers[0].GetInt("Id"));
}
}

/// <summary>
/// The DB Recommended way to parse a request.
/// Use xlinq to extraxt only the data you need directly into your application model.
/// </summary>
[Test]
public void Get_customers_using_dto_from_assembly_and_parse_all_with_xlinq()
{
using (var client = new Soap12ServiceClient(base.WsSyncReplyUri))
{
var request = new GetCustomers { CustomerIds = new ArrayOfIntId { CustomerId } };
var response = client.Send(request);
var el = XNode.ReadFrom(response.GetReaderAtBodyContents()) as XElement;

var customers = CustomerTranslator.Instance.ParseAll(el.AnyElement("Customers").AllElements("Customer")).ToList();

Assert.AreEqual(1, customers.Count);
Assert.AreEqual(CustomerId, customers[0].Id);
}
}

/// <summary>
/// Using deserialization to parse request.
///
/// Note: it is considered bad practice to use the deserialized DTO objects within your application.
/// Instead you should copy the data from the service dto into your application model.
/// </summary>
[Test]
public void Get_customers_using_dto_from_assembly_and_deserialize()
{
using (var client = new Soap12ServiceClient(base.WsSyncReplyUri))
{
var request = new GetCustomers { CustomerIds = new ArrayOfIntId { CustomerId } };
var response = client.Send(request);
var customersResponse = response.GetBody<GetCustomersResponse>();
Assert.AreEqual(1, customersResponse.Customers.Count);
Assert.AreEqual(CustomerId, customersResponse.Customers[0].Id);
}
}

}
}

Change log

r398 by demis.bellot on Oct 14, 2009   Diff
re add UsageExamples
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 4002 bytes, 103 lines
Powered by Google Project Hosting