Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle who care about creating, delivering, and maintaining software responsibly. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a save game editor. It uses a library that can read the file and return it in a form of an object. Within that object is an ItemSave object.

Also, to be able to interpret the data in the ItemSave object, I need more info (from an xml) which I get by a method in my own code that returns the ItemData object. They have all different fields, except of the "id" field. The id field is used to make the connection between the two.

For each ItemData variant (there is about 1500 of those, might expand, out of my control) I can have numerous ItemSave object (eg. a gun will always be called a gun and will always have clip size of 10, but number of bullets loaded may vary, etc).

As you can see, one class is pretty useless without the other, so I'd like to combine them.

Currently, I have an ItemBinder class that holds the reference to both ItemData and ItemSave and also has fields of both of them to flatten it out (So I can access data with ItemBinder.Value, not ItemBinder.ItemSave.Value). However, this is bad code and is hard to maintain.

I could drop all the fields in the ItemBinder and just have the two references and suffer through having longer paths, but I'd like the ItemBinder to seem as "the" class that has all the data, the other two being hidden away.

Additional complication is that I'd have to define several methods in the ItemBinder class that would manipulate data from both of the two classes. That may or may not be a problem depending on solution to this, if there is one.

Also, all data will be a reference type, so that simplifies things a bit.

share|improve this question

bumped to the homepage by Community 2 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

    
What is the question? You have already explored the alternatives. C# doesn't support delegation directly, which is the solution you seem to be searching for. For this reason, separate classes will be the easiest to maintain. – Frank Hileman Sep 30 at 0:33
    
My question is "Have I missed something?". I'm not that well versed with C# and people do come up with most ingenious solutions. – Karlovsky120 Sep 30 at 0:38
    
The only place I have seen something like delegation work in C# is using the property grid in visual studio, which has something like "virtual properties". I don't think this would help you. C# is a simple language in many ways. Separate classes, without the convenience properties, will be the easiest to maintain. – Frank Hileman Sep 30 at 0:42

I understand why ItemBinder may make sense, given than there are many ItemSave per ItemData. And in fact it seems that ItemData won't change during the lifetime of the application. In this case you would ideally add a ItemData property in ItemSave, but you can't modify ItemSave (and making a derived class is pointless because the third party code won't use it). So you create ItemBinder.

Yet ItemBinder is not a good solution, as it add a new maintenance problem with the convenience properties.

I would suggest to create an extension method for ItemSave that allows you to get the appropiate ItemData and then continue to use ItemSave directly. To get info from ItemData call the extension method. Edit: your additional methods may be extension methods too.

Of course you would use a dictionary to store your ItemData for fast retrieval.

share|improve this answer
    
I don't think an extension method would be any easier to maintain than a normal instance method, for the delegation needed. – Frank Hileman Oct 1 at 17:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.