我有一个Dictionary
,它将枚举值作为键并返回一个对象作为值。对象构造函数涉及调用需要很长时间并消耗大量内存的方法。现在,字典中的每个可能的对象都会被创建,即使只需要一个对象。有没有办法只创建键指定的对象?Make对象的字典只创建指定键的对象
private DataPreparer SetUpOuDataPreparer()
{
Scope = _activeDirectoryScope.Context;
var activeDirectorySearcher = new ActiveDirectorySearcher(
_activeDirectoryScope);
var ouDataPreparers = new Dictionary<QueryType, DataPreparer>
{
[OuComputers] = new DataPreparer
{
Data = activeDirectorySearcher.GetOuComputerPrincipals(),
Attributes = DefaultComputerAttributes
},
[OuGroups] = new DataPreparer
{
Data = activeDirectorySearcher.GetOuGroupPrincipals(
CancellationToken),
Attributes = DefaultGroupAttributes
},
[OuUsers] = new DataPreparer
{
Data = activeDirectorySearcher.GetOuUserPrincipals(),
Attributes = DefaultUserAttributes
},
[OuUsersDirectReports] = new DataPreparer
{
Data = activeDirectorySearcher.GetOuUsersDirectReports(
CancellationToken),
Attributes = DefaultUserDirectReportsAttributes
},
[OuUsersGroups] = new DataPreparer
{
Data = activeDirectorySearcher.GetOuUsersGroups(
CancellationToken),
Attributes = DefaultUserGroupsAttributes
}
};
return ouDataPreparers[QueryType];
}
也就是说方法创建所有五个DataPreparer
S,其中的每一个具有在ActiveDirectorySearcher
昂贵的方法调用。我想以某种方式只创建由QueryType
密钥指定的DataPreparer
,最好不使用switch
或if/else
。为了更好的格式化/风格,我将其从那些更改为Dictionary
。
为什么要使用字典呢?为什么不只是一个带有switch语句的工厂方法方法? –
@AntP我在问题中提到我想避免切换,如果出于文体原因。 –
我不确定他们是什么“风格的原因”,但创建了一个你只需要一个对象的集合,因为这个工作的正确工具看起来不太漂亮,这对我来说似乎有点落后。 –