C# DAL类和业务层类
嗨,
你能告诉我这是否可能吗?
public class Person
{
public string Name { get; set; }
public int ID { get; set; }
}
填充一个类调用,比如位于名为 Entities 的程序集中的 person,这样,代码的填充在名为 DataAccessLayer 的不同程序集中完成(因此 person 和填充它的位置不在同一个程序集中)
//下面代码将从数据读取器等读取,但这样做只是为了使其易于解释。
Person p=new Person();
p.Name="tom";
p.id = 10;
现在,另一个系统可以访问 person 类,以便他们能够访问 person。我想要的是防止其他系统能够更改 ID。能读但不能写。我是否需要创建另一个类等来允许此操作,并且仅将此类公开给其他系统(即业务对象)(即 ORM)?
我知道很多人会说只需将 ID 设置为只读即可。即,
public int ID { get; }
但如果我这样做,那么我无法从类似于上面的代码填充 ID,因为在我的 DataAccessLayer 中,我将无法设置 ID,因为它是只读的。
谢谢 尼尔
HI,
can you tell me if this is possible.
public class Person
{
public string Name { get; set; }
public int ID { get; set; }
}
Populate a class call say person which is in an assembly called Entities like this with the population of the code being done in a different assembly called DataAccessLayer (so person and the place where it is populated are not in the same assembly)
//the below code would be reading from a datareader etc but have just done this to make it //easy to explain.
Person p=new Person();
p.Name="tom";
p.id = 10;
The person class is now to be made accessible to another system to allow them to be able to access person. What i would like is to prevent the other system from being able to change the ID. be able to read it but not write. Do i need to create another class etc to allow this and only expose this class to the other system (i.e. a business object) (i.e. ORM)?
i know alot of people are going to say just make the ID readonly. i.e.
public int ID { get; }
but if i do this then i cannot populate the ID from the code similar to above because in my DataAccessLayer i will not be able to set the ID as it is readonly.
thanks
Niall
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为可将 ID 传递到其中的对象创建一个
内部
构造函数,然后为实体 DLL 设置标志,以允许另一个 DLL (DataAccessLayer) 能够查看并使用此构造函数中的内部调用DLL。 (InternalsVisibleTo
属性)You can create an
internal
constructor for the object that you can pass ID into, then set the flag for the Entities DLL that allows another DLL (DataAccessLayer) to be able to see and use the internal calls within this DLL. (InternalsVisibleTo
attribute)寻找将为您分配实体 ID 的 ORM 工具,您的 id 属性将如下所示:
如果您选择这种方式,则无需担心分配属性和类型转换。
Look toward ORM tools which will assign ID of entity for you and your id property will look:
if you choose this way, you don't need to worry about assigning properties and casting of types.