Design for cross-platform classes in C #

Summary: I want to know the best design for creating cross-platform (for example, desktop, web and Silverlight) classes in C # without code duplication, with pluses and minuses of each construction.

I often write new useful classes for a single application domain; there is no reason why they will not work across domains. How can I structure my code to make it perfectly cross-platform?

For example, let's say I wanted to create a generic class "MyTimer" with an interval and an on-tick event. On the desktop, this will use the built-in .NET timer. In Silverlight, I would use DispatchTimer.

Construct # 1 can be "create a class and use preprocessor directives for conditional compilation", for example. "#IF SILVERILGHT ...". However, this makes the code less clear, readable, and maintained.

Construct # 2 can be "create subclasses called DesktopTimer and SilverlightTimer and consume them from MyTimer." How it works?

Although this is a trivial case, I may have more complex classes that, for example, consume platform-specific classes (IsolStorage, DispatchTimer, etc.), but without replacing them directly.

What other constructions / paradigms can be used?

+3
source share
7 answers

Interface, . , , , ( ).

, , , , , , , DispatchTimer Silverlight .NET .

, , , .

№ 1

. , Dependancy Injection, Unity Application Block .

XML, , "", , . UnityContainer . .

№ 2

Dependency Injection Unity Application Block. ?

, . , , ! =)

№ 3

StopLight [...] , ( ).

, XML, ! =)

, , , , quickstart StopLight XML .

, . XML. =)

+4

, - ( , , )

public interface ITimer
{
    void CreateTimer(int _interval, TimerDelegate _delegate);
    void StopTimer();
    // etc...
} // eo interface ITimer

:

public class DesktopTimer : ITimer
{
} // eo DesktopTimer

public class SilverlightTimer : ITimer
{
} // eo class SilverlightTimer

public class WebTimer : Timer
{
} // eo class WebTimer

. ? - factory, , . ( , , , , factory , )

public enum Platform
{
     Desktop,
     Web,
     Silverlight
} // eo enum Platform

public class TimerFactory
{
    private class ObjectInfo
    {
        private string m_Assembly;
        private string m_Type;

        // ctor
        public ObjectInfo(string _assembly, string _type)
        {
            m_Assembly = _assembly;
            m_Type = _type;
        } // eo ctor

        public ITimer Create() {return(AppDomain.CurrentDomain.CreateInstanceAndUnwrap(m_Assembly, m_Type));}
    } // eo class ObjectInfo

    Dictionary<Platform, ObjectInfo> m_Types = new Dictionary<PlatForm, ObjectInfo>();

    public TimerFactory()
    {
        m_Types[Platform.Desktop] = new ObjectInfo("Desktop", "MyNamespace.DesktopTimer");
        m_Types[Platform.Silverlight] = new ObjectInfo("Silverlight", "MyNameSpace.SilverlightTimer");
        // ...
    } // eo ctor

    public ITimer Create()
    {
        // based on platform, create appropriate ObjectInfo
    } // eo Create
} // eo class TimerFactory

, factory , factory, , , . .

+1

OOD, . - (Windows, Mono/destkop, web). (, Timer). / Factory .

EDIT: - , , .

EDIT: DI/ Factory. , , . , , .

// Common.dll

public interface IPlatformInfo
{
    string PlatformName { get; }
}

public interface PlatformFactory
{
    IPlatformInfo CreatePlatformInfo();
    // other...
}

public class WelcomeMessage
{
    private IPlatformInfo platformInfo;

    public WelcomeMessage(IPlatformInfo platformInfo)
    {
        this.platformInfo = platformInfo;
    }

    public string GetMessage()
    {
        return "Welcome at " + platformInfo.PlatformName + "!";
    }
}

// WindowsApp.exe

public class WindowsPlatformInfo : IPlatformInfo
{
    public string PlatformName
    {
        get { return "Windows"; }
    }
}

public class WindowsPlatformFactory : PlatformFactory
{
    public IPlatformInfo CreatePlatformInfo()
    {
        return new WindowsPlatformInfo();
    }
}

public class WindowsProgram
{
    public static void Main(string[] args)
    {
        var factory = new WindowsPlatformFactory();
        var message = new WelcomeMessage(factory.CreatePlatformInfo());
        Console.WriteLine(message.GetMessage());
    }
}

// MonoApp.exe

public class MonoPlatformInfo : IPlatformInfo
{
    public string PlatformName
    {
        get { return "Mono"; }
    }
}

public class MonoPlatformFactory : PlatformFactory
{
    public IPlatformInfo CreatePlatformInfo()
    {
        return new MonoPlatformInfo();
    }
}

public class MonoProgram
{
    public static void Main(string[] args)
    {
        var factory = new MonoPlatformFactory();
        var message = new WelcomeMessage(factory.CreatePlatformInfo());
        Console.WriteLine(message.GetMessage());
    }
}
+1

1) : ITimer , , "WebAssembly", WebTimer. "WebAssembly.dll" "DesktopAssembly.dll". /, . MEF .

2) (), . . WebTimer #ifdef WEB_PLATFORM ..

# 1, , , - .net- silverlight . , - .

+1

, - . Moo-Juice sugestion...

` // ???

ITimer {
void StopTimer();//etc...

void StartTimer();//etc...

TimeSpan Duration {get;}//eo interface ITimer

} `

ITimer , . . .

, , .

:

var foo = new Foo(new WebTimer());

, . . , , , .

(IOC). (Thats )

"" IOC, "Foo". "Foo", IOC . , , ITimer. ITimer , , .

- ITimer , , .

, ...

P.s: IOC ...

:

http://ninject.org/download

http://www.castleproject.org/container/index.html

http://www.pnpguidance.net/Category/Unity.aspx

0

Why not have a configuration section that tells your library about the host application platform. You should install this only once in your application in the host configuration file (web.config or app.config), and you can use the rest using the Factory method, as suggested by Moo-Juice. You can use platform details throughout the entire library functionality.

0
source

Source: https://habr.com/ru/post/1776248/


All Articles