View Full Version : Using object ID in domain objects
Ted Husted
04-08-2005, 01:04 PM
(I thought I posted this yesterday, but I don't see it. )
Is there a way to obtain the object ID set in the spring-objects configuration and inject it into the domain object.
Within my applications, I often have a need for an entity identifier, and end up replicating the object Id as an injected property, which offends my sense of DRY :?
Case in point:
<object id="select_all" type="PhoneBook.Core.Commands.BaseList">
<property name="ID"><value>select_all</value></property>
</object >
All this object does is use the ID to run a query statement (via iBATIS).
I could just as easily use the object.id value instead of injecting another.
-Ted.
Rick Evans
04-08-2005, 01:32 PM
Hi Ted
Well, if you don't mind coupling your domain objects to the Spring.NET API, you can always have your domain objects implement the
Spring.Objects.Factory.IObjectNameAware (http://www.springframework.net/doc/api/html/Spring.Objects.Factory.IObjectNameAware.html) interface (if you're going down this route, you'll no doubt want to stick the implementation of said interface on a base domain class -- if you have such a class).
Mmm, not the nicest solution is it? Your solution works as well; I guess it depends on which of your senses is least offended... DRY or no dependency on Spring.NET.
I take it from your post that you're using iBatis.NET, which is great... there have been a number of support forum and mailing list posts about NHibernate support for Spring.NET (along the lines of the API presented in the Spring.Java framework). No-one has picked this (NHibernate support) up yet, but I'm using iBatis.NET in a project that I'm working on, and have functionality that is equivalent with the iBatis API offered by the Spring.Java framework.
I'm a bit disconnected at the moment (just moved house -- again), but as soon as BT wires me up, I'll commit the iBatis.NET support into the Spring.NET sandbox. Hopefully that won't be too long away. :?
Have a cool weekend, ciao
Rick
Ted Husted
04-08-2005, 02:51 PM
Well, that actually gives me an idea.
We already have an IController member, and a Controller implementation for Spring. (The members know that there is aN iController, but they don't know that the Controller instance uses Spring.) Since the Controller already knows the spring id, we could just have the Controller set the object ID as it returns the instance.
Thanks for replying, Colin.
-T.
Rick Evans
04-08-2005, 03:52 PM
No worries Ramone
I guess one could also get real dirty, and use a special IFactoryObject to churn out your Command (?) objects for you. If your Command (?) class exposes an Id property, you could have your CommandFactoryObject implement the IObjectNameAware interface... then, your IFactoryObject would be instantiated by the container, have its ObjectName property set as part of the IObjectNameAware interface implementation, and then it goes and sets the Id property of the Command (?) object that it is creating indirectly via the GetObject() method implementation.
To wit...
public sealed class TedHustedCommandFactoryObject : IFactoryObject, IObjectNameAware
{
public bool IsSingleton
{
get { return false; }
}
public Type ObjectType
{
get { return typeof (TedHustedCommand); }
}
public object GetObject()
{
_command.Id = _id;
return _command;
}
public string ObjectName
{
set { _id = value; }
}
public TedHustedCommand Command
{
get { return _command; }
set { _command = value; }
}
private string _id;
private TedHustedCommand _command;
}
and the XML...
<objects>
<object id="select_all" type="Foo.TedHustedCommandFactoryObject, Foo" singleton="false">
<property name="command">
<object type="Foo.PhoneBook.Core.Commands.BaseList, Foo"/>
</property>
</object>
</objects>
This way none of your Controller classes have to change. Messy though. Please accept my humblest apologies, 'cos I'm sure Mark or Griffin will castigate me for suggesting such a travesty.
Ciao
Rick
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.