Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetArchTest ignores a nameof expression #81

Open
dbeylkhanov opened this issue Nov 7, 2020 · 3 comments
Open

NetArchTest ignores a nameof expression #81

dbeylkhanov opened this issue Nov 7, 2020 · 3 comments

Comments

@dbeylkhanov
Copy link

dbeylkhanov commented Nov 7, 2020

I faced with the strange behavior by NetArchTest when I worked with the nameof expression.

I have a simple an architecture test which is below:

public void DomainLayerDoesNotHaveDependencyOnInfrastructureLayer() {
	TestResult result = Types.InAssembly(typeof(Identity).Assembly)
		.That()
		.ResideInNamespace("StronglyTypedIdentity.Domain")
		.ShouldNot()
		.HaveDependencyOn("StronglyTypedIdentity.Infrastructure")
		.GetResult();

	Assert.That(result.IsSuccessful);
}

As you see, I want to avoid dependencies on the Infrastructure in the Domain layer (according to the Onion architecture it's wrong).

Now I'm going to apply this arch. test for my pet project .

To reproduce a strange behavior with nameof I deliberately decided to instantiate an object with CustomDbContext type(Infrastructure) in the CustomType (Domain). This means that I added a dependency on the Infrastructure layer in the Domain layer.
And now is an interesting moment, cuz, when I'm wrapping CustomDbContext into the nameof expr. (is evaluated at compile-time), then my arch test is successfully passing, but in fact, it should not. When I don't use nameof, the arch. test result is failed, and that is correct.

Watch my screencast here to realize an issue.
Also, visit my pet project to reproduce an issue

@NeVeSpl
Copy link
Contributor

NeVeSpl commented Nov 7, 2020

It is happening because nameof expression is replaced with an ordinal string literal during compilation. And NetArchTest is looking for dependencies in compiled code, not source code.

Example:
https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpGYZQAs6AsgBQCU6hxBSxr6AbgIYBO6AxugC86AHacAtgFMA9gDNqAZQAu3AJYiA5rQDcjIgF9k+oA===

@dbeylkhanov
Copy link
Author

dbeylkhanov commented Nov 8, 2020

It is happening because nameof expression is replaced with an ordinal string literal during compilation.

yes, it's. But it's still incorrect I think, isn't it? So would be nice to fix it

@dbeylkhanov
Copy link
Author

dbeylkhanov commented Nov 8, 2020

Let's imagine, that Domain and Infrastructure are separate assemblies. In this case, I deliberately should add a reference to Infrastructure assembly into the Domain assembly to use CustomDbContext in the CustomType. It means that NetArchTest will ignore also the reference(including dependency) on denied assembly.
I'm sure that should be fixed...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants