patterncsharpMinor
Player Permission System
Viewed 0 times
playerpermissionsystem
Problem
I have coded a fully working permission system for my gaming emulator to determine permissions for users.
What I really want to know is:
PermissionManager:
```
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sahara.Core.Database;
using Sahara.Core.Logging;
namespace Sahara.Base.Game.Permissions
{
internal class PermissionManager
{
private readonly Dictionary _permissions;
private readonly Dictionary _permissionCommands;
private readonly Dictionary _permissionGroups;
private readonly Dictionary> _permissionGroupRights;
private readonly Dictionary> _permissionSubscriptionRights;
private readonly LogManager _logManager;
public PermissionManager()
{
_permissions = new Dictionary();
_permissionCommands = new Dictionary();
_permissionGroups = new Dictionary();
_permissionGroupRights = new Dictionary>();
_permissionSubscriptionRights = new Dictionary>();
_logManager = Sahara.GetServer().GetLogManager();
InitializePermissions();
}
private void InitializePermissions()
{
using (var mysqlConnection = Sahara.GetServer().GetMySql().GetConnection())
{
LoadPermissions(mysqlConnection);
LoadCommandPermissions(mysqlConnection);
LoadPermissionGroups(mysqlConnection);
LoadPermissionRights(mysqlConnection);
LoadPermissionSubscriptions(mysqlConnection);
}
}
private void LoadPermissions(DatabaseConnection mysqlCon
What I really want to know is:
- Is my code standard good enough?
- Is the design of the class good? (Not sure about so many Initialize methods)
- Are there any parts of the classes that can be improved (even small improvements)?
- Is there anything I could've done better?
PermissionManager:
```
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sahara.Core.Database;
using Sahara.Core.Logging;
namespace Sahara.Base.Game.Permissions
{
internal class PermissionManager
{
private readonly Dictionary _permissions;
private readonly Dictionary _permissionCommands;
private readonly Dictionary _permissionGroups;
private readonly Dictionary> _permissionGroupRights;
private readonly Dictionary> _permissionSubscriptionRights;
private readonly LogManager _logManager;
public PermissionManager()
{
_permissions = new Dictionary();
_permissionCommands = new Dictionary();
_permissionGroups = new Dictionary();
_permissionGroupRights = new Dictionary>();
_permissionSubscriptionRights = new Dictionary>();
_logManager = Sahara.GetServer().GetLogManager();
InitializePermissions();
}
private void InitializePermissions()
{
using (var mysqlConnection = Sahara.GetServer().GetMySql().GetConnection())
{
LoadPermissions(mysqlConnection);
LoadCommandPermissions(mysqlConnection);
LoadPermissionGroups(mysqlConnection);
LoadPermissionRights(mysqlConnection);
LoadPermissionSubscriptions(mysqlConnection);
}
}
private void LoadPermissions(DatabaseConnection mysqlCon
Solution
Default class access modifier
Unless otherwise specified, classes in C# are internal by default. This means that you can remove the
However, if you like explicitly specifying the
Lambda expressions => properties
In your
If you've added this to allow for the reading of the private field, but not writing, then you can simply turn
If this wasn't your original intent, then you can keep the lambda expression.
Proper string formatting
There are many places in your code where you are concatenating values together into strings, like this:
Instead of doing this you can use
If you use string interpolation, then your code will become this:
Take note of the "
Unless otherwise specified, classes in C# are internal by default. This means that you can remove the
internal access modifier in places like these:internal class PermissionManager
internal class Permission
internal class PermissionCommandHowever, if you like explicitly specifying the
internal access modifier, then you can keep it.Lambda expressions => properties
In your
Permission class you declare the following lambda expression which returns the private readonly field _permissionName:public string PermissionName => _permissionName;If you've added this to allow for the reading of the private field, but not writing, then you can simply turn
_permissionName into a property with a private setter, like so:public string PermissionName { get; private set; }If this wasn't your original intent, then you can keep the lambda expression.
Proper string formatting
There are many places in your code where you are concatenating values together into strings, like this:
_logManager.Log("Loaded " + _permissions.Count + " permissions [" + stopwatch.ElapsedMilliseconds + "ms]", LogType.Information);Instead of doing this you can use
string.Format() or string interpolation. Either of these two methods will increase the readability of your code. Using string.Format(), the above code will become this:_logManager.Log(string.Format("Loaded {0} permissions [{1}ms]", _permissions.Count, stopwatch.ElapsedMilliseconds), LogType.Information);If you use string interpolation, then your code will become this:
_logManager.Log($"Loaded {_permissions.Count} permissions [{stopwatch.ElapsedMilliseconds}ms]", LogType.Information);Take note of the "
$" at the beginning of the string in the above example.Code Snippets
internal class PermissionManager
internal class Permission
internal class PermissionCommandpublic string PermissionName => _permissionName;public string PermissionName { get; private set; }_logManager.Log("Loaded " + _permissions.Count + " permissions [" + stopwatch.ElapsedMilliseconds + "ms]", LogType.Information);_logManager.Log(string.Format("Loaded {0} permissions [{1}ms]", _permissions.Count, stopwatch.ElapsedMilliseconds), LogType.Information);Context
StackExchange Code Review Q#145548, answer score: 4
Revisions (0)
No revisions yet.