开发者

Parsley: Creating an ObjectDefinitionBuilder from an ObjectDefinition

开发者 https://www.devze.com 2023-03-28 12:36 出处:网络
I\'m working on a framework extension for Parsley, and need to effectively clone an ObjectDefinition during initialziation of the context, and mutate some of开发者_运维问答 it\'s properties.

I'm working on a framework extension for Parsley, and need to effectively clone an ObjectDefinition during initialziation of the context, and mutate some of开发者_运维问答 it's properties.

As ObjectDefinition's are inherently immutable, I'm finding I have to jump through hoops to create a new builder that safely ensures the properties of the existing ObjectDefinition have been copied across.

Is there a way to safely create a new ObjectDefinitionBuilder from an existing ObjectDefinition?

If not, what other alternatives exist for creating an ObjectDefinition clone?


I'm not sure if there's a more elegant solution, but I ended up doing this long-hand.

In my specific case, I want to change the class of the registered object, but keep the other properties. Here's the approach I ended up using:

private function cloneDefinition(objectDefinition:ObjectDefinition,replacementClass:Class):ObjectDefinition
{
        switch (true)
        {
            case objectDefinition is DefaultSingletonObjectDefinition:
                return new DefaultSingletonObjectDefinition(ClassInfo.forClass(replacementClass),objectDefinition.id,objectDefinition.registry);
            case objectDefinition is DefaultDynamicObjectDefinition:
                return new DefaultDynamicObjectDefinition(ClassInfo.forClass(replacementClass),objectDefinition.id,objectDefinition.registry);
        }
        throw new Error("Support for cloning object definitions of type " + getQualifiedClassName(objectDefinition) + " not yet supported");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消