Replies: 35 comments 20 replies
-
I guess that with
you mean
I think this is an excellent idea (I have thought about it myself). For many applications the project is not the optimal granularity for determining visibility |
Beta Was this translation helpful? Give feedback.
-
There's no way to express this in IL, so even if C# annotated it specially and played along, other languages would still see everything as |
Beta Was this translation helpful? Give feedback.
-
@jnm2 That's where the "in the same assembly" condition would come in. No annotations would even be required, it would only need to be marked as internal in IL. I wouldn't mind if |
Beta Was this translation helpful? Give feedback.
-
Why not just compile this into its own assembly? Then internal will mean just what you want. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi I could get the same behaviour by using different assemblies and the internal scope but it is very common that in a namespace I will have a set of related a few classes that has just one class with the entry points that I want to expose to the rest of the project (the other classes are just implementation detail). Using assembly's seems like a bit overkill for the amount of times that this occurs and I would prefer not to have potential many assembly's with only a handful of classes in each. |
Beta Was this translation helpful? Give feedback.
-
Why not make those "implementation details" classes, private nested classes of the entry point class? |
Beta Was this translation helpful? Give feedback.
-
Here is a related proposal I never ported over here: dotnet/roslyn#8391 To sum it up: separate the logical concept of component from the physical concept of assembly. You can have multiple components in an assembly (components would likely be different namespaces). The One major difference with just using assemblies is performance during the build process. Achieving this style of encapsulation would need multiple small assemblies. Building many tiny projects takes much longer than a large monolithic project. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw in my previous company we had an analyzer for very this purpose that wouldn't allow you to use the type from external namespace. We hade very same issue: one assembly which contains multiple "projects", each in its own namespace. And yet, one simple analyzer that doesn't allow you to add |
Beta Was this translation helpful? Give feedback.
-
as long as its internal you shouldn't really care. with correct use of namespaces you can eliminate need of this feature. with a proper naming and grouping of types in namespaces you will no longer pollute namespaces, please don't get obsessed as long as these types are internal and it wont affect users experience, neither yours. don't worry to write namespaces with only internal types. when you reference a compiled dll of your class library those namespaces will not show up in intellisense. |
Beta Was this translation helpful? Give feedback.
-
One of the goals of the other proposal is dependency management. Today you can use assemblies to enforce decoupling (no cyclic dependencies) and encapsulation ( |
Beta Was this translation helpful? Give feedback.
-
To be clear, I don't split my code into 1000 projects. I don't think many developers do. The build time isn't worth it, even if you get better decoupling and encapsulation. But I do segment large projects into many namespaces. Turning those into components would be a huge help for dependency management. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw I already have given you an answer: write code analyzer, 30 minutes of coding and you're done. |
Beta Was this translation helpful? Give feedback.
-
@Pzixel I understood that. Obviously an analyzer would work. The other proposal was even based on an NDepend "analyzer" that did this years before Roslyn existed. Including this mechanism within the shipping product standardizes it. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw then you could just create a nuget package with this analyzer and share it whenever you want :) Seriously, I don't think we need this feature to be built in the language itself. |
Beta Was this translation helpful? Give feedback.
-
The issue i have is this: That's a fine idea, but i don't know why it is tied to a namespace. For example, i may still have a logical component that is spread over a few namespaces. I may do this for my own organizational reasons, or because it makes sense for my public API. Common things may go in my root namespace, with much more specialized stuff in my child namespaces. It sounds like you'd actually want something totally unrelated to accomplish this. Something like:
That said, i think this sort of thing always breaks down. Eventually you have strange venn diagrams of things you want to be visible (i.e. Component A and B should see each other, and B and C shoudl as well, but not A and C). So i think it's sorta worthless in the end. Today, we have an actual solution throughout the toolchain: Use assemblies, and use InternalsVisibleTo to indicate when components do share between them. You can then merge those assemblies at the end of the day if you don't want to actually ship that way. This, to me, seems like a good enough solution without having to go spend an enormous effort to yet against try to come up with a componentization model. |
Beta Was this translation helpful? Give feedback.
-
That would be even better for parallelization. Lots of opportunity there :) |
Beta Was this translation helpful? Give feedback.
-
I would like to see something like this as well. Here is how I would like to see it done: namespace RootNamespace.InnerNamespace
{
private class PrivateClass { }
protected class ProtectedClass { }
}
namespace RootNamespace
{
// PrivateClass is not accessible
// ProtectedClass is not accessible
}
namespace RootNamespace.InnerNamespace
{
// PrivateClass is accessible
// ProtectedClass is accessible
}
namespace RootNamespace.InnerNamespace.InnerNamespace2
{
// PrivateClass is not accessible
// ProtectedClass is accessible
} |
Beta Was this translation helpful? Give feedback.
-
@ChristopherHaws this is how it works today, isn't it? 😉 |
Beta Was this translation helpful? Give feedback.
-
@Pzixel No. Currently you can't have private or protected classes directly in namespaces, only in classes. |
Beta Was this translation helpful? Give feedback.
-
What you want is "internal". Can you please elaborate more on whats the use of private/protected class in namespace level? @ChristopherHaws |
Beta Was this translation helpful? Give feedback.
-
@ChristopherHaws There's no way to express that in the IL: top-level types are either |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr Does this really need to be featured in IL? I mean, if this was just a c# language feature (making namespace level private in IL) that would be enough for me. |
Beta Was this translation helpful? Give feedback.
-
It is possible to mix languages in a single assembly (though VS doesn't currently support this directly). So you'd have the situation where eg a web project containing C# and VB.Net code would have different accessibility rules depending on the language used. Sounds messy. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno I don't think that types declared with the |
Beta Was this translation helpful? Give feedback.
-
That's not what I said. You can mix languages in the same assembly, so that C# code would be unable to access types that the VB.Net code, in the same assembly, would be able to access. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno how's that possible? and if so, why does both csproj and vbproj exist? |
Beta Was this translation helpful? Give feedback.
-
@Logerfo this produce different assemblies http://msdn.microsoft.com/en-us/library/ms366714.aspx Using MSBuild and custom task you can force two different languages to be merged in one single assembly. The supplied .targets force the projects to be single language - but there's no runtime or tooling restriction preventing this. Both the VB and CS compilers can output to modules - the CLR's version of .obj files. Using the assembly linker, you could take the modules from the VB and CS code and produce a single assembly. |
Beta Was this translation helpful? Give feedback.
-
Plus, everyone is familiar with ILMerge, right? |
Beta Was this translation helpful? Give feedback.
-
Did anybody considered something like Haskell or ML style modules? Am not sure how they differ from VB.Net modules. |
Beta Was this translation helpful? Give feedback.
-
The suggested namespace modifier would fill the gap between the current protected and internal modifiers. There is no clean easy workaround for the lack of safe encapsulation at namespace branch (subsystem) level. I don't care for cross-assembly so what equates to "internal namespace" is fine and would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Limit the scope of a class or interface to namespace level.
e.g.
internal namespace class MyClass
or
internal namespace interface MyInterface
These would only be accessible to classes within the same namespace.
Thanks
Nick
Beta Was this translation helpful? Give feedback.
All reactions