Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3407070
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:42:20+00:00 2026-05-18T05:42:20+00:00

Summary: I want to know the best design for creating cross-platform (eg. desktop, web,

  • 0

Summary: I want to know the best design for creating cross-platform (eg. desktop, web, and Silverlight) classes in C#, with no duplication of code, with the pros and cons of each design.

I’m often writing new, useful classes for one application domain; there’s no reason why they won’t work across domains. How can I structure my code to make it ideally cross-platform?

For example, let’s say I wanted to make a generic “MyTimer” class with an interval and on-tick event. In desktop, this would use the built-in .NET timer. In Silverlight, I would use a DispatchTimer.

Design #1 might be “create a class and use pre-processor directives for conditional compilation,” eg. “#IF SILVERILGHT …”. However, this leads to code that is less understandable, readable, and maintainable.

Design #2 might be “create subclasses called DesktopTimer and SilverlightTimer and consume those from MyTimer.” How would that work?

While this is a trivial case, I may have more complicated classes that, for example, consume platform-specific classes (IsolatedStorage, DispatchTimer, etc.) but aren’t directly replacing them.

What other designs/paradigms can I use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:42:21+00:00Added an answer on May 18, 2026 at 5:42 am

    I would suggest writing Interfaces that you would simply implement for your platform specific code. Then, the interfaces assure that your code will respect the contracts given by your interface, otherwise there will be a code break (if one member is not implemented).

    Besides, within this library where resides your specific timer classes, to stick to your example, I would create a class for each platform, thus using the DispatchTimer for Silverlight, and the built-in .NET timer for the desktop version.

    In the end, you would end up using only one interface that only its implementers know how to deal with the contract specifically to your underlying platform.

    EDIT #1

    Conditonal design is not an option for a good design. Here is a tool that will help you deal with the Dependancy Injection, that is called Unity Application Block, and is used to deal with such scenario like yours.

    You only use an XML configuration that is very versatile to "tell" what has to be instantiated when this or that interface is needed. Then, the UnityContainer consults with the configuration you have made, and instantiate the right class for you. This assures good design approach and architecture.

    EDIT #2

    I’m not very familiar with Dependency Injection, and not at all familiar with Unity Application Block. Can you point to some resources or explain these a bit further?

    1. Microsoft Enterprise Library 5.0 – April 2010;
    2. Microsoft Unity 2.0 – April 2010;
    3. Microsoft Unity 2.0 Documentation for Visual Studio 2008;
    4. Are there good tutorial/walkthroughs for unity that don’t use configuration files? (SO question on the topic that should provide valuable hints to start with Unity);
    5. Specifying Types in the Configuration File;
    6. Walkthrough: The Unity StopLight QuickStart;
    7. Walkthrough: The Unity Event Broker Extension QuickStart.

    I think these resources shall guide you through your learnings. If you need further assistance, please let me know! =)

    EDIT #3

    But anyway, the StopLight quickstart […] seems to imply that the dependency mapping of interface to concrete class is done in code (which won’t work for me).

    In fact, you can do both code and XML dependency mapping, the choice is yours! =)

    Here are some example that you should perhaps inspire from to make the StopLight quickstart use the XML configuration instead of the coded mapping.

    1. Testing Your Unity XML Configuration;
    2. Using Design-Time Configuration;
    3. Source Schema for the Unity Application Block.

    If this doesn’t help you get through, let me know. I shall then provide a simple example using XML dependency mapping. =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know if Html.ValidationSummary is present in code. The tag div class=validation-summary-valid
Summary: I want the prettiness of Silverlight/WPF in part of my current Winforms application.
Summary: I want to know if it is possible to do something like this:
I want to know if there is any method to show summary info from
Summary: I want a horizontal row of ImageButtons to scale down evenly to fit
Summary: I want to display a value (in a text box) stored in another
Summary I want to be able to call a JavaScript function from a Flex
This is what i want to do: I want /summary.php to include 5 latest
I want to generate a summary of the files that are in one tree
We are two students who want to use one-class svm for dectection of summary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.