2

In my ASP.NET classic WebForms application, I have a class that returns a list of Role objects that I use for mapping the roles of the users in my database.

I wrote this static class:

public static class RoleHelper
{
    public static List<RoleValue> getRoles()
    {
        List<RoleValue> myroles = null;
        string connectionString = ConfigurationManager.ConnectionStrings["SQLConnStr"].ConnectionString;
        if (!string.IsNullOrWhiteSpace(connectionString))
        {
            using (SqlConnection dbconn = new SqlConnection(connectionString))
            {
                dbconn.Open();
                SqlDataAdapter cmd = new SqlDataAdapter(" SELECT groupID,Name FROM Gruppi", dbconn);
                cmd.SelectCommand.CommandType = CommandType.Text;
                DataSet ds = new DataSet();
                cmd.Fill(ds);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    myroles = new List<RoleValue>();

                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        RoleValue myrole = new RoleValue();
                        myrole.roleID = (int)row["groupID"]; ;
                        myrole.roleName = (string)row["Name"]; ;
                        myroles.Add(myrole);
                    }
                }
                dbconn.Close();
            }
        }
        return myroles;
    }
}

At first I wrote:

List(RoleValue) myroles = null; 

Is this wrong?

In the calling function, I check if (rolesList.Count > 0) but I should check if(!rolesList is null) but null isn't allowed for lists?


Hibernate: bidirectionality vs. unidirectionality, depending on relationship

Reading a wiki page about Hibernate I elaborated some perplexing conclusions:

1) Bidirectionality is reccomended in one-to-many

2) Bidirectionality is optional in many-to-one

3) Bidirectionality is normally present in many-to-many

4) Unidirectionality is reccomended in one-to-one relationships, 
using as owner class the one with the primary key of the 
relation (not the foreign key).

Are these statements true? Do you have any example to explain why in some cases unidirectionality is reccomended and in others bidirectionality is reccomended instead?

Here's the wiki page (read under "concepts"):

http://wiki.elvanor.net/index.php/Hibernate

4

5 回答 5

4

将列表初始化为 并没有错null,但更广泛地使用这种方式:

List<RoleValue> myroles = new List<RoleValue>();

然后您将返回列表,调用者将检查列表的长度以查看是否为空,如下所示:

List<RoleValue> listOfRoles = getRoles();

if(listOfRoles.Count == 0)
{
    // Report message to user if having no roles is worthy of a notification
}

返回列表实例的优势在于null,用户将执行的大多数操作无需检查 就可以工作null,例如数据绑定和迭代列表。

于 2013-08-12T15:04:55.443 回答
1

我假设在正常情况下数据库访问有效,所以你会到达

myroles = new  List<RoleValue>();

因此,您可能已经在顶部创建此实例,而不是

List<RoleValue> myroles = null;

优点:调用者可以遍历所有角色,如果集合为空,则根本不会创建输出。

于 2013-08-12T15:05:06.350 回答
0

按照惯例,您应该将其初始化为一个空列表,而不是null,并检查它是否有任何项目,而不是查看它是否在null其他地方。这避免了使用该类型检查 null 的所有代码的需要,如果没有项目,大多数操作应该foreach正常工作(即什么都不做),例如.

于 2013-08-12T15:04:33.930 回答
0

返回一个包含 0 个对象的列表并返回 null 都是完全有效的。您需要关注的是调用者对方法的期望。他们应该收到一个空列表吗?这将使他们避免必须先进行空检查。

于 2013-08-12T15:05:00.553 回答
0

一般来说,它可能为空。但我认为你的 if 子句总是正确的。这样这条线就会完成 myroles = new List(); 然后它不是空的。最好要求 .Count 属性

于 2013-08-12T15:06:19.687 回答