开发者

.NET security mechanism to restrict access between two Types in the same project?

开发者 https://www.devze.com 2022-12-31 22:14 出处:网络
Question Is there a mechanism in the .NET Framework to hide one custom Type from another without using separate projects/assemblies? I\'m not talking about access modifiers to hide members of a Type

Question

Is there a mechanism in the .NET Framework to hide one custom Type from another without using separate projects/assemblies? I'm not talking about access modifiers to hide members of a Type from another type - I mean to hide the Type itself.

Background

I'm working in an ASP.NET Website project and the team has decided not to use separate project assemblies for different software layers. Therefore I'm looking for a way to have, for example, a DataAccess/ folder of which I disallow its classes to access other Types in the same ASP.NET Website project. In other words I want to fake the layers and have some kind of security mechanism around each layer to prevent it from accessing another.

More Info and Details ...

Obviously there's not a way to enforce this restriction using language-specific OO keywords so I am looking for something else, for example: maybe a permission framework or code access mechanism, maybe something that uses meta data like Attributes. Even something that restricts one namespace from accessing another. I'm unsure the final form it might take.

If this were C++ I'd likely be using friend to make as solution, which doesn't translate to C# internal in this case although they're often compared.

I don't really care whet开发者_如何学编程her the solution actually hides Types from each other or just makes them inaccessible; however I don't want to lock down one Type from all others, another reason access modifiers are not a solution. A runtime or design time answer will suffice. Looking for something easy to implement otherwise it's not worth the effort ...


You could use NDepend to do this:

http://www.ndepend.com/

NDepend could allow you to enforce "layering" rules by specifying that certain namespaces should not reference each other. You then plug NDepend and the ruleset into your automated build, and it will fail the build (with a full report) if there are any misdemeanours.

In this way you can enforce logical software layering concepts within an assembly without having to use project structures to do it physically.

Update

I answered the question late last night, and rather literally i.e. how you can directly solve the question. Although a tool can be used to solve the issue, developing in one project across the whole team is more than likely going to be a pretty miserable experience as the project grows:

  • Unless people are incredibly disciplined, the build will keep breaking on layering violations.
  • There will be source control merge thrashing on the VS project file - not pleasant.
  • Your unit of re-use is very large and undefined if you want to share assemblies with other applications\projects you are developing. This could lead to very undesired coupling.

Although I do not advocate having lots of tiny assemblies, a sensible number defined around core concepts is very workable and desirable e.g. "UI", "data access", "business logic", "common library" and "shared types".


Nothing out of the box; there may be some 3rd-party tools that you can use to kludge some rules together, based perhaps on namespaces etc. Something like a custom fx cop rule...

0

精彩评论

暂无评论...
验证码 换一张
取 消