我怎么能隐藏];制定者"从所有,但一个程序集?制定者、程序、我怎么能、QUOT

2023-09-04 00:48:23 作者:所念皆星河

我在my另一个问题,但我认为这是值得的打破它到它自己的问题,因为它不是真正依赖于其他场景我提到的。

I have alluded to this issue in my other question, but i think it's worthwhile breaking it out into its own question, as it's not really dependant on the other scenarios i mentioned.

不管怎样 - 到Q,不知道这是可能的。寻找一个解决方案/变通办法。

Anyways - onto the Q, don't know if this is possible. Looking for a solution/workaround.

我有一个类库,什么也没有,但POCO的:

I have a Class Library, with nothing but POCO's:

MyCompany.MyProject.Domain.POCO

该组件具有POCO这样的:

This assembly has a POCO like this:

public class Post
{
   public int PostId { get; set; }
   public int Name { get; set; }
      ...
}

现在,我还有一个类库,这是我的DAL /存储库,并使用实体框架4.0的持久性:

Now, i have another Class Library, which is my DAL/Repository, and uses Entity Framework 4.0 for the persistence:

MyCompany.MyProject.Repositories

该组件具有参考POCO项目,因为它需要在POCO的是执行CRUD操作(抢DB对象,项目到POCO的,回报率,以及修改POCO的)。

This assembly has a reference to the POCO project, as it needs to be perform CRUD operations on the POCO's (grab DB objects, project into POCO's, return, as well as modify POCO's).

现在,我也有一个 Web应用程序,它引用了两个POCO和库组件。

Now, i also have a Web Application, which has a reference to both the POCO and Repository assembly.

如果我这样做:

somePOCO.PostId = 10;

我得到的SQLException,如 PostId 在数据库中的标识字段,因此不应该被明确地设定。

I get a SQLException, as PostId is an IDENTITY field in the database, and hence should not be explicity set.

有没有一种方法可以让我的隐藏的制定者对于这些特殊的性质,所以只有存储库访问二传手?

Is there a way i can hide the setter for these special properties, so that only the repository has access to the setter?

我不能想办法把定期访问修饰符做(因为他们没有适应的情况下),你们能想到一个变通办法的?

I can't think of a way to do it with regular accessibility modifiers (as none of them suit the scenario), can you guys think of a workaround?

推荐答案

你可以做二传手内部,使内部可见的存储库组件

you could make the setter internal, and make the internals visible to the repository assembly

[assembly: InternalsVisibleTo("MyLibrary.Repositories")]
public class Post
{
   public int PostId { get; internal set; }
   public int Name { get; internal set; }
      ...
}

这意味着一切都可以'得到'这些属性,但仅此集会,而且,可以'设置'包含存储库中的组件

This means that everything can 'get' those properties, but only this assembly, and the assembly containing the repository, can 'set'