我的任务是在c#中构建数据集合。
每个数据集都具有以下属性:
属性“类型”和“名称”具有已知且固定的值集。
示例数据:
Type: User
-> Name: Get_Users_List
-> Name: Add_User
-> Name: Enable_User
-> Name: Disable_User
Type: Rights
-> Name: Get_Rights_List
-> Name: Approve_User_Right
......等等。
为了最大限度地减少编码错误,并且因为条目列表已知且已修复,我已为“类型”和“名称”创建了几个枚举。
示例数据:
enumTypes
enumUserFunctions
enumRightFunctions
......等等。
我也知道如何在代码中访问数据:
SomeCollection[enumType.User][enumUserFunctions.Add_User].ServiceString
SomeCollection[enumType.User][enumRightFunctions.Approve_User_Right].HTTPMethod
我正在尝试使用嵌套字典来实现这一目标,但我面临的问题是嵌套字典具有不同的类型,具体取决于我试图存储的数据集类型。
Dictionary<enumTypes, Dictionary<variableEnum, AutoPropertyWithValues>>
我如何轻松实现我的目标?
修改
根本原因是我需要在代码中多次使用一些对象,并且我想实现一种直观的方式来导航它们。这就是我在Sinatr建议之后根据我的根本想法所做的事情。
public class FunctionCollection
{
public UserFunctions UserFunctions { get; } = new UserFunctions();
public BaseDataFunctions BaseDataFunctions { get; } = new BaseDataFunctions();
/* ... and so on ... */
}
public class UserFunctions
{
public RestFunction Do_User_Login { get; set; } = new RestFunction();
public RestFunction Do_User_Logout { get; set; } = new RestFunction();
/* ... */
}
public class BaseDataFunctions
{
public RestFunction Get_Product_List { get; set; } = new RestFunction();
/* ... */
}
public class RestFunction
{
public string ServiceUriString { get; set; }
public HttpVerbs Method { get; set; }
/* ... and so on ... */
}
最后一个问题。 有没有办法直接在定义中设置对象? 例如,在Do_User_Login的定义中设置ServiceUriString和HTTPMethod:
public RestFunction Do_User_Login { get; set; } = new RestFunction();
答案 0 :(得分:1)
条目列表已知且已修复
请勿使用此类动态结构(Dictionary
,List
等)。而不是标准属性,那么您不需要额外的枚举来识别类型:
public class Configuration
{
public User UserA { get; set; } = new User();
public User UserB { get; set; } = new User();
...
}
public class User
{
public string ServiceString { get; set; } = "whatever";
}
用法
var serviceString = configurationInstance.UserA.ServiceString;
如果你需要枚举它们,那么你可以使用反射或添加类似于
的方法public IEnumerable<User> GetUsers()
{
yield return UserA;
yield return UserB;
}
答案 1 :(得分:0)
您可以通过
简化它public class MyConfig
{
private readonly Dictionary<string, Dictionary<string, RestFunction>> _config = new Dictionary<string, Dictionary<string, RestFunction>>
{
[nameof( User )] = new Dictionary<string, RestFunction>
{
[nameof( UserSection.Add_User )] = new RestFunction { HttpMethod = "GET", ServiceString = "foo" },
[nameof( UserSection.Get_Users_List )] = new RestFunction { HttpMethod = "GET", ServiceString = "foo" },
},
[nameof( Rights )] = new Dictionary<string, RestFunction>
{
[nameof( RightsSection.Approve_User_Rights )] = new RestFunction { HttpMethod = "GET", ServiceString = "foo" },
[nameof( RightsSection.Get_Rights_List )] = new RestFunction { HttpMethod = "GET", ServiceString = "foo" },
},
};
protected IDictionary<string, RestFunction> GetSectionValues( [CallerMemberName] string propertyName = null )
{
return _config[propertyName];
}
public UserSection User => new UserSection( GetSectionValues() );
public RightsSection Rights => new RightsSection( GetSectionValues() );
}
public class RestFunction
{
public string HttpMethod { get; set; }
public string ServiceString { get; set; }
}
public abstract class ConfigSectionBase
{
protected readonly IDictionary<string, RestFunction> _values;
protected ConfigSectionBase( IDictionary<string, RestFunction> values )
{
_values = values;
}
protected RestFunction GetProperty( [CallerMemberName] string propertyName = null )
{
return _values[propertyName];
}
}
public class UserSection : ConfigSectionBase
{
public UserSection( IDictionary<string, RestFunction> values ) : base( values )
{
}
public RestFunction Add_User => GetProperty();
public RestFunction Get_Users_List => GetProperty();
}
public class RightsSection : ConfigSectionBase
{
public RightsSection( IDictionary<string, RestFunction> values ) : base( values )
{
}
public RestFunction Approve_User_Rights => GetProperty();
public RestFunction Get_Rights_List => GetProperty();
}