0

Say I have four Projects. DataRepositories, DataRepositoryInterfaces, SetupInject, WebAPI. I want WebAPI to only have a direct reference to two of those projects. Essentially making WebAPI ignorant to the concrete implementations of my repository interfaces.

To do this I've made it so SetupInjections main purpose is to define the concrete types that will be injected. However, when I reference the InjectionSetup project I get all it's dependencies in the WebAPI project as well.

Although I never referenced the project that defines the concrete classes directly, they are still available for me to use. The main objective is to restrict the developer from making an instance of the class in this project.

Although I never referenced the concrete classes directly, they are still available for me to use.

I recall being able to do something like this in previous versions of .NET. Is there any way to do this now?

1) Is what I'm trying to do suggested?

2) Can it be done with .Net Core?

3) Why could it be done in previous versions.

Brian Rizo
  • 838
  • 9
  • 14

1 Answers1

1

I believe you can do this using Friend Assemblies, https://msdn.microsoft.com/en-us/library/mt632254.aspx I've often use this in Unit Test projects that need to access classes defined as internal.

In your SUT using System.Runtime.CompilerServices; using System;

[assembly: InternalsVisibleTo("AssemblyB")]

// The class is internal by default.
class FriendClass
{
    public void Test()
    {
        Console.WriteLine("Sample Class");
    }
}

In your Unit/Int tests

// Public class that has an internal method.
public class ClassWithFriendMethod
{
    internal void Test()
    {
        Console.WriteLine((new FriendClass).Test);
    }

}

Here's more details, https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx You may also add additional code signing to further restrict it.

David Lai
  • 814
  • 7
  • 13
  • 1
    This is good however i would like to be more implicit rather than declaring each class I would like t he whole assembly to be hidden. Thank you for the answer though, I'll be using this until I find something more suitable for replacing it – Brian Rizo Jul 01 '16 at 14:43