Announcement

Collapse
No announcement yet.

Code sharing between bridge.net and pure c# projects

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Code sharing between bridge.net and pure c# projects

    Hello,

    I'm a bit confused on how to share code between existing c# projects and bridge.net projects.
    I'm interested in translating to javascript some classes that already exists in another project: the goal is to have the very same implementation both in c# and javascript (it's a formatting routine for numbers and strings).
    Bridge.net requires the use of substitute base types, not recognized in projects not aware of it.

    How do you organize your projects for code sharing ?

    Thanks a lot for any help

    #2
    Hi sgatto

    I was asking myself the same question recently. Of course the main purpose of Bridge isn't really to share code between both worlds, but to facilitate JS development and make it more professional by strongly typing things. I think your best option of sharing code would be an approach like this:

    1. Include the shared code file in both projects (Bridge lib and regular .NET DLL), using a Visual Studio link in one of the projects. That way you only have one physical file for the share class. See this article for more details on how to create a file link.

    2. If not all code of that class can be shared, use compiler directives such as an #if statement:
    #if BRIDGE
    using Bridge.Html5;
    #endif
    using System;
    using System.Collections.Generic;
    using System.Linq;
    I'm not sure if this is the best approach, but I'm sure it will work without having to duplicate any code.

    Hope it helps.
    Regards,
    Marco

    PS: The compiler variable "BRIDGE" doesn't exist by default, you have to set it manually. Perhabs the Bridge crew can tell is if there's a built in compiler directive to recognize bridge?
    Last edited by Marco; 2016-06-14 @ 10:33 AM.

    Comment


      #3
      If you want to share code and you will use attributes like [External] or [ObjectLiteral] that are defined in bridge you will still need to define them in your native c# project or also add if/defs

      Comment


        #4
        Thanks for your reply.

        I see, linking files between projects would be effective for small projects, for larger ones this solutions needs a deep refactoring to loose the coupling of classes so that they can be linked from the outside.
        As of my actual understanding of bridge.net, this could be the biggest problem preventing it to be adopted from a large user base.

        I'll give it a try and will let you know :)

        Comment


          #5
          Is there a better way meanwhile?

          I managed to separate code in different Bridge.Net Libs but referencing a normal C# library is not working, even if it contains only code working with Bridge.Net like this min Test Code:

          namespace BridgeNetLib02 {
              public class Tester01 {
                  public static string Hello(string name) {
                      return "Hello " + name;
                  }
              }
          }
          What is the different between a normal C# class library and a class library created with the bridge template for class libraries?

          Comment


            #6
            It is because a normal C# library references mscorlib - Bridge Removes this reference and uses its own System DLL, So it is like trying to use .Net Core Libs with normal .Net Framework Libs.

            One way you could get around it is, Have the Core Library - Then have Core.Bridge Library - Link all cs files - Core to Core.Bridge - then add the reference for Standard C# with Core and Bridge C# with Core.Bridge - Also use the same Namespace for both Files.

            This is how I get around this.

            I tend to do this also.

            Client - in Bridge - (Namespace ClientApp)
            Core - in C# Normal - (Namespace ClientApp)
            Server - in C# Normal - (Namespace ServerApp)

            Then just link the files needed from Core in Client.To Make it easier I also make the Client and the Core have the same namespace.


            Comment


              #7
              Thanks for the info! I will try it this way!

              Comment

              Working...
              X