मेरे पास एक एएसपी.नेट वेबएपी परियोजना है जिसका मैं काम कर रहा हूं। बॉस रिटर्न को "आंशिक प्रतिक्रिया" का समर्थन करने के लिए पसंद करेंगे, जिसका अर्थ है कि डेटा मॉडल में 50 फ़ील्ड हो सकते हैं, क्लाइंट प्रतिक्रिया के लिए विशिष्ट फ़ील्ड का अनुरोध करने में सक्षम होना चाहिए। इसका कारण यह है कि यदि वे उदाहरण के लिए एक सूची लागू कर रहे हैं तो उन्हें केवल 50 फ़ील्ड के ऊपरी हिस्से की आवश्यकता नहीं है, वे शायद सूची बनाने के लिए पहला नाम, अंतिम नाम और आईडी चाहते हैं। इस प्रकार मैंने एक कस्टम कॉन्ट्रैक्ट रिजॉल्वर (डायनेमिक कंट्रैक्ट रिसेल्वर) का उपयोग करके एक समाधान लागू किया है, जैसे कि जब कोई अनुरोध आता है तो मैं ऑनएक्शन एक्सेलिंग विधि में फ़िल्टर (फील्डलिस्ट फिल्टर) के माध्यम से इसमें देख रहा हूं और यह निर्धारित कर रहा हूं कि "फ़ील्डलिस्ट" नामक फ़ील्ड मौजूद है या नहीं और तो अगर यह है कि मैं अपने डायनामिक कंट्रैक्ट रिसेल्वर के एक नए उदाहरण के साथ वर्तमान अनुबंध रिसेल्वर को बदल रहा हूं और मैं कन्स्ट्रक्टर को फ़ील्डलिस्ट पास करता हूं।एएसपी.नेट वेबएपी और आंशिक प्रतिक्रिया
कुछ नमूना कोड
DynamicContractResolver.cs
protected override IList<JsonProperty> CreateProperties(Type type, Newtonsoft.Json.MemberSerialization memberSerialization)
{
List<String> fieldList = ConvertFieldStringToList();
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
if (fieldList.Count == 0)
{
return properties;
}
// If we have fields, check that FieldList is one of them.
if (!fieldList.Contains("FieldList"))
// If not then add it, FieldList must ALWAYS be a part of any non null field list.
fieldList.Add("FieldList");
if (!fieldList.Contains("Data"))
fieldList.Add("Data");
if (!fieldList.Contains("FilterText"))
fieldList.Add("FilterText");
if (!fieldList.Contains("PageNumber"))
fieldList.Add("PageNumber");
if (!fieldList.Contains("RecordsReturned"))
fieldList.Add("RecordsReturned");
if (!fieldList.Contains("RecordsFound"))
fieldList.Add("RecordsFound");
for (int ctr = properties.Count-1; ctr >= 0; ctr--)
{
foreach (string field in fieldList)
{
if (field.Trim() == properties[ctr].PropertyName)
{
goto Found;
}
}
System.Diagnostics.Debug.WriteLine("Remove Property at Index " + ctr + " Named: " + properties[ctr].PropertyName);
properties.RemoveAt(ctr);
// Exit point for the inner foreach. Nothing to do here.
Found: { }
}
return properties;
}
FieldListFilter.cs
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
// We need to determine if there is a FieldList property of the model that is being used.
// First get a reference to the model.
var modelObject = actionContext.ActionArguments.FirstOrDefault().Value;
string fieldList = string.Empty;
try
{
// Using reflection, attempt to get the value of the FieldList property
var fieldListTemp = modelObject.GetType().GetProperty("FieldList").GetValue(modelObject);
// If it is null then use an empty string
if (fieldListTemp != null)
{
fieldList = fieldListTemp.ToString();
}
}
catch (Exception)
{
fieldList = string.Empty;
}
// Update the global ContractResolver with the fieldList value but for efficiency only do it if they are not the same as the current ContractResolver.
if (((DynamicContractResolver)GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver).FieldList != fieldList)
{
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new DynamicContractResolver(fieldList);
}
}
मैं तो json सामग्री पेलोड जैसे देख के साथ एक अनुरोध भेज सकते हैं:
{
"FieldList":"NameFirst,NameLast,Id",
"Data":[
{
"Id":1234
},
{
"Id":1235
}
]
}
और मैं बहुत ऐसी ही प्रतिक्रिया प्राप्त होगा:
{
"FieldList":"NameFirst,NameLast,Id",
"Data":[
{
"NameFirst":"Brian",
"NameLast":"Mueller",
"Id":1234
},
{
"NameFirst":"Brian",
"NameLast":"Mueller",
"Id":1235
}
]
}
मुझे विश्वास है कि ContractResolver का उपयोग कर सूत्रण समस्या आने पर हो सकता है। यदि मैं इसे एक अनुरोध के लिए बदलता हूं तो यह तब तक सभी अनुरोधों के लिए मान्य होगा जब तक कि कोई इसे किसी अन्य अनुरोध पर नहीं बदलता (परीक्षण के माध्यम से ऐसा लगता है) यदि ऐसा है, तो मुझे अपने उद्देश्य के लिए उपयोगिता दिखाई नहीं दे रही है।
संक्षेप में, मैं गतिशील डेटा मॉडल रखने का एक तरीका ढूंढ रहा हूं जैसे कि अनुरोध से आउटपुट अनुरोध के आधार पर ग्राहक द्वारा कॉन्फ़िगर करने योग्य है। Google इसे अपने वेब एपीआई में लागू करता है और वे इसे "आंशिक प्रतिक्रिया" कहते हैं और यह बहुत अच्छा काम करता है। मेरा कार्यान्वयन एक बिंदु पर काम करता है लेकिन मुझे डर है कि यह एकाधिक एक साथ अनुरोधों के लिए टूटा जाएगा।
सुझाव? युक्तियाँ?
बस FYI ... जेसन फॉर्मेटर के लिए '$ select' सुविधा समर्थन की जांच करें जो अगली रिलीज में आ रहा है: https://aspnetwebstack.codeplex.com/wikipage?title=%24select%20and%20%24expand%20support&referringTitle = चश्मा –
मुझे नहीं लगता कि मेरे पास अगले संस्करण की प्रतीक्षा करने का समय है। हमारे पास 2 महीने से भी कम समय में एक डेमो है और मुझे लागू करने के लिए बहुत कुछ मिला है। बिंदु पर और, मेरी विधि मूल रूप से टूटा हुआ है। मैंने वेब के सुझावों और ट्यूटोरियल्स के आधार पर कार्यान्वित किया था, लेकिन मेरे पास इसकी नींव के बारे में कुछ सवाल हैं जो मैं पूरी तरह समझ नहीं पा रहा हूं। –
यदि आप प्रत्येक अनुरोध पर 'डायनामिक कंट्रैक्ट रिसेल्वर' का एक नया उदाहरण बनाते हैं और इसे 'कॉन्ट्रैक्ट रिसेल्वर' के रूप में उपयोग करते हैं, तो वहां एक सहमति समस्या नहीं होनी चाहिए। – muratgu