依赖于字符串值的业务逻辑

发布于 2024-12-26 04:24:35 字数 615 浏览 6 评论 0原文

在我正在进行的一个项目中,我使用的是 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 技术交流群。

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

发布评论

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

评论(3

顾铮苏瑾 2025-01-02 04:24:35

我个人更喜欢将 ProjectTypeId 转换为枚举类型。

var projectType = Enum.Parse(typeof(ProjectType), project.ProjectTypeId);
switch(projectType)
{
    case ProjectType.P: // logic goes here
    case ProjectType.N:
        break;
    default: throw new ArgumentOutOfRangeException("That wasn't a valid project type");
}

我假设您有固定数量的 ProjectType,并且您的代码应该了解所有这些类型。当您需要查看所有可以使用的项目类型时,此方法为您提供了一个单一的“事实来源”。与其他选项(例如具有字符串常量的类)相比,我更喜欢此选项,因为:

  1. 如果您发现项目具有无效的项目类型,则更容易“快速失败”。
  2. 您可以将 ProjectType 作为强类型参数传递给实用程序函数等。

I'd personally prefer converting ProjectTypeId to an enum type.

var projectType = Enum.Parse(typeof(ProjectType), project.ProjectTypeId);
switch(projectType)
{
    case ProjectType.P: // logic goes here
    case ProjectType.N:
        break;
    default: throw new ArgumentOutOfRangeException("That wasn't a valid project 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:

  1. It's easier to "fail fast" if you discover that the project has an invalid project type.
  2. You can pass ProjectTypes around as strongly-typed parameters to utility functions and such.
稍尽春風 2025-01-02 04:24:35

我知道这个问题已经得到了回答,但我们使用与枚举略有不同的方法:

public static class ProjectType
{
    public const string P = "P";
    public const string N = "N";
}

您仍然拥有单一的事实来源。与枚举一样,常量是在编译时定义的。因此,您的客户端代码将如下所示:

if( ProjectType.P.Equals(project.ProjectTypeId) )
    // logic goes here

它本质上执行相同的操作,但不需要 Enum.Parse

I know this has already been answered, but we use a slightly different approach than enums:

public static class ProjectType
{
    public const string P = "P";
    public const string N = "N";
}

You still have a single source of truth. Like enums, consts are defined at compile time. So your client code would look like this:

if( ProjectType.P.Equals(project.ProjectTypeId) )
    // logic goes here

It essentially does the same thing, but without the need for Enum.Parse.

护你周全 2025-01-02 04:24:35

我同意奥斯汀的观点,你真的应该有类似的东西......

公共类项目
{
公共 int Id { 得到;放; }

// snip...      

// Foreign Key      
public string ProjectId { get; set; }      

// navigation proeprty      
public virtual IProjectType ProjectType { get; set; }      

}

公共类 ProjectTypeA : IProjectType
{
公共字符串 Id { 获取;放; }

public virtual ICollection<Project> Projects { get; set; } 

}

公共类 ProjectTypeB : IProjectType
{
公共字符串 Id { 获取;放;然后

public virtual ICollection<Project> Projects { get; set; } 

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; }

// snip...      

// Foreign Key      
public string ProjectId { get; set; }      

// navigation proeprty      
public virtual IProjectType ProjectType { get; set; }      

}

public class ProjectTypeA : IProjectType
{
public string Id { get; set; }

public virtual ICollection<Project> Projects { get; set; } 

}

public class ProjectTypeB : IProjectType
{
public string Id { get; set; }

public virtual ICollection<Project> Projects { 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;

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