2

Problem: Class B is a subclass of Class A. RIA service returns a list of object Bs. Class A and B are both necessarily defined on the server-side. They serialize fine, and I can use them in the primary client project.

I have two other libraries, organized as client libraries. One is for custom controls, and the other is for classes that are shared between custom controls and the actual client project.

I need Class A to be accessible from the Classes library clientside (so that the custom controls can get to it). How can I do this?

I've done this:

http://msdn.microsoft.com/en-us/library/ee707369%28v=vs.91%29.aspx

but the *.shared.cs convention doesn't give libraries other than the actual Client library access to Class A. The second method (Add as Link) does do what I want it to do, except that updating ClassA.cs in the server project doesn't cause the Client version to update, making it necessary to update both class files each time they're changed, which is unacceptable.

edit: Add as Link worked great after trying again several times.

Nathan
  • 75
  • 1
  • 6
  • What does it mean "doesn't cause the Client version to update"? It is the **same source** file added to two different *.dlls. It just can't update in one and not be updated in the other. – Wiktor Zychla Aug 10 '12 at 16:30
  • I'm using [SourceShare][1] plugin for Visual Studio for this purpose [1]: https://enless-soft.com/index.php?option=com_es_products&view=pr&layout=overview&pid=33 – hazzik Aug 10 '12 at 16:53
  • @WiktorZychla If I edit the source file, then open the other file, there is no indication in the other file that it has changed to reflect the source (ie, After adding a new property to source, the other file still doesn't have the new property). Is this expected behavior where I should make a point of totally ignoring the link file? Is there something I need to update? I wasn't inclined to think so because of the Note in the Add as Link section of this How To page: http://msdn.microsoft.com/en-us/library/9f4t9t92%28v=vs.80%29.aspx – Nathan Aug 10 '12 at 17:16
  • Nathan: my guess is that you have two different solutions. In order to work correctly, this mechanism requires that file is shared by two projects in the SAME solution. – Wiktor Zychla Aug 10 '12 at 18:40
  • I can confirm that everything is in one solution. Server and Client projects are organized by folders which all branch from a single solution. Should there be any indication on the file icons? [Image of Files](http://i.imgur.com/o2shb.png) – Nathan Aug 10 '12 at 19:39
  • Problem was solved by coming in to work the next day and trying again. Not sure what the issue was. – Nathan Aug 13 '12 at 19:16

1 Answers1

1

In Visual Studio (2010, at least — dunno exactly whe the feature was added), you can add an existing item to a project as a 'link', meaning the source file is shared between projects. Only one version of the source file exists.

  1. Right-click on your project.
  2. Click on 'Add..Existing Item'.
  3. Find the source file of choice and select it.
  4. The 'Add' Button is a drop-down. Click the drop-down icon in the button.
  5. Click on 'Add As Link'.

Easy!

Now any change to the share source file is reflected on both places. The drawback, of course, is that the developer gets no indication that changes to the shared source file might have wider ramifications than she might realize.

Another option would be to create a hard link so two file names reference the same file ('inode' in Unix terms). On the command line, just chant the magic incantation:

fsutil hardlink create <new-filename> <existing-filename>

something like:

fsutil hardlink create c:\foo\bar\some-project\bazbat.cs c:\foo\bar\another-project\bazbat.cs

The restriction is that both names have to be on the same volume.

This has the possibility, of course, confusing your source control system. I'm willing to bet that TFS hasn't consider the possibility that a filesystem isn't necessarily a tree structure and that the same file might exist in multiple directories.

Hard-linked files may be deleted in any order: the file ceases to exist when the last link to gets removed.

The 3rd option, of course, and likely the "best-practice" — hate that term! — is to factor out the shard classes into an independent assembly that gets deployed to both the client and server. If want to limit the number of assemblies floating around, post-build, you can use ilmerge to merge assemblies:

Come to think of it, there's also no reason you can't embed the shared assemblies as embedded resources that get loaded on demand (or even at startup). This might even be cleaner than using ilmerge. Here's how to do that:

http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx

Community
  • 1
  • 1
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • Cab you give an specific insight into why the first method might not work? After adding as link, I can edit both files, and updating source doesn't change the link file. – Nathan Aug 10 '12 at 18:21
  • No idea. I don't believe all types of projects allow linked files. For instance, I don't believe website projects or unmanaged C++ projects don't allow linked files. If the file already exists in the project, you might have problems too. For instance, you can "add as link" a file that already exists and VS doesn't whine, it just silently doesn't do anything. Before trying to link an existing from from one project to another, you might want to removee the file from the one project, delete it from the file system (and source control?), restart VS and then do the link. Just a thought. – Nicholas Carey Aug 10 '12 at 20:26