非常基础的问题,但是我在搜索谷歌时失败了。
我有一个域对象,比如说User,在这个对象上我希望Active属性只读。然而,当从数据库中加载时,该属性需要通过EF(6)进行设置。
相关表格:
CREATE TABLE Users (
ID INT NOT NULL PRIMARY KEY CLUSTERED,
Active BIT NOT NULL DEFAULT 1
-- other stuff
);
类:
public class User
{
public int ID { get; set; }
public bool Active { get; set; } // this should be RO
public void Activate() {
Active = true;
this.AddChangelog(ChangeType.Activation, "Activated");
}
public void Deactivate() {
Active = false;
this.AddChangelog(ChangeType.Activation, "Deactivated");
}
// changelogging, etc.
}
开发人员不应直接更改
Active
,而应使用Activate()
和Deactivate()
方法。 但是,EF6需要直接设置Active
以便实例化对象。架构(如果重要):
- User类存在于
Domain
项目中
- EF6类配置是通过Data
项目完成的
- EF6使用Fluent API配置(将ORM内容保留在我的域项目中)
- EF6不使用DTO,而是直接配置域类问题:
如何强制执行(或至少警告)开发人员不设置
user.Active
? 至少,我能否为setter提供一些编译时警告(类似于[Obsolete]
),以便他们收到通知?谢谢,