简体   繁体   English

C#对象/设计基础

[英]c# object/design fundamentals

Is this a standard, good practice way of doing things? 这是一种标准的,良好的做法吗? Basically return a list of itself? 基本上返回本身的列表? Should the actual fields (id, title, etc) be a separate class? 实际字段(ID,标题等)是否应该是单独的类? (I've seen people call it DTO objects) (我见过人们称其为DTO对象)

I'm starting a project & I want to try & get some of these fundamentals down-- 我正在开始一个项目,我想尝试并逐步降低这些基本知识-

Thanks!! 谢谢!!

public class Calendar
{
    public int id { get; set; }
    public string title { get; set; }

    public List<calendar> GetAll()
    {
        var list = new List<calendar>();

        var db = new mssql2();
        db.set("s1");
        string sql = @"select * from [cal]";
        var dr = db.dr(sql);
        while (dr.Read())
        {
           var e = new calendar();
           e.id = (int)dr["id"];
           e.title = dr["title"].ToString();
           list.Add(e);
        }
        return list;
    }
}

You seem to be mixing your Domain model with your Data Access layer. 您似乎正在将Domain模型与Data Access层混合在一起。

Keep Calendar as it's own class, and maybe make another class called CalendarService or CalendarRepository that returns you a list of Calendar objects. 将Calendar保留为其自己的类,并可能使另一个类称为CalendarServiceCalendarRepository ,该类将为您返回Calendar对象的列表。

Here is an example: 这是一个例子:

public class Calendar
{
    public Calendar() { }
    public Calendar(int id, string title)
    {
       Id = id;
       Title = title;
    }
    public int Id { get; set; }
    public string Title { get; set; }
}

public class CalendarService
{
    public static List<Calendar> GetAll()
    {
        var list = new List<Calendar>();

        var db = new mssql2();
        db.set("s1");
        string sql = @"select * from [cal]";
        var dr = db.dr(sql);
        while (dr.Read())
        {
            // Use the constructor to create a new Calendar item
            list.Add(new Calendar((int)dr["id"], dr["title"].ToString()));
        }
        return list;
    }
}

The general idea is for the classes to represent domain objects , and class members various properties of those domain objects . 一般的想法是,类代表域对象 ,并且类成员具有这些域对象的各种属性 Class functions would represent what the objects can do . 类函数将表示对象可以做什么

In your case, it might be more fitting to remove the get_all() to a some class abstracting database operations. 在您的情况下,将get_all()删除到一些抽象数据库操作的类上可能更合适。 Calendar would have the functionalities of a calendar (getting/setting some dates, getting skip years, getting/setting some appointments); Calendar具有Calendar的功能(获取/设置一些日期,获取跳过的年份,获取/设置一些约会); depending of what you want to accomplish with a calendar. 取决于您要完成的日历。

对象设计

You're tightly coupling data access, and your "get_all" method isn't even using anything from the object of type calendar. 您正在紧密耦合数据访问,并且您的“ get_all”方法甚至没有使用日历类型的对象中的任何东西。 If, as in this case, your method doesn't use any data from the instance of the class to which it belongs, then that method should either not be there, or should be a static method. 如果在这种情况下,您的方法不使用其所属类的实例中的任何数据,则该方法应不存在,或应为静态方法。 My preference would be for the former -- have a class whose intent is to retrieve a calendar or calendars from the database. 我的偏好是使用前者-拥有一个旨在从数据库中检索一个或多个日历的类。 It is a more sensible organization of code, is more testable, can be more easily abstracted from the data layer, and it also makes your data object more portable. 它是更明智的代码组织,更易于测试,可以更轻松地从数据层抽象出来,并且还使您的数据对象更具可移植性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM