小编tor*_*nup的帖子

是否有一种"正确"的方式来抽象出我的代码?

我已经在C#中开发了大约12个月了(从头开始,除了一点点PHP脚本黑客之外没有以前的开发经验)我喜欢认为我已经将我的技能发展到可以编写应用程序的级别完美地执行其功能.

但是,我仍然对最佳编码实践感到困惑,我理解这段代码很糟糕:

class Example1
{
    public static Alert GenerateAlert()
    {
        Alert AlertObject = new Alert();

        AlertObject.AlertDatetime = DateTime.Now;
        AlertObject.AlertHasRecords = false;

        return AlertObject;
    }
}
Run Code Online (Sandbox Code Playgroud)

例如,如果AlertDatetime需要的不仅仅是一条简单的线条,DateTime.Now;我将最终填满一个庞大的功能.不好!

但是,我不能看到以下两个例子的问题(我赞成例2)

class Example2
{
    public static Alert AlertObject = new Alert();

    public static Alert GenerateAlert()
    {
        PopulateAlertDate();
        CheckForAlertRecords();

        return AlertObject;
    }

    private static void CheckForAlertRecords()
    {
        AlertObject.AlertHasRecords = false;
    }

    private static void PopulateAlertDate()
    {
        AlertObject.AlertDatetime = DateTime.Now;
    }
}



class Example3
{
    public static Alert GenerateAlert()
    {
        Alert AlertObject …
Run Code Online (Sandbox Code Playgroud)

c# oop

3
推荐指数
1
解决办法
99
查看次数

包含使用 entityFramework 插入的 IEnumerable 属性的模型

我有以下两种型号:

public class Alert
{
    public int Id { get; set; }
    public DateTime AlertDatetime { get; set; }
    public bool Unread { get; set; }
    public int? ReadByUserId { get; set; }
    public DateTime? ReadDateTime { get; set; }
    public DateTime ImportDateTime { get; set; }
    public bool AlertHasRecords { get; set; }

    //Error Reporting and Recording.
    public bool Error { get; set; }
    public string ErrorText { get; set; }

    public virtual IEnumerable<AlertRecord> Records { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework

1
推荐指数
1
解决办法
2045
查看次数

标签 统计

c# ×2

entity-framework ×1

oop ×1