2

I am just curious whether or not this can be achieved: I have currently got a sort of problem where I am using controllers to set and get data inside a Class and this is done through methods and instances on the handler side.

I am just wondering, because doing public string x { get; set; } can become very long winded for each property that your project consists of.

Is there a way that I can achieve this sort of ideology inside a Class?

public core Array[] (
          Option1 => string Array[] (
             Name => 'example'
          ),
          Option2 => String Array[] (
             Name => 'example2'
          ) { set; get; }
);

Of course, this is just a theory and won't be the exact solution. I am wondering if I'd:

1) Need a controller when appending data to the index's.
2) Need to instance the Class that the Controller handles or if I can do it through Main Class methods.
3) Need a multidimensional Array or List.

My current solution is to long winded and due to the large amount of Data the Core site uses, It's response is descending for every feature being added.

Could anyone reference any infrastructure references or possibly give a walk through on how to actually allocate the properties using this ideology?

Thank-you in advance.

Edit: I mean something like this (PHP):

$example = array (
    'location1' => array(
        'location_id' => 1
    ),
    'location2' => array(
        'location_id' => 2
    )
);

Now the data can be handled easier by:

foreach($example as $k=>$v){ // todo: handle }

So here $k becomes your array name (ie it could be, Name, Address) and $v becomes your nested array making data so much easier to handle.

14
  • 2
    type prop then press tab Commented Mar 3, 2016 at 13:24
  • I am new to C#, I apologize if this Question isn't in the best of a description. I am not understanding what your comment is referring to? Thanks for your input however, @dotctor Commented Mar 3, 2016 at 13:26
  • 2
    @KyleE4K: "Shorter code" and "simpler code" aren't always synonymous. That does make sense in PHP but not so much in C#, being a very different language. Commented Mar 3, 2016 at 13:32
  • 2
    @KyleE4K: Properties themselves and the types of those properties are two different things. And using untyped arrays to store class structures is a famously bad idea in languages like C# and Java. There exist refactoring patterns specifically to move away from doing that. Commented Mar 3, 2016 at 13:43
  • 1
    @KyleE4K: It doesn't really answer the question, and I suspect it's because the question is just off the mark of what's really being addressed. Presumably you have some code in C# which you don't think is "clean" or maintainable in some way, and you want to make it "better". You've assumed one way to do it, based on a different language, but that way doesn't make much sense here. While code reviews themselves are off-topic here, you might find some help refactoring C# code from things like Martin Fowler's Refactoring Patterns, or Robert Martin's "Clean Code" book. Commented Mar 3, 2016 at 13:53

2 Answers 2

4

While I strongly disagree with the usage of this pattern, I still think it's valuable to know.

I think you are looking for a Dictionary<TKey, TValue>. It provides a way to map keys of any type to values of any type. For your use case:

IDictionary<string, string> DynamicProperties {get; set;} = new Dictionary<string, string>
{
    { "FirstName", "John" },
    { "LastName", "Doe" }
};

You can then iterate over your "properties" with a loop:

foreach(KeyValuePair pair in DynamicProperties)
{
    string key = pair.Key; // "FirstName", "LastName"
    string value = pair.Value; // "John", "Doe"
    // Use them as you wish.
}

You can have dictionaries of dictionaries too. To match your updated example:

IDictionary<string, IDictionary<string, int>> Example {get; set;} = new Dictionary<string, IDictionary<string, int>>
{
    {"location1", new Dictionary<string, int> {{"location_id", 1}}},
    {"location2", new Dictionary<string, int> {{"location_id", 2}}}
};

But look at this code - you were looking for simplicity. This is not simple at all, nor is it short, clear, or testable. Having classes and properties is the way to go in C#.

I think the root of the problem here is that you are coding with C#, but thinking with PHP ideas. C# is strongly typed, while PHP is weakly typed, (see this wiki article), and you need to readjust your thinking appropriately.

Sign up to request clarification or add additional context in comments.

6 Comments

See, I've spent 4 years in PHP. Recently got an apprenticeship in C# and its just so crazy seeing how much longer C# is.. I still don't understand why people are not using Hack because that uses, like you say, a weakly typed language but has the optimized performance of a C language. These C# Controllers, Handlers and Instances are so confusing to get at :/ Thanks for helping me understand though!
@KyleE4K Longer isn't necessarily worse, writing the most compact code shouldn't be a goal, writing the most maintainable and readable code should be. Lots of languages have different ideologies, you can see it like: When in Rome, do as Romans do...
Gediminas, wouldn't it make more sense to set the dictionary as IDictionary<string, object> seeing that not only strings go in there? And then it can be easy to add a new Dictionary<string,object> as a value for any properties? Although I highly agree on your critical remarks ;)
This does what all that does and is maintainable and readable: pastebin.com/xHZDH3h4 @Icepickle convert that to Hack and your performance is boosted and page load is average 2/3seconds. This C# is so confusing xD but thanks! - sometimes the slowest don't always win the race :)
@Icepickle I wouldn't say so, as then you would need to cast the object back to the original type whenever you wish to use the values stored. Now, you could consider IDictionary<string, dynamic>... But let's not.
|
0

I suggest to look how ViewBag used in MVC works

here a good link

How ViewBag in ASP.NET MVC works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.