XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Organizing project for XML Serialization

Last post 24/05/2009 22:46 by Morsel. 3 replies.
  • 24/05/2009 6:18

    Organizing project for XML Serialization

    Hey guys,

    I have gone over a few tutorials on how to load assets into my game through the content pipeline with XML but I have run into a bit of a snag. I took all the classes that needed to be loaded through XML and put them in a new shared library project, I have my main game project and my pipeline extension project referencing the shared library - that all makes sense. However, some of the classes in the library need to have access to certain classes in the main project... but I can't reference them because it would create "circular logic". How would i get around this? or do I just put all necessary classes in the shared project regardless if they are actually being directly utilized by the content pipeline?
  • 24/05/2009 16:02 In reply to

    Re: Organizing project for XML Serialization

    In my "day job," I supply libraries to other programmers, so I run into this problem a lot in both C# and C++.  Usually I design (or refactor) the project one of two ways:

    1) Least commonly, I'll remove the circularity by combining code or moving code between two libraries.  In the current case, for example, I might move the parts of the main project which are dependent on the Xml library into the Xml library.  I would continue to move sub-dependencies until I found some logical breaking point that both separated the dependencies and minimized coupling.  In very rare instances, two libraries might actually get combined into one.  But like I said, that is what I least commonly do.

    2) Most commonly, I'll factor out the dependency into a third library.  For example, I might factor out the runtime version of the data along lines that left it separate from the both the main code and the Xml code.  Then, the Xml could depend on this data library, and the main could depend on both the Xml and the data libraries.  About half the time this process goes really smoothly, and about half the time it results in a re-thinking of the entire design.  And when it happens, that re-think is almost always a better solution.  This is the approach I take about 95 percent of the time.

    The main thing, I think, is to do what makes sense.  Separate the dependencies in a way that would be most understandable to some third person using the libraries for the first time.  Most of the time, that will also result in good design with the least obnoxious coupling.

    -Neil

     

  • 24/05/2009 22:21 In reply to

    Re: Organizing project for XML Serialization

    In the standard classes, you'll notice that for each content object, there is a custom content object, oftentimes in the framework these custom objects follow the Document Object Model. These classes serialize the same way as the run-time classes but are missing all of the methods and other implementation details that don't need to be serialized. For example, the Model class and its counterpart ModelContent which is used during the Content Pipeline. These DOM objects act as placeholders and don't need the run-time references that your real class requires because their methods aren't implemented.

    You can do the same thing with your custom classes to leverage the Content Pipeline without causing a circular dependency.
  • 24/05/2009 22:46 In reply to

    Re: Organizing project for XML Serialization

    Neil - I took your advice and made another library and after a lot of refactoring everything seems to be running smoothly!

    MrLeebo - I was actually using those empty "content" classes at first but when it came to enums the pipeline kept trying to reference the empty class' enum instead of the actual one which caused errors. I thought it was odd, but it seemed sort of hackish to use empty classes anyway so I thought I was just doing it wrong :(. Have you had any success using enums with this method? I'd be curious even just for educational purposes.

    Thanks for the replies fellas!
Page 1 of 1 (4 items) Previous Next