.NET - Cross project code reuse

Status
Not open for further replies.

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
I'm re-thinking how to reuse common code across unrelated applications. (E.G. A method to convert an Exception into a well-formatted and useful text with all of it's inner exceptions included.)

I never thought I'd ever want anything from the C/C++ world added into .NET, but #include functionality (injecting raw code from another file into other source files) seems to be an ideal way to do this.
  • Would give the ability to insert the code inside whatever namespace/class the host application expects utility functions to be at.
  • Would keep things DRY, and allow central bug-fixing.
  • Could be used any app, legacy or new without causing a stir. (What?!? You added a Common.Utilities namespace to my precious legacy app?!?? All such functionality must be in the LegacyApp.Shared.CommonTasks class library, or we will embark on HOLY WAR!!!)
  • No deployment issues, since it wouldn't create a new .dll.
  • Flexibiliy to use the include file as either a link (Would get central bug fixes) or as a copy (versionable)

Keeping such code in its own .dll severely limits it's ability to be used across a wide variety of apps.

Keeping such code as snippets or templates covers that, but ends up being copypasta with no ability to update a function ubiquitously.

What do you guys use for cross-app code reuse?
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Sometimes you can use your source control functionality to deal with it.
VS thinks it's just a file, but you pull it in directly from some other repository. This might be very painful depending on how it's set up...
You need your build system to understand how to do this (and do so repeatedly for versioned builds). If it's all one svn repo then a shared revision number will work easily, but go to a multiple repos world and suddenly you need to track the versions pointed to, which can get complicated, fast.

I would note that the things you gain compared to a dll are:

* Namespace agnosticism
* (.net specific), access to internals functionality within the actual dll/project it is consumed by
* Effectively statically compiled in, so if it is genuinely isolated you can have multiple versions in play in the same composite app.
** that might not be a benefit, it might be horribly confusing.
* Only requires compile time compatibility, not binary compatibility for non destructive changes[1]
* One less dll (it's rarely that big a deal, but hey).

Deciding that this is worth requiring a third party build layer on top of the system to get the right versions of the files is a balancing act.

If you're not wanting to use a versionable system using links to files is pretty trivial, host the source files like you would any other library. Have a standard location for them (via an environment variable is best), have VS project reference the linked file. Job done. I wouldn't ever want to do this, throwing away versionability is a non starter in my field, and I would think not desirable for most, but if that's what you want to do...

1. this is actually very significant, pushing function definitions into interfaces, return values from call sites (sometime requiring use of var) all simply work on a compatible rebuild, from a binary point of view they don't.

If you go in for this massively then I should note that this is effectively the philosophy underpinning how go works (but everything is done that way), the idea being that you pull in the latest version of dependencies at the source level, and commit that to your repo. I think that has a great many less pleasant implications, I know I'm not alone in that.
 
[url=http:/civis/viewtopic.php?p=27969233#p27969233:1r3sjdk6 said:
ShuggyCoUk[/url]":1r3sjdk6]
If you go in for this massively then I should note that this is effectively the philosophy underpinning how go works (but everything is done that way), the idea being that you pull in the latest version of dependencies at the source level, and commit that to your repo. I think that has a great many less pleasant implications, I know I'm not alone in that.

Not to totally derail this thread, but I have been playing with Go, and yes, that definitely seems like a potentially big problem. I think it was in this very forum a while ago somebody posted a link to a discussion on an open-source game that was being written in Go and then ran into huge problems due to external dependencies changing. If you go this way, you are putting a lot of faith in your external dependencies (be they of your own writing or not).

I vote with the others that you should just follow proper design principals and encapsulate this re-usable functionality in its own DLL. Then you have control over what version of it you use and can introduce changes when you want to, not when changes are made to the functionality.
 

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
[url=http:/civis/viewtopic.php?p=27968489#p27968489:2nxzy1g1 said:
svdsinner[/url]":2nxzy1g1]
  • Could be used any app, legacy or new without causing a stir. (What?!? You added a Common.Utilities namespace to my precious legacy app?!?? All such functionality must be in the LegacyApp.Shared.CommonTasks class library, or we will embark on HOLY WAR!!!)
  • No deployment issues, since it wouldn't create a new .dll. [NOTE: Do you really want to go through paperwork/documentation/etc. to explain why another file is needed in a deployment package for functions this minor?]
  • Flexibiliy to use the include file as either a link (Would get central bug fixes) or as a copy (versionable)
When I say cross app, I mean reusable cross ANY app, whether I'm working on a personal project, or writing code on a contract, etc.

There are several minor functions that I routinely use that frustrate me when I switch applications, and have to waste time either rewriting them, or creating places in the app to splice copy/paste code into. I'm sure most of us have our little snippets we like to use.

Thus far, the best I've come up with is using the MSDN benefit of online TFS to keep snippets that I should be able to access from most places. Not ideal.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Why not just put the dll up some place you can get to it?
Heck run a nuget server if you want to.

If something is so useful as to be used everywhere it's worth writing it to be stable and well tested/versions.

There doesn't have to be anything "OO" about it. It's about *modularisation*, which normally offere ways to encapsulate.

Edit OK -> OO, apparently auto correct isn't sold on it either.
 
Status
Not open for further replies.