views:

159

answers:

2

Hi,

My application is built in two sections. A C# executable which is the front end UI and a C++ dll which is more the low-level stuff. My application creates and manages many instances of objects, where every C++ object instance has a corresponding C# object instance. What techniques or libraries can I use to ensure that objects in the C# and C++ sections and data in those objects are always in sync at runtime? A change of one member in one object instance should update the corresponding object instance.

Thanks!

Edit: clarified a little what I meant by keeping the objects in "sync"

+2  A: 

Perhaps code-generation would work well. E.g. define the properties/methods for these classes in one spot (maybe XML or something) and generate C# and C++ classes from that. Perhaps use something like CodeSmith (http://www.codesmithtools.com/) to generate your code.

Alex Black
When you do that, make sure the C# classes you generate are 'partial'. That way, if you have to add any UI specific code to the C# class, you don't have to regenerate the object, and doing so doesn't prevent the regeneration of the object either.
GWLlosa
Hi Alex! I realized I didn't specify exactly in what way those objects should stay in sync, so I updated my question. What I meant was to keep instances in sync at run time. Thanks!
Steve the Plant
+1  A: 

It's not quite clear whether it would solve the problem, but have you considered Managed C++? I have had pretty good success simply compiling my C++ code as Managed C++, then using the managed extensions to create .NET classes that use the underlying C++ data directly. That way, there's only one copy of the data.

Probably not suitable for every situation (and I haven't tested its limits by any means) but I found it quite a timesaver. And since Managed C++ is a proper .NET language, the result was clean to use from the C# side, with none of the usual oddities or quirks one often has to work around when trying this sort of thing.

(Another similar approach would be to use SWIG (http://www.swig.org/) to generate wrappers for you. I hear it is easy to use and works well, but I haven't used it myself.)

brone
Hi brone! Thanks for answering. Unfortunately, the design is out of my hands. I'm just looking for the best solution taking into account the current design.
Steve the Plant
however, had I the choice I'd probably pick a solution exactly like this.
Steve the Plant