View Full Version : Get object's id.
ash9696
11-07-2006, 04:15 PM
hi All,
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 ?
Thanks
-ashish
Bruno Baia
11-07-2006, 08:47 PM
Hi,
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,
Bruno
rantsi
11-29-2006, 06:35 AM
Hi,
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">
<property name="Items">
<list>
<object id="Child" type='MyAssembly.MyObject, MyAssembly"/>
</list>
</property>
</object>
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.
rantsi
Aleks Seovic
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.
- Aleks
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.