views:

513

answers:

6

Hi,

I'm a programmer who has never really used .dll files. Of cause, when I need 3rd party software, such as a graphics library, a library to help me create graphs etc. I do add the references/ddl files to my program and use them in my code.

Also, it seems like you can use .dll for a lot of different things, so I'd like the topic to concentrate on C#.

Right now I am working on a sanitizing library(?) (I think that is the correct term), which will be full of relevant methods that can sanitize variables in all sorts of different ways.

What I want to know is:

would there be any advantage to:

1) Write the methods to class library -> compile/build it -> add the library as a reference to the program - which would need to sanitize some variables ?

Or would it be exactly the same if I where to:

2) Create a new SanitizeClass in the program -> add all the sanitize methods -> call the methods from the SanitizeClass in the different classes in the program that needs to sanitize variables

In general, I also want to know when it is an advantage to use compiled class libraries. I'm thinking of speed, security, all of it.

Could anyone enlightenment me? :)

+1  A: 

The first thought that comes to mind is re usability. Will this sanitize library be used ever outside of the application you're currently working on? If you are, then you don't want to have to reference an exe in the future, for that you want to build a DLL file (possibly even strong name it and GAC it) and then just reference that in the future.

BFree
+11  A: 

The key question is: does it make sense for more than one application to use the type? If so, it should be in a class library. If not, you may still want to put it in a class library just for the sake of separation (e.g. have one assembly per tier in an n-tier solution), but you could just put it in your application.

If your sanitization is general-purpose, then putting it in a class library would definitely be the right move.

I know of people who write almost no code in executable applications, and put almost everything in class libraries - so the application basically just wraps the class libraries. I don't tend to go quite that far myself...

Jon Skeet
But is there any speed advantages or loss using a class library instead of putting it directly in the application?
CasperT
YES - any code executed on an external assembly would always be slower compared to code executed on the same assembly.
Raj
Do you have any figures on that claim? I tend to think that the loss in speed is only marginally. But then again, I too don't have any figures to back that statement up.
borisCallens
I'd want to see evidence too. I would expect that after JITting, in many (but possibly not all) cases there'd be no difference at all. I certainly wouldn't expect a *significant* difference.
Jon Skeet
+1 for Jon (I'm sure I'm the first). I want proof that executing the code in a referenced assembly is going to be slower than code in the entry assembly.
Adam Robinson
I think the only slow point will be when first loading the dll, since there, it will be same performance
Jhonny D. Cano -Leftware-
@Raj, no. The CLR is very, very good at things like that. Once your code is JITed, provided it's in the same App Domain, there will not be any difference.
unforgiven3
+1  A: 

Using libraries is useful when you want to share code between multiple programs.

If you think you will need your classes to sanitize data in more than one program, this is a good idea to put them in a library.

It is also a good idea to put layers of your application in different libraries (check n-tier architecture)

Martin
+2  A: 

Additional assemblies will add a slight load time and memory overhead, but otherwise have no performance implications.

Security will only make a difference if you need different code access security (CAS) and will make use of CAS.

Generally there are three reasons:

  1. It makes things simpler. Putting everything in a single assembly is becoming unwieldy (this could include organisational/team issues).

  2. Reuse.

  3. You need separate assemblies. E.g. to have interfaces and implementation in different assemblies to allow AppDomains to be unloaded (e.g. for plugins).

Richard
A: 

I think the easiest way is to add SanitizeLibrary project as Library Class project to your solution and reference this project in your application. If you find it useful you may then extract library from current project and reference it as dll in your other projects.

empi
A: 

if you would be reusing this code in different apps - ie: this would be some sort of shared library - then compile it as a dll

reg performance, please note any code which is executed against an external assemble would always be a bit slower than executing code within the same assembly

Raj
"would always be a bit slower": Please expand. The JIT (and ngen) will allow inlining and other optimisations across assemblies.
Richard