DarcyThomas
DarcyThomas

Reputation: 1288

Preventing one project from referencing another in .NET

I have a ASP.NET site which consumes services from a single class library project (RootServices) This library calls 2 (or more) SubService class library projects (e.g., PeopleService & OrderingService ) these service projects have a one to one mapping to a entity framework projects (e.g., PeopleDBEntities & OrdersDBEntities) which make calls to there respective DBs.

Ok this is great as we have single responsibilities for each part and we get the benefits of microservices with out the horrendous waste of making calls across the network (latency in nanoseconds not milliseconds!)

However as we add new people to the team they want to call the OrdersDBEntities from the PeopleService and the PersonEntities from the RootServices No circular dependencies, but still not what I designed. **Gah my beautiful architecture is being turned into a pile of spaghetti! **

Is there an easy way of forcing that a given project may only be referenced by other certain projects?

Upvotes: 0

Views: 107

Answers (2)

Mike Nakis
Mike Nakis

Reputation: 61993

Is there an easy way of forcing that a given project may only be referenced by other certain projects?

No. But the next best thing (or perhaps even better thing) is possible:

You can ensure that a certain project does not reference certain other projects. Here is how:

Assembly.GetExecutingAssembly()
        .GetReferencedAssemblies()
        .Select( a => a.Name )

The above code snippet will obtain the names of all assemblies referenced by the currently executing assembly. Then, you can check whether this list of names contains the name of an assembly that should not be referenced.

For details, (and more complete code,) see https://stackoverflow.com/a/72460117/773113

Upvotes: 0

Richardissimo
Richardissimo

Reputation: 5763

I would suggest adding Unit Tests of the project files to ensure the reference is not there. Depending how distinctive the name of the assembly is, this could be as simple as checking whether the file contains that name, or you may need a Regex.

The test can be named appropriately, so that if it fails the developer in question can be suitably enlightened without even needing to ask what the problem is.

I'm making the assumption that you have unit tests in place, which are regularly run (e.g. as part of an automated build), such that by breaking the test the issue will be highlighted by the broken build.

Upvotes: 1

Related Questions