View Full Version : Get object's id.
11-07-2006, 04:15 PM
just wondering if its possible to get back the id from which an object was created.
so for example if i create
Customer customer = Factory.Create("MyCustomer");
<object id="MyCustomer" type='MyAssembly.Customer, MyAssembly"/>
is it possible to get that "MyCustomer" back ?
11-07-2006, 08:47 PM
sure, take a look to this part of the documentation :
Knowing who you are (http://www.springframework.net/doc-latest/reference/html/objects.html#objects-factory-aware)
... and specially the IObjectNameAware interface :
The Spring.Objects.Factory.IObjectNameAware interface gives you the ability to let the container set the name of the object definition on the object instance itself. In those cases where your object needs to know what its name is, implement this interface.
string ObjectName: the property that will be set to let the object know what its name is.
Your Customer class just needs to implement this interface, but you will lost the non-invasive aspect of Spring.NET as your code becomes dependent from Spring.NET.
Hope this helps,
11-29-2006, 06:35 AM
just wondering the effects of IObjectNameAware as it seems that embedded child objects do not receive their original name, but "(child object)" string as their ObjectName. Even ContextRegisty.GetContext().GetObject("Child") won't return the child item.
<object id="Parent" type='MyAssembly.MyObject, MyAssembly">
<object id="Child" type='MyAssembly.MyObject, MyAssembly"/>
If Child is moved to root level and parents Items is populated with refs to children, will the children have names.
If this a known feature or not, I don't know. But with quick search to docs I didn't find the answer.
11-29-2006, 01:08 PM
The ID attribute is optional and has no effect whatsoever on child objects. That implies that they are not register in object factory, which is why they always return "(child object)" as a name.
If you make child object a top-level object and use reference instead, you will be able to get its name, as expected.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.