Click here to Skip to main content
14,391,058 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
 
GeneralRe: Sound of the Week Pin
Sander Rossel7-Sep-19 2:02
professionalSander Rossel7-Sep-19 2:02 
GeneralRe: Sound of the Week Pin
phil.o7-Sep-19 2:15
mvephil.o7-Sep-19 2:15 
GeneralRe: Sound of the Week Pin
Slacker0076-Sep-19 1:35
professionalSlacker0076-Sep-19 1:35 
GeneralRe: Sound of the Week Pin
Sander Rossel6-Sep-19 2:01
professionalSander Rossel6-Sep-19 2:01 
GeneralRe: Sound of the Week Pin
User 48350476-Sep-19 5:19
MemberUser 48350476-Sep-19 5:19 
GeneralRe: Sound of the Week Pin
Sander Rossel7-Sep-19 1:48
professionalSander Rossel7-Sep-19 1:48 
GeneralRe: Sound of the Week Pin
Tachyonx7-Sep-19 6:58
MemberTachyonx7-Sep-19 6:58 
Generali love JSON. i'm not sure the relationship is healthy though Pin
honey the codewitch5-Sep-19 20:22
Memberhoney the codewitch5-Sep-19 20:22 
It's a torrid affair. I've begun using JSON to back entity objects and after doing so I don't think I'm ever going back.

Jeez it's cool tho.

I'm addicted. Maybe I need help.

This is just exciting.

At the risk of dropping some code here:

an example of one (read-only in this case, but adding writing is doable too)

public abstract class TmdbEntity : IEquatable<TmdbEntity>
{
	protected TmdbEntity(IDictionary<string, object> json)
	{
		Json = json ?? throw new ArgumentNullException(nameof(json));
	}
		
	public IDictionary<string, object> Json { get; protected set; }

		
	protected T GetField<T>(string name,T @default=default(T))
	{
		object o;
		if (Json.TryGetValue(name, out o) && o is T)
			return (T)o;
		return @default;
	}
	// objects are considered equal if they
	// point to the same actual json reference
	public bool Equals(TmdbEntity rhs)
	{
		if (ReferenceEquals(this, rhs))
			return true;
		if (ReferenceEquals(rhs, null))
			return false;
		return ReferenceEquals(Json, rhs.Json);
	}
	public override bool Equals(object obj)
	{
		return Equals(obj as TmdbEntity);
	}
	public static bool operator==(TmdbEntity lhs, TmdbEntity rhs)
	{
		return lhs.Equals(rhs);
	}
	public static bool operator!=(TmdbEntity lhs, TmdbEntity rhs)
	{
		return !lhs.Equals(rhs);
	}
	public override int GetHashCode()
	{
		// we need the actual base hashcode, not the one
		// computed by the JsonObject.
		var jo = Json as JsonObject; // should always be but it doesn't *have* to be
		if(null!=jo)
		{
			jo.BaseDictionary.GetHashCode();
		}
		return Json.GetHashCode();
	}
}


Then in your derived classes:
public class TmdbLanguage : TmdbEntity
{
	public TmdbLanguage(IDictionary<string,object> json) : base(json)
	{
	}
	public string Name => GetField<string>("name");
	public string EnglishName => GetField<string>("english_name");
	public string Iso => GetField<string>("iso_639_1");

}


Then on any entity you can also get the Json property to get all of the actual data for your object as one unit, which you can then query on. This makes it better than traditional entities that consume and toss or otherwise hide the underlying data structure, frankly. It's not only more "pure" to back your state with the actual data you got, it's also easier to query on it or update it. By query it i mean do like "JsonObject.Select(myEntity.Json,"$.id")" if you want or "myEntity.Id"

So, do I need help?
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

JokeRe: i love JSON. i'm not sure the relationship is healthy though Pin
Nelek5-Sep-19 20:33
protectorNelek5-Sep-19 20:33 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
honey the codewitch5-Sep-19 20:35
Memberhoney the codewitch5-Sep-19 20:35 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
Jörgen Andersson5-Sep-19 22:21
professionalJörgen Andersson5-Sep-19 22:21 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
honey the codewitch5-Sep-19 22:35
Memberhoney the codewitch5-Sep-19 22:35 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
Jörgen Andersson6-Sep-19 0:25
professionalJörgen Andersson6-Sep-19 0:25 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
honey the codewitch6-Sep-19 0:28
Memberhoney the codewitch6-Sep-19 0:28 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
Jörgen Andersson6-Sep-19 0:36
professionalJörgen Andersson6-Sep-19 0:36 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
honey the codewitch6-Sep-19 0:38
Memberhoney the codewitch6-Sep-19 0:38 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
Sander Rossel5-Sep-19 21:37
professionalSander Rossel5-Sep-19 21:37 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
honey the codewitch5-Sep-19 21:43
Memberhoney the codewitch5-Sep-19 21:43 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
Slacker0075-Sep-19 22:44
professionalSlacker0075-Sep-19 22:44 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
honey the codewitch6-Sep-19 0:36
Memberhoney the codewitch6-Sep-19 0:36 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
Slacker0076-Sep-19 1:31
professionalSlacker0076-Sep-19 1:31 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
honey the codewitch6-Sep-19 1:34
Memberhoney the codewitch6-Sep-19 1:34 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
Slacker0076-Sep-19 1:40
professionalSlacker0076-Sep-19 1:40 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
honey the codewitch6-Sep-19 1:40
Memberhoney the codewitch6-Sep-19 1:40 
GeneralRe: i love JSON. i'm not sure the relationship is healthy though Pin
Slacker0076-Sep-19 1:43
professionalSlacker0076-Sep-19 1:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.