(Real-Life Example: API Models में JSON Property Names, Validation Attributes)”**
C# में Attributes वो फीचर है जो आपके code को
extra power, extra information और extra behaviour देता है —
बिना logic को बदले।
Attributes =
Code के ऊपर लगाया गया छोटा-सा tag
जो बताता है कि यह class/property/method का use कैसे होना चाहिए।
Real-life में attributes बहुत ज़रूरी हैं:
API models
JSON mapping
Validation rules
Database mapping (Entity Framework)
Logging
Reflection-based systems
GPS servers में packet mapping
आज attributes को सबसे आसान तरीके + real-life API & GPS examples से समझते हैं।
Attribute =
Code के बारे में extra जानकारी
जो runtime या compiler को बताती है कि इसे कैसे handle किया जाए।
Example:
[Obsolete]
इसका मतलब method पुरानी हो चुकी है।
[AttributeName]
class MyClass { }
Attribute हमेशा [] brackets में होता है।
✔ [JsonPropertyName]
✔ [JsonProperty] (Newtonsoft)
✔ [Required]
✔ [MaxLength]
✔ [Table], [Key] (Entity Framework)
✔ [Obsolete]
✔ [Serializable]
इनसे आपका code ज्यादा smart बनता है।
Suppose API output में आपको:
{
"lat": 26.85,
"lon": 80.94
}
नाम छोटा चाहिए लेकिन class में नाम meaningful चाहिए।
class GPSDevice
{
[JsonPropertyName("lat")]
public double Latitude { get; set; }
[JsonPropertyName("lon")]
public double Longitude { get; set; }
}
Output JSON:
{
"lat": 26.85,
"lon": 80.94
}
Perfect clean mapping!
अगर Newtonsoft use कर रहे हो:
[JsonProperty("lat")]
public double Latitude { get; set; }
GPS packet:
IMEI=861128068064267;BAT=80;SIG=4;
Class:
class GPSDeviceInfo
{
[JsonProperty("imei")]
public string IMEI { get; set; }
[JsonProperty("bat")]
public int Battery { get; set; }
[JsonProperty("sig")]
public int Signal { get; set; }
}
Now serialize → clean JSON mapping!
Real-life APIs में user गलत data भेज सकता है।
Example Model:
class Student
{
[Required]
public string Name { get; set; }
[Range(1, 120)]
public int Age { get; set; }
[MaxLength(20)]
public string City { get; set; }
}
अगर गलत data भेजा →
API automatically error दे देगी।
class User
{
[Key]
public int UserId { get; set; }
[Required]
[MaxLength(50)]
public string Username { get; set; }
[Table("UserInfo")]
public class UserInfo { }
}
Attributes से database tables linked हो जाते हैं।
[Obsolete] — पुरानी method पर Warning[Obsolete("Use NewMethod instead")]
void OldMethod() { }
अब developer जब OldMethod call करेगा →
warning आएगी।
आप खुद भी attributes बना सकते हो:
[AttributeUsage(AttributeTargets.Method)]
class LogAttribute : Attribute
{
public string Message { get; }
public LogAttribute(string msg) { Message = msg; }
}
Use:
[Log("This method does important work")]
void Process() { }
GPS servers में custom attributes code को ज़्यादा structured बनाते हैं।
आप अलग-अलग packet types के लिए annotation लगा सकते हो:
[Packet("LOCATION")]
void HandleLocation(string pkt) { }
[Packet("ALARM")]
void HandleAlarm(string pkt) { }
अब reflection से:
RunHandlerBasedOnPacketType("LOCATION");
Ultra-clean design!
C# Attributes code को बनाते हैं:
✔ Smart
✔ Clean
✔ Structured
✔ API-friendly
✔ Database-friendly
✔ Developer-friendly
GPS servers, APIs, billing software, ERPs —
हर जगह attributes powerful role निभाते हैं।
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav