简体   繁体   English

限制对特定程序集的访问

[英]Restrict access to a specific assembly

I'm working on a Winforms project with sql server, splitted in several assemblies. 我正在使用sql server进行Winforms项目,分成几个程序集。

The first assembly Entities contains DTO like : 第一个程序集Entities包含DTO,如:

public class Attribution
{
    public short UserId { get; set; }
    public User User { get; set; }
}
public class User
{
    public short Id { get; set; }
}

The second assembly Repository is accessing Sql Server database. 第二个程序集Repository正在访问Sql Server数据库。

The third assembly Service is the link between previous. 第三个组装Service是之前的链接。

There are other layers but this is not the point. 还有其他层,但这不是重点。 I need of course DTO's everywhere in the app. 我当然需要DTO在应用程序的任何地方。

In sql server, Attribution.UserId and User.Id are the same datas, located in 2 separate tables, linked by Ìnner join . 在sql server中, Attribution.UserIdUser.Id是相同的数据,位于2个单独的表中,由Ìnner join链接。

Attribution.UserId must be public because I need access from Repository , Service , etc... But I don't need it in the "logical" part of the app, what I need is Attribution.User . Attribution.UserId必须是公共的,因为我需要从RepositoryService等访问...但我不需要它在应用程序的“逻辑”部分,我需要的是Attribution.User

At this time I have a UserService class in which there is a GetUser() method and I call this method to get the user in my AttributionService.GetAttribution() method. 这时我有一个UserService类,其中有一个GetUser()方法,我调用此方法来获取我的AttributionService.GetAttribution()方法中的用户。

Is there a way to restrict access to Attribution.UserId property to Service assembly? 有没有办法将Attribution.UserId属性的访问权限限制为Service程序集? Or is it a kind of "good practice violation" to query a User DTO in AttributionService class? 或者在AttributionService类中查询User DTO是一种“良好做法违规”?

Many thanks for your recommandation. 非常感谢您的推荐。

` `

One option would be to make the set of the property internal and the use the InternalsVisibleTo attribute to grant access to internals to the Repository assembly. 一种选择是将属性setinternal并使用InternalsVisibleTo属性授予对Repository程序集内部的访问权限。

Another less technical and more logical option would be to make the setter private and let the only way for it to be modified be the classes constructor. 另一个技术性较强且逻辑性较强的选项是将setter设为私有,并让它被修改的唯一方法是类构造函数。 That way, your repository can get users built, but nobody can modify the ID later. 这样,您的存储库可以构建用户,但是之后没有人可以修改ID。

As a last option you could create an interface that contains only what the non-repository classes should have access to and pass this around. 作为最后一个选项,您可以创建一个仅包含非存储库类应该访问的接口并传递它的接口。 I'm not a big fan because that means you have to cast it back to your concrete class in the repository and that basically means your repository is lying (saying it accepts an ISomething , but then throwing if the ISomething is not the exact, concrete Something it expects). 我不是一个大粉丝,因为这意味着你必须将它强制转换回存储库中的具体类,这基本上意味着你的存储库正在撒谎(说它接受了一个ISomething ,但是如果ISomething不是精确的那么扔掉它预期的Something )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM