我怎样才能隐藏“setters”?除了一个集会之外的所有集会?

发布于 2024-09-26 21:51:18 字数 1006 浏览 1 评论 0原文

我在我的其他问题,但我认为值得将其分解为自己的问题,因为它并不真正依赖于我提到的其他场景。

无论如何-到Q上,不知道这是否可能。寻找解决方案/解决方法。

我有一个类库,除了 POCO 之外什么都没有:

MyCompany.MyProject.Domain.POCO

这个程序集有一个像这样的 POCO:

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

现在,我有另一个类库,它是我的 DAL/存储库,并使用 Entity Framework 4.0 进行持久化:

MyCompany.MyProject.Repositories

这个程序集有一个对POCO 项目,因为它需要对 POCO 执行 CRUD 操作(抓取 DB 对象、投影到 POCO 中、返回以及修改 POCO)。

现在,我还有一个 Web 应用程序,它引用了 POCO 和存储库程序集。

如果我这样做:

somePOCO.PostId = 10;

我得到一个 SQLException,因为 PostId 是数据库中的 IDENTITY 字段,因此不应显式设置。

有没有办法隐藏这些特殊属性的setter,以便只有存储库才能访问setter?

我想不出一种方法来使用常规的可访问性修饰符(因为它们都不适合这种情况),你们能想到解决方法吗?

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.

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

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

MyCompany.MyProject.Domain.POCO

This assembly has a POCO like this:

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

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

MyCompany.MyProject.Repositories

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).

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

If i do this:

somePOCO.PostId = 10;

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光下的泡沫是彩色的 2024-10-03 21:51:18

您可以将设置器设置为内部,并使内部对存储库程序集可见

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

这意味着所有内容都可以“获取”这些属性,但只有此程序集以及包含存储库的程序集可以“设置”

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'

您的好友蓝忘机已上羡 2024-10-03 21:51:18

将所有 setter 标记为内部,然后将 InternalsVisibleTo 属性添加到 POCO 程序集中:

 [assembly: InternalsVisibleTo( "MyCompany.MyProject.Repositories" )]  

Mark all your setters as internal, then add the InternalsVisibleTo Attribute to your POCOs assembly:

 [assembly: InternalsVisibleTo( "MyCompany.MyProject.Repositories" )]  
简单 2024-10-03 21:51:18

卢克·谢弗的是很好的答案。满足题目的技术要求。但我建议谨慎行事,因为这可能无法解决将来的问题。

并不是建议过度工程,但也许您可能需要考虑进一步抽象出您对这些具有可设置 ID 属性的对象所做的事情。用这个属性装饰创建这种“可见性”可能就足够了,但我认为这是解决问题的一种方法。将这些对象与其他对象之间的交互封装起来可能会更干净,从而从使用代码中删除 setter 访问。

Luke Schafer's is a good answer. It satisfies the technical requirement of the question. But I'd advise caution simply because this may not address the problem in the future.

Not to suggest over engineering but perhaps you might want to think about abstracting away further what you are doing with these objects that have the settable ID property. It may suffice to create this 'visibility' with this attribute decoration but i think it's sort of a hack for the problem. It might be cleaner to encapsulate the interaction between these objects with something else, thus removing the setter access from consuming code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文