3

I asked a question on here some time ago when I wandered if it was better to split a large project (.NET class library) into multiple .NET DLLs. The advice was to have one large DLL.

This DLL is now used in another project. The other project only uses a few classes and therefore there are lots of classes in the project that are unused.

Is this bad practice from an architectural perspective to have one DLL or am I over thinking this?

4 Answers 4

4

There are certain pros and cons for having the seperate dlls:-

Cons:-

The performance and complexity may increase.

Pros:-

code reuse and layers organization

You may also be interested to check articles on good design (The starting five is about SOLID principles. The rest is about how to structure your dll's)

EDIT:-

You will be intersted to read this MSDN:-

Prefer Single Large Assemblies Rather Than Multiple Smaller Assemblies

To help reduce your application's working set, you should prefer single larger assemblies rather than multiple smaller assemblies. If you have several assemblies that are always loaded together, you should combine them and create a single assembly.

The overhead associated with having multiple smaller assemblies can be attributed to the following:

•The cost of loading metadata for smaller assemblies.

•Touching various memory pages in pre-compiled images in the CLR in order to load the assembly (if it is precompiled with Ngen.exe).

•JIT compile time.

•Security checks. Because you pay for only the memory pages your program accesses, larger assemblies provide the Native Image Generator utility (Ngen.exe) with a greater chance to optimize the native image it produces. Better layout of the image means that necessary data can be laid out more densely, which in turn means fewer overall pages are needed to do the job compared to the same code laid out in multiple assemblies.

Sometimes you cannot avoid splitting assemblies; for example, for versioning and deployment reasons. If you need to ship types separately, you may need separate assemblies.

NOTE:-

.Net DLL's are assemblies but the reverse is not true.

4
  • 1
    How would the performance be negatively affected by using more than one DLL?
    – Geeky Guy
    Sep 17, 2013 at 20:52
  • @Renan:- I refered the performance degradation for seperate dlls not one dll!!! is it misleading?? Sep 17, 2013 at 20:53
  • No, I got what you meant. I want to understand how that degradation happens.
    – Geeky Guy
    Sep 17, 2013 at 21:08
  • I see. +1 for the reference.
    – Geeky Guy
    Sep 18, 2013 at 10:15
2

I tend to split the layers out into several projects/DLLs even for small projects. For one thing, it helps keep your separation of concerns - for example, if your business layer has no way to talk to the database, but can only talk to your repository project, then you're guaranteed that you will go through the appropriate path.

With that same concept, you can expose interfaces from layer-to-layer, without exposing the underlying classes. This helps you code by contract, since one layer cannot talk directly to a class in another layer, but has to use its interface, which is connected through an IoC library like Ninject.

As your project grows, it's also nice to have them separate, if you have multiple developers working on a project. You can have a front-end dev, a service dev, a database dev, etc., who will never conflict with each other because they're working on entirely different projects.

EDIT

Regarding the comment:

1) You can still use interfaces within a layer, with DI. A simple example would be:

public interface IUserManager{}
internal class UserManager : IUserManager{}

public interface ICustomerManager{}
internal class CustomerManager : ICustomerManager {
    private readonly IUserManager _userManager;
    public CustomerManager(IUserManager userManager) {
        // DI library automatically populates this object
        _userManager = userManager;
    }

    void SomeMethod() {
        var user = _userManager.GetUser(42);
    }
}

2) I'd say if you go to the trouble of working with interfaces, then use them all the time and don't give yourself the option not to. For example:

// Repository project
public interface IUserRepository{}
internal class UserRepository : IRepository{}

The business project only knows about the IUserRepository, not the UserRepository, so there's no way it can ever get a direct reference. Another alternative is to keep your interfaces and your implementations in two separate projects, and the caller would only have a direct reference to the interfaces project.

It just keeps you honest.

But if you have no reason to use interfaces at all - for example, if you have no intention of ever mocking out your database or switching to a different layer which implements the same interface - then I'd just say leave them out completely. Only code with interfaces if it offers you some kind of potential benefit, otherwise you're just adding complexity for no reason.

5
  • Thanks +1. I have two questions here: 1) would you ever use interfaces in the same tier e.g. business layer to business layer? except to use interface polymorphism? 2) Would you ever not use an interface between tiers?
    – w0051977
    Sep 17, 2013 at 20:58
  • The separation of layers is logical, not physical. You could have all your layers each in a different project, and it would naturally lead to each having its own corresponding assembly - but you could also merge all your assemblies into one, and you'd still have your layers in there. The only reason I see that would justify having each layer in its own assembly is if you want to redistribute some of the "lower" layers but not the "topmost" ones.
    – Geeky Guy
    Sep 17, 2013 at 21:13
  • @Renan Sure, that's an option, but at the same time, the stuff I discuss, like ensuring you go through proper channels (UI->business->repo->data or whatever your path is), separating your developers, forcing coding-by-contract, etc., will not be available to you if you keep everything in one library. Redistribution is only one of many benefits to splitting, and there are very few drawbacks.
    – Joe Enos
    Sep 17, 2013 at 21:18
  • "will not be available to you if you keep everything in one library" How? I'll be able to reference the library and all the namespaces and classes will be available to me. As for the drawback, now you have multiple assemblies to version. Hardly a considerable overhead, I know, but think about having to strong name, sign and certify and GAC-manage multiple assemblies instead of having to deal with only one.
    – Geeky Guy
    Sep 17, 2013 at 21:21
  • "will not be available" I mean the benefits listed are not available. Not having access directly to a class is actually a benefit in a tiered application. Regarding the strong name/signing stuff, yep, it's a little more work, but in anything more than a trivial project, that would all be scripted out in build scripts anyway - once you know how to write a build script for one project, you can do it for 10 projects very quickly and easily. There's no one "right" way of doing things, but n-tier (physical DLLs or services as tiers) is in my opinion the way to go.
    – Joe Enos
    Sep 17, 2013 at 21:27
1

It's good practice to split any given project into distinct layers. UI, Data, and Business Logic are common lines of separation (MVC is a version of this). If your project has multiple concerns like this all mashed together, it's probably a good idea to split it up.

Performance isn't really the issue--it's more about code maintainability.

1
  • 1
    You can have those layers in a single DLL. The separation of layers is logical, not physical. Writing each layer in a separate project is often considered good practice and will lead to each layer having its own assembly, but you can merge those assemblies into one as well if you so wish.
    – Geeky Guy
    Sep 17, 2013 at 21:10
0

You're overthinking it. If all the DLL contains is logic then it'll be a pretty small file by today's standards, and any classes that are not used won't occupy any memory space whatsoever.

Also you keep thing tidier if you have less files to deploy and track.

4
  • Thanks, though I am looking at this from an architectural perspective rather than a performance perspective. Is your answer still the same?
    – w0051977
    Sep 17, 2013 at 20:50
  • Yes. You keep things tidier if you don't have to deal with maany files as well.
    – Geeky Guy
    Sep 17, 2013 at 20:51
  • Are you saying that you would have one DLL? - containing business logic and data logic.
    – w0051977
    Sep 17, 2013 at 20:53
  • Sure thing, why not? Actually that's the key question to be answered IMO: just why not?
    – Geeky Guy
    Sep 17, 2013 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.