View Full Version : New Feature? cxt.GetObject<T>()
dsellers
02-26-2006, 02:17 AM
What does everyone think about a function like the following on IApplicationContext?
OLD
IApplicationContext cxt = ContextRegistry.GetContext();
IRepository r = cxt.GetObject("repository") as IRepository;
NEW
IApplicationContext cxt = ContextRegistry.GetContext();
IRepository r = cxt.GetObject<IRepository>();
Bruno Baia
03-22-2006, 01:01 PM
Hi Dru,
nice idea !
I'm investigating on generics support for Spring.NET.
I implemented with a few lines this new method in my sandbox :
Instead of this :
MovieLister lister = (MovieLister)ctx.GetObject("MyMovieLister");
I got this :
MovieLister lister = ctx.Get<MovieLister>("MyMovieLister");
Internally it will call the method "GetObject(string name, Type typeRequired)" that check the requiredType.
#if NET_2_0
public T Get<T>(string name)
{
return (T) GetObject(name, typeof(T), ObjectUtils.EmptyObjects);
}
#endif
If the object "MyMovieLister" doen't match the type "MovieLister", we'll got an ObjectNotOfRequiredTypeException (Spring.NET framework exception) instead of an InvalidCastException (.NET framework exception).
A nice type-safe approach, one the benefits using generics.
Now the question is :
GetObject<T> or Get<T> ??
PS : The same approach can be applied to the GetRessourceObject method.
-Bruno
Erich Eichinger
03-22-2006, 01:10 PM
Now the question is :
GetObject<T> or Get<T> ??
I don't think of "GetObject" as "give me something of type 'object'". Rather it means "give me an instance". But I admit, that "GetObject" might be somewhat confusing.
Thus I would prefer either Get<T> or maybe better GetInstance<T>.
br,
oaky
Aleks Seovic
03-23-2006, 04:07 AM
I actually tend to disagree that this is a good idea...
There is nothing "generic" about aplication context/object factory. It contains objects of any type and it returns objects of any type. Using generic method to return strongly typed instance from the by design weakly typed container is to me missleading and wrong.
Get<T>/GetObject<T> is not any "safer" than (T) GetObject -- it just moves cast into generic type specifier, but it's still a cast. If you specify wrong type, you will get an exception, just like you would get it with a cast, but in this case you will get a non-standard exception which to me is worse than InvalidCastException that everyone is familiar with.
Overall, I see generics as a shiny new toy that will be grossly missused in years to come, just like the attributes. In my opinion, example in this thread is one such missuse. I would be very cautious before introducing generic API members into Spring codebase. Sure, we need to allow users to inject generic types just like they would inject non-generic ones, but that doesn't require us to make Spring API generic.
Later,
Aleks
Bruno Baia
03-23-2006, 09:48 AM
Yeah, u're right.
There is nothing generic in there, neither type safe even if it looks like from outside :D
It could replace the "GeObject(string name, Type requiredType)" method, it looks nicer, but nothing generic, just a "easy way"/misuse.
-Bruno
Aleks Seovic
03-23-2006, 11:05 AM
I'm not quite sure why we even have GetObject(string name, Type requiredType). I don't think similar method exists in Java, and I seem to remember voting against it when someone proposed it.
The reason I don't like it is similar to the reason I don't like a generic version -- it doesn't buy you anything and if type doesn't match actual type you will still get a custom version of cast exception. Name/id is all you need to retrieve an object from the context, so specifying type is unnecessary. It's not like you can have two objects with the same name and different types, in which case type would allow you to narrow selection down.
Another reason I don't like it is because it implies factory usage -- it is only useful if you plan to invoke it from your code, which is not how you should typically write Spring applications. Ideally, there should be zero calls to any version of GetObject in the end users' code -- it should only be called internally during dependecy resolution and injection.
- Aleks
Erich Eichinger
03-23-2006, 11:43 AM
Although I don't know about NET generics yet, I've some fairly good knowledge of C++ templates. I never found correct usage for templates going beyond typed containers and algorithms like STL has (Ok, I've implemented some patterns like Double-Dispatch using templates - but it got too complex and has been horrible to debug, so nobody wanted to use it ;-)). Does anybody have more ideas? Please let me know!
br,
oaky
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.