How to correctly separate classes from frameworks (decoupling at the initial level)

I am working on a game project using the Microsoft XNA infrastructure, although this question is a general one related to the decoupling of classes / systems.

Let's say I have a class (simplified for this example) as shown here:

public class GameCell
{   
        public Color Color { get; set; }
}

I would like to reuse as much code as possible between multiple platforms using C #.

The problem with direct redirection of the Color structure is that this type is defined in XNA assemblies, creating a strong connection (or dependency) between my code and XNA.

In another structure that I will use, it may (or not) have its own Color object with its own set of properties and API.

I would like to have the same “know” source file to automatically use XNA or another framework implementation.

I know other types of decoupling methods, such as IoC, but that would mean that I would connect different versions of the system / class instead of reusing the same class in different contexts.

Can this be done? how would you suggest keeping such a system portable?

I saw some cases (in your own development in C ++) where you would define a set of classes that reflect the classes you use in the structure (for example, here, define the color again), and so you can "re-display" it later, so that use different classes as needed.

#IFDEF using ( XNA ). ?

+5
1

, Color . :

#define USE_DOTNET
//#define USE_SOMETHINGELSE

#if USE_DOTNET
using System.Drawing;
#endif

#if USE_SOMETHINGELSE
using SomethingElse.Drawing;
#endif

public struct MyColor
{
    #if USE_DOTNET
    Color TheColor;
    #endif
    #if USE_SOMETHINGELSE
    SomethingsColor TheColor;
    #endif

    public int R
    {
        get
        {
            #if USE_DOTNET
                // code to return .NET Color.R value
            #endif
            #if USE_SOMETHINGELSE
                // code to return SomethingColor.R value
            #endif
         }
    }
}

- .NET SomethingElse. :

#if USE_DOTNET
public int R
{
    get { return TheColor.R; }
}
// other properties and methods here
#endif

#if USE_SOMETHINGELSE
// properties and methods for using SomethingElse
#endif

, , .NET Color , .

C/++ , Windows, Mac . , , , .

, , , .NET , . , , , , - , , . ( , - ...)

+5

All Articles