C
C#2y ago
Shemiroth

Can I have a `ListThingT ` where T is different for each item?

Can I have a List<Thing<T>> where T is different for each item?
37 Replies
Scratch
Scratch2y ago
No. I always think of this problem this way, how do you plan on using the objects in the list? Is there common functionality of Thing that doesn't depend on T? If so, there should be a non-generic Thing base class
toddlahakbar
toddlahakbar2y ago
Nope, why do you think you need it?
Shemiroth
Shemiroth2y ago
I want the user to be able to create objects and the properties that will be on them. So I might create an object called recipe and give it properties of string difficulty, double time etc then I could also create another thing called Ingredient with it's own properties and make that a property on the Recipe thing
toddlahakbar
toddlahakbar2y ago
You want those definable by the user at runtime?
Shemiroth
Shemiroth2y ago
Yeah
toddlahakbar
toddlahakbar2y ago
So you want to replicate the type system at runtime. An oft desired thing.
Shemiroth
Shemiroth2y ago
Yeah basically I just want to create a skeleton of an app that let users define their own entities to track.
Binto86
Binto862y ago
Well you can always use object Plus maybe store the type of the object in the list too, so you can cast it
toddlahakbar
toddlahakbar2y ago
It's not an easy thing to do in c# and there's no easy solutions for anything when you're trying to essentially map the type system onto a db
Shemiroth
Shemiroth2y ago
Well I just thought if it's going in a DB then probably everything will just be stored as strings. So type will be a string like "double"/"number" and the value would be "1.5" or something. I'll just have some methods for getting back the value as a double.
toddlahakbar
toddlahakbar2y ago
Your going to have to store much more than just the value, even for numerics.
1.054 means different things in different locales
Shemiroth
Shemiroth2y ago
That's fine 😄 it's just going to be for me really. Maybe some people I know might use it. I just want to make one app for managing all things in my life without having to write code for them all 😄
toddlahakbar
toddlahakbar2y ago
Don't we all
Doombox
Doombox2y ago
Dictionary<Type, List<object>> <a:gpa_pepe_kekno:985632411629010944>
Shemiroth
Shemiroth2y ago
I've been playing with an app called Home assistant recently and it has dynamic entities
toddlahakbar
toddlahakbar2y ago
It's like the first time you think you want to individually track every value in the database
Doombox
Doombox2y ago
as long as you can define a reasonable enough API through interfaces and what you're trying to do isn't too insane List<object> and pattern matching is somewhat viable but yeah generally don't do that
Binto86
Binto862y ago
Just use json probably, just serilize/deserilize easy
Shemiroth
Shemiroth2y ago
That would make things like storing a Thing as a property easier yeah
Binto86
Binto862y ago
Well as you need to store your object, just use JavaScript object notation (aka. json) as it is literaly made for storing objects
Shemiroth
Shemiroth2y ago
better to use something like MongoDb for that?
Binto86
Binto862y ago
Isn't mongodb just db that stores JSONs?
Shemiroth
Shemiroth2y ago
Yeah So sounds perfect right?
Binto86
Binto862y ago
So yes thats something i would propose
Shemiroth
Shemiroth2y ago
Nice, finally a change from SQL server 😄
Binto86
Binto862y ago
Well im pretty sure this is the easyest part of the project 😅 Btw are you going to make this opensource? I would like to look at the code/maybe contribute
Shemiroth
Shemiroth2y ago
Yeah I guess if I actually get anywhere with it. I pretty much start a new project every week these days 😄
Binto86
Binto862y ago
Wait as i think about it you maybe don't need to actually create objects, you can probably just do with parsing strings, as you just want to read/write properties on the entities right?
Shemiroth
Shemiroth2y ago
I want the user to be able to create their own entity types and the properties.
Binto86
Binto862y ago
Ok i might have idea for clean solution: you would need something like this for template of certain entity, for example ingredience:
{
"name":"ingredience for cooking",
"id":"ingredience",
"properties":[
{"Name":"Ingredience Name","DataType":"string"}
]
}
{
"name":"ingredience for cooking",
"id":"ingredience",
"properties":[
{"Name":"Ingredience Name","DataType":"string"}
]
}
And if you reference your type somewhere, you just fill the id of the entity you want there in the datatype field instead of primitive type
Shemiroth
Shemiroth2y ago
Yeah that's the kind of thing I am thinking too
Binto86
Binto862y ago
Than for the actual objects just take the properties and let user fill in the values (you will need to keep the id somewhere so you know what type are you dealing with)
Shemiroth
Shemiroth2y ago
Sounds good. I am off to bed now so will have a look some more tomorrow. Thanks for your advice 🙂
Binto86
Binto862y ago
Np im going to bed too One thing to consider is that if the code is going to run on users device, it probably is easyer to store it in plain file/files
Shemiroth
Shemiroth2y ago
True, not much need for a server and db
Binto86
Binto862y ago
Yep this is the kind of thing where i think that having the files stored on client side is better and much much easier