如何使用.NET MVC和c#从配置文件中的列表中引用一个特定的对象

时间:2022-02-02 16:54:32

I'm writing a setup sequence where I'm populating the required data for a new site in a multi-tenancy CMS. However, while I'm quite familiar with the data design and all the web front end, I'm extremely new to actual coding.

我正在编写一个设置序列,在一个多租户CMS中为一个新站点填充所需的数据。然而,尽管我非常熟悉数据设计和所有的web前端,但我对实际的编码非常陌生。

As you can see from the code below, I have a 'for each' expression for creating my roles that are in a list in the config.cs - these create beautifully.

从下面的代码中可以看到,我有一个“for each”表达式,用于创建配置中列表中的角色。cs -它们创造得很漂亮。

For each default user however, I need to assign one of the values from the user roles that I just created by name. I think this might have something to do with LINQ, but I'm not sure how to break this out. Any suggestions?

但是,对于每个默认用户,我需要从用户角色中分配一个值,这个值是我刚才通过名称创建的。我认为这可能与LINQ有关,但我不知道该如何解决这个问题。有什么建议吗?

Here is the code:

这是代码:

 foreach (var userRoleName in _configData.UserRoles)
        {
            var newUserRole = new UserRole(newSite, userRoleName);
            _daoFactory.GetUserRoleDao().Save(newUserRole);
        }

        _daoFactory.GetUserRoleDao().FlushChanges();

        //Thinking this is not needed?
        Thread.Sleep(500);

        //var newSiteCopy = _daoFactory.GetSiteDao().GetById(newSite.Id);
      _daoFactory.GetSiteDao().Refresh(newSite);

        foreach (var defaultUser in _configData.Users)
        {
            var newUser = new User(newSite, defaultUser.FullName, defaultUser.Login, defaultUser.Password);

            _daoFactory.GetUserDao().Save(newUser);

        }

        _daoFactory.GetUserDao().FlushChanges();

Here is the config.cs code: (The actual values will be moved out of this file into a doc but are here for dev purposes only.)

这是配置。cs代码:(实际值将从这个文件中移到一个doc中,但这里只是用于开发目的。)

 public class ConfigData
{
    public ConfigData()
    {
        SystemDomainDefault = ".joyatechsolutions.com";
        UserRoles = new List<string>{"Administrator","Editor","Public","God"};
        Users = new List<DefaultUser>
                    {
                        new DefaultUser { FullName = "Default Public User", Login = "", Password = "", UserRoleName = "Public"},
                        new DefaultUser { FullName = "Mary Camacho", Login = "mc", Password = "test1", UserRoleName = "God"},
                        new DefaultUser { FullName = "Mary", Login = "mn", Password = "test2", UserRoleName = "God"},
                    };

    }
    public List<DefaultUser> Users { get; set; }
    public string SystemDomainDefault { get; set; }
    public List<string> UserRoles { get; set; }
}

public class DefaultUser
    {
        public string FullName { get; set; }
        public string Login { get; set; }
        public string Password { get; set; }
        public string UserRoleName { get; set; }
    }

2 个解决方案

#1


1  

I think I may have left out some important details that would have been helpful in answering the question I posed and I received some help from a colleague last night so this is how we solved the problem:

我想我可能漏掉了一些重要的细节,这些细节对于回答我提出的问题是有帮助的,昨晚我从一位同事那里得到了一些帮助,这就是我们解决问题的方法:

    User defaultPublicUser = null;
        foreach (var newUserData in _configData.Users)
        {
            var newUser = new User(newSite, newUserData.FullName, newUserData.Login, newUserData.Password);
            var userRole = newSite.UserRoles.First(ur => ur.Name == newUserData.UserRoleName);
            if (newUserData.UserRoleName == "Public")
            {
                defaultPublicUser = newUser;
            }
            newUser.UserRoles.Add(userRole);
            newUser.UserStatusType = _daoFactory.GetUserStatusTypeDao().GetById(UserStatusType.cActive);
            _daoFactory.GetUserDao().Save(newUser);
        }
        _daoFactory.GetUserDao().FlushChanges();

Essentially, we had a new Site object that held the saved UserRoles and we used LINQ to query that object and return the value that we then associated with the new User. You can see in the config.cs from above that we already identified what the name of the role should be for each new user - so we are finding that name using '.first' in the userRoleName and then associating the saved role to the user.

本质上,我们有一个新的站点对象,它保存了保存的用户角色,我们使用LINQ查询该对象并返回我们随后与新用户关联的值。可以在配置中看到。从上面的cs中,我们已经为每个新用户确定了角色的名称——因此我们使用'。首先在userRoleName中,然后将保存的角色关联到用户。

We were able to use the '.First' term and because this code will strictly be used during a site setup process and we know that the only user roles that will exist for the new site are the ones that we just created inside of this sequence. This would not be robust enough for more general code.

我们能够使用。第一个术语,因为这个代码将严格地在站点设置过程中使用,我们知道新站点将存在的唯一用户角色是我们刚刚在这个序列中创建的。这对于更一般的代码来说不够健壮。

Finally, not related to the question, we added an if statement so that that we could associate the public user to the site. Again, this works in the site setup only because we know that only one public user exists for the site at the time this code is run.

最后,与问题无关,我们添加了一个if语句,以便我们可以将公共用户与站点关联起来。同样,这在站点设置中是有效的,因为我们知道在运行代码时,站点只存在一个公共用户。

#2


0  

Can you define UserRoles as:

您可以定义UserRoles为:

enum UserRoles
{
    Administrator,
    Editor,
    Public,
    God
}

That way you can get the name of the role by doing this:

这样你就可以通过这样的方式得到这个角色的名字:

UserRoles role = UserRoles.Administrator;
string roleName = role.Tostring();

And get the enum type from String like this:

从字符串中获取enum类型如下:

(UserRoles)Enum.Parse(typeof(UserRoles), "Administrator");

#1


1  

I think I may have left out some important details that would have been helpful in answering the question I posed and I received some help from a colleague last night so this is how we solved the problem:

我想我可能漏掉了一些重要的细节,这些细节对于回答我提出的问题是有帮助的,昨晚我从一位同事那里得到了一些帮助,这就是我们解决问题的方法:

    User defaultPublicUser = null;
        foreach (var newUserData in _configData.Users)
        {
            var newUser = new User(newSite, newUserData.FullName, newUserData.Login, newUserData.Password);
            var userRole = newSite.UserRoles.First(ur => ur.Name == newUserData.UserRoleName);
            if (newUserData.UserRoleName == "Public")
            {
                defaultPublicUser = newUser;
            }
            newUser.UserRoles.Add(userRole);
            newUser.UserStatusType = _daoFactory.GetUserStatusTypeDao().GetById(UserStatusType.cActive);
            _daoFactory.GetUserDao().Save(newUser);
        }
        _daoFactory.GetUserDao().FlushChanges();

Essentially, we had a new Site object that held the saved UserRoles and we used LINQ to query that object and return the value that we then associated with the new User. You can see in the config.cs from above that we already identified what the name of the role should be for each new user - so we are finding that name using '.first' in the userRoleName and then associating the saved role to the user.

本质上,我们有一个新的站点对象,它保存了保存的用户角色,我们使用LINQ查询该对象并返回我们随后与新用户关联的值。可以在配置中看到。从上面的cs中,我们已经为每个新用户确定了角色的名称——因此我们使用'。首先在userRoleName中,然后将保存的角色关联到用户。

We were able to use the '.First' term and because this code will strictly be used during a site setup process and we know that the only user roles that will exist for the new site are the ones that we just created inside of this sequence. This would not be robust enough for more general code.

我们能够使用。第一个术语,因为这个代码将严格地在站点设置过程中使用,我们知道新站点将存在的唯一用户角色是我们刚刚在这个序列中创建的。这对于更一般的代码来说不够健壮。

Finally, not related to the question, we added an if statement so that that we could associate the public user to the site. Again, this works in the site setup only because we know that only one public user exists for the site at the time this code is run.

最后,与问题无关,我们添加了一个if语句,以便我们可以将公共用户与站点关联起来。同样,这在站点设置中是有效的,因为我们知道在运行代码时,站点只存在一个公共用户。

#2


0  

Can you define UserRoles as:

您可以定义UserRoles为:

enum UserRoles
{
    Administrator,
    Editor,
    Public,
    God
}

That way you can get the name of the role by doing this:

这样你就可以通过这样的方式得到这个角色的名字:

UserRoles role = UserRoles.Administrator;
string roleName = role.Tostring();

And get the enum type from String like this:

从字符串中获取enum类型如下:

(UserRoles)Enum.Parse(typeof(UserRoles), "Administrator");