依赖于字符串值的业务逻辑
在我正在进行的一个项目中,我使用的是 Entity Framework 4.1(代码优先)。我在两个实体之间有如下关系:
public class Project
{
public int Id { get; set; }
// snip...
// Foreign Key
public string ProjectId { get; set; }
// navigation proeprty
public virtual ProjectType ProjectType { get; set; }
}
public class ProjectType
{
public string Id { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
现在我的业务逻辑取决于正在创建/编辑的项目类型,所以我有这样的代码:
if( "P".Equals(project.ProjectTypeId) )
// logic goes here
是否有其他不依赖于我的方法来做到这一点比较字符串值?
In one of my projects I am working on I am using Entity Framework 4.1 (Code First). I have a relationship between two entities like the following:
public class Project
{
public int Id { get; set; }
// snip...
// Foreign Key
public string ProjectId { get; set; }
// navigation proeprty
public virtual ProjectType ProjectType { get; set; }
}
public class ProjectType
{
public string Id { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
Right now I business logic that depends on what type of project is being created/edited so I have code like this:
if( "P".Equals(project.ProjectTypeId) )
// logic goes here
Is there some other way to do this that doesn't rely on me comparing string values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我个人更喜欢将 ProjectTypeId 转换为枚举类型。
我假设您有固定数量的 ProjectType,并且您的代码应该了解所有这些类型。当您需要查看所有可以使用的项目类型时,此方法为您提供了一个单一的“事实来源”。与其他选项(例如具有字符串常量的类)相比,我更喜欢此选项,因为:
ProjectType
作为强类型参数传递给实用程序函数等。I'd personally prefer converting ProjectTypeId to an enum type.
I'm assuming that you have a fixed number of ProjectTypes, and that your code is supposed to be aware of all of them. This approach gives you a single "source of truth" to look at when you need to see all the ProjectTypes that can be used. I prefer this over other options like a class with string constants because:
ProjectType
s around as strongly-typed parameters to utility functions and such.我知道这个问题已经得到了回答,但我们使用与枚举略有不同的方法:
您仍然拥有单一的事实来源。与枚举一样,常量是在编译时定义的。因此,您的客户端代码将如下所示:
它本质上执行相同的操作,但不需要
Enum.Parse
。I know this has already been answered, but we use a slightly different approach than enums:
You still have a single source of truth. Like enums, consts are defined at compile time. So your client code would look like this:
It essentially does the same thing, but without the need for
Enum.Parse
.我同意奥斯汀的观点,你真的应该有类似的东西......
公共类项目
{
公共 int Id { 得到;放; }
}
公共类 ProjectTypeA : IProjectType
{
公共字符串 Id { 获取;放; }
}
公共类 ProjectTypeB : IProjectType
{
公共字符串 Id { 获取;放;然后
if
你可以有类似
(p.ProjectType is ProjectTypeB )
{}
或对于链接
varprojects = from p in Project.ofType select p;
I agree with Austin that you should really have something like..
public class Project
{
public int Id { get; set; }
}
public class ProjectTypeA : IProjectType
{
public string Id { get; set; }
}
public class ProjectTypeB : IProjectType
{
public string Id { get; set; }
}
Then you can have something like
if (p.ProjectType is ProjectTypeB )
{}
or for link
var projects = from p in Project.ofType select p;