2

Using Microsoft Bond in a C# project.

Suppose I had a Bond file A used in one project A', and wanted to have two projects B' and C' have Bond files B and C both of which have structs that inherit from a struct in file A. How would I do that?

I thought about making a ProjectReference, but it appears to only supply the C# classes generated from these Bond files, and not allow me to make Bond inheritance from the original Bond files, so instead I made a link between one project to the other. Unfortunately, my current solution is very error-prone, and changing that file location in one project would break the other as well.

What would be the suggested way to do that?

chwarr
  • 6,777
  • 1
  • 30
  • 57
yuvalm2
  • 866
  • 2
  • 10
  • 27
  • So the actual problem lies in how these bond definitions are used between the projects, not by the code generated from them? – rene Oct 11 '17 at 11:00
  • @rene - Yes, the problem is that project reference does not allow the bond compiler to consume bonds coming from the referenced project (At least, not the way I use it) – yuvalm2 Oct 12 '17 at 06:51

1 Answers1

1

To have Bond files B and C inherit from or contain the types from Bond file A (in project A'), you need to import Bond file A at the top of B and C. This makes the types in A known in B and C.

a.bond

namespace A;

struct Base { }

b.bond

import "a.bond"

namespace B;

struct Derived : A.Base { } 

struct Composition {
    // notice fully qualified name is used for Base
    0: A.Base has_a_base;
}

For the import path, you have three options:

  1. Use a fully qualified path like import "C:/src/projectAPrime/schemas/a.bond"
  2. Use a relative path like import "../../projectAPrime/schemas/a.bond"
  3. Use a relative path like import "a.bond" or import "schemas/a.bond" and add to the Bond import paths: elsewhere in the consuming project, you'd make sure that the BondImportDirectory item was augmented with a search directory for project A.

I would not recommend the first option, as it ties the paths to one specific machine's layout. Mixes of option 2 or 3 are used in practice.

For approach three, the consuming project usually has something like this either directly in it, or via some other MSBuild file it imports.

<ItemGroup>
  <BondImportDirectory Include="$(ProjectAPrimeRoot)" />
  <!-- or some other reference to project A, depending
       on how your projects are structured (perhaps you
       have a all_projects.props file for these sort of
       variables or item modifications -->
</ItemGroup>

I can't give you more concrete guidance than this, as your specific project structure isn't something I know, and MSBuild doesn't have a way for a project to "export" values that referencing project automatically get (that I'm aware of).

Note that you will still need a ProjectReference to project A', as the code generated in projects B' and C' will have dependencies on the code generated and compiled into the assembly that project A' produces.

For a working example of imports using BondImportDirectory see the C# import example in the Bond repository. This does not address the cross-project import, however, so you'll need to adapt for your situation.

If you're a C++ developer, this should feel similar to #include paths.

chwarr
  • 6,777
  • 1
  • 30
  • 57
  • Thanks! Just making sure, the answer is true for any importation of one bond from another, and not just inheritance, right? (e.g. bond A which contains a field of type bond B) – yuvalm2 Oct 21 '17 at 17:57
  • Composition works the same way. The answer has been updated. – chwarr Oct 23 '17 at 18:39
  • I felt inclined to also add if you were doing cpp (vcxproj) you would use this instead `BondIncludeDirectory`. – NiceNAS Aug 16 '22 at 21:36
  • @NiceNAS: the Bond project doesn't include C++/vcxproj code generation MSBuild targets. There's no such thing as [`BondIncludeDirectory`](https://github.com/microsoft/bond/search?q=BondIncludeDirectory). Perhaps someone in your team has written your own custom codegen targets that uses this item. (There are some of these floating around inside of Microsoft, if you happen to work there. They are specific to Microsoft's internal build tooling, so they can't be included in the open source version of Bond.) – chwarr Aug 16 '22 at 22:02