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.
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 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?
Here is the code:
foreach (var userRoleName in _configData.UserRoles)
开发者_C百科 {
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.)
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; }
}
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.
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.
Can you define UserRoles as:
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:
(UserRoles)Enum.Parse(typeof(UserRoles), "Administrator");
精彩评论