patterncsharpMinor
Simplifying a web service method
Viewed 0 times
servicemethodsimplifyingweb
Problem
I have the following method in a web service class. I'm a little unhappy about the big block of
```
public string UserCatalog(string numericSessionId, JObject incomingRequestJson)
{
JObject json = new JObject();
JObject returningJson = new JObject();
JArray userCatalogArray = new JArray();
string deviceId = incomingRequestJson.SelectToken("deviceId", true).ToString();
string version = getOptionalData(incomingRequestJson, "version", "1.0.0");
requireMinVersion(incomingRequestJson);
IEnumerable catalog = readerBLL.UserCatalog(numericSessionId, deviceId);
var publicKeyRSA = readerTools.GetPublicKey(HttpUtility.HtmlDecode(numericSessionId), HttpUtility.HtmlDecode(version));
//Build the userCatalog json array
foreach (Bookcard card in catalog)
{
JObject arrayEntry = new JObject(
new JProperty("bookThumbnailUrl", card.bookThumbnailUrl),
new JProperty("bookId", card.bookId),
new JProperty("bookTitle", card.bookTitle),
new JProperty("titlePrefix", card.titlePrefix),
new JProperty("author", card.author),
new JProperty("annotation", card.annotation),
new JProperty("publisher", card.publisher),
new JProperty("numPages", card.numPages),
new JProperty("returnDate", card.returnDate),
new JProperty("downloaded", card.downloaded),
new JProperty("deviceId", card.deviceId),
new JProperty("currentPageLabel", card.currentPageLabel),
new JProperty("furthestPageLabel", card.furthestPageLabel),
new JProperty("currentReadPosition", card.currentReadPosition),
new JProperty("furthestReadPosition", card.furthestReadPosition),
new JProperty("lastReadTimestamp", c
new JProperty(...) calls in the for loop. Is there a way to simplify that?```
public string UserCatalog(string numericSessionId, JObject incomingRequestJson)
{
JObject json = new JObject();
JObject returningJson = new JObject();
JArray userCatalogArray = new JArray();
string deviceId = incomingRequestJson.SelectToken("deviceId", true).ToString();
string version = getOptionalData(incomingRequestJson, "version", "1.0.0");
requireMinVersion(incomingRequestJson);
IEnumerable catalog = readerBLL.UserCatalog(numericSessionId, deviceId);
var publicKeyRSA = readerTools.GetPublicKey(HttpUtility.HtmlDecode(numericSessionId), HttpUtility.HtmlDecode(version));
//Build the userCatalog json array
foreach (Bookcard card in catalog)
{
JObject arrayEntry = new JObject(
new JProperty("bookThumbnailUrl", card.bookThumbnailUrl),
new JProperty("bookId", card.bookId),
new JProperty("bookTitle", card.bookTitle),
new JProperty("titlePrefix", card.titlePrefix),
new JProperty("author", card.author),
new JProperty("annotation", card.annotation),
new JProperty("publisher", card.publisher),
new JProperty("numPages", card.numPages),
new JProperty("returnDate", card.returnDate),
new JProperty("downloaded", card.downloaded),
new JProperty("deviceId", card.deviceId),
new JProperty("currentPageLabel", card.currentPageLabel),
new JProperty("furthestPageLabel", card.furthestPageLabel),
new JProperty("currentReadPosition", card.currentReadPosition),
new JProperty("furthestReadPosition", card.furthestReadPosition),
new JProperty("lastReadTimestamp", c
Solution
It is not clear (since you don't show the definition of card) however if all you are doing is creating properties for each public property of card then try this:
You might need a few more lines to add or remove additional properties based on your business rules. But this would cut down on a lot of the clutter.
JObject arrayEntry = JObject.FromObject(card);You might need a few more lines to add or remove additional properties based on your business rules. But this would cut down on a lot of the clutter.
Code Snippets
JObject arrayEntry = JObject.FromObject(card);Context
StackExchange Code Review Q#46217, answer score: 4
Revisions (0)
No revisions yet.