我怎样才能隐藏“setters”?除了一个集会之外的所有集会?
我在我的其他问题,但我认为值得将其分解为自己的问题,因为它并不真正依赖于我提到的其他场景。
无论如何-到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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将设置器设置为内部,并使内部对存储库程序集可见
这意味着所有内容都可以“获取”这些属性,但只有此程序集以及包含存储库的程序集可以“设置”
you could make the setter internal, and make the internals visible to the repository assembly
This means that everything can 'get' those properties, but only this assembly, and the assembly containing the repository, can 'set'
将所有 setter 标记为内部,然后将 InternalsVisibleTo 属性添加到 POCO 程序集中:
Mark all your setters as internal, then add the InternalsVisibleTo Attribute to your POCOs assembly:
卢克·谢弗的是很好的答案。满足题目的技术要求。但我建议谨慎行事,因为这可能无法解决将来的问题。
并不是建议过度工程,但也许您可能需要考虑进一步抽象出您对这些具有可设置 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.