There is method GetFull
that should use OData return related entities.
When I call GetFull(12, "entity1,entity2")
then I will get to current object with id = 12 and related entity1 and entity2 (http://.../api/City/12?$expand=Region
)
When I call GetFull(12, "0")
then I want to automatical find and get all related entities from current object (http://.../api/City/12?$expand=Region,Country
). For that I'm using reflections.
How can I improve this method?
It's working but I'm sure that I implemented it not properly.
P.S : It was done for user-friendly link
before:http://.../api/City/12?$expand=Region,Country
now:http://.../api/City/12/GetFull
or http://.../api/City/12/GetFull/Region
public virtual HttpResponseMessage GetFull(int id, string entities)
{
string propertyList = string.Empty;
if (!entities.Equals("0"))
{
propertyList = entities;
}
else
{
foreach (var prop in typeof(T).GetProperties().Where(p => p.GetGetMethod().IsVirtual))
{
if (prop.PropertyType.IsClass && !prop.PropertyType.FullName.StartsWith("System."))
{
propertyList += prop.Name + ",";
}
}
propertyList = propertyList.Remove(propertyList.Length - 1);
}
string entityName = typeof(T).Name;
var baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority);
var response = Request.CreateResponse(HttpStatusCode.Found);
StringBuilder sb = new StringBuilder();
sb.Append(baseUrl);
sb.Append("/api/");
sb.Append(entityName);
sb.Append("/");
sb.Append(id);
sb.Append("?$expand=");
sb.Append(propertyList);
response.Headers.Location = new Uri(sb.ToString());
return response;
}
Sorry for my English and if I have to add more informations, please let me know. Thanks