Per,
in broad strokes as you describe the scenario it does look like a job
for typed factory. Typed factories are generally tool you use in places
when you need to obtain a component in another component _after_ it was
created which obviosuly looks like the case here.
By default Windsor uses return type (and optionally method name) to look
up component to resolve. If you're passing the type as argument you will
have to override the default convention which is very simple.
The documentation
http://stw.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx?NoRedirect=1#Custom_codeITypedFactoryComponentSelectorcodes_15
has an example of using string argument to lookup components by name, in
your case code will be pretty much identical, with difference being you
override |GetComponentType| instead.
I'm not sure what you actually do in the
Populate the object properties using the .NET Reflection API
part.
You can adjust the typed factory method's signature and override
GetArguments in your selector to have Windsor do this part of the work
for you as well if feasable.
cheers,
Krzysztof the tunguebreaker.
On 08/11/2010 7:48 PM, Lundberg, Per wrote:
Hi there,
I’ve been reading this blog post
http://kozmic.pl/archive/2009/12/24/castle-typed-factory-facility-reborn.aspx
by Krzysztof now (man, your name has too many consontants in it J) and
have some questions, of a “design pattern” character.
What we’re after is something like this. Historically, we’ve had code
that looked something like this:
///<summary>
/// Maps search result text strings to corresponding entity objects.
///</summary>
public class SearchResultItemMapper
{
IObjectFactory _objectFactory;
/// <summary>
/// Creates a new instance of the class.
/// </summary>
/// <param name="objectFactory">The Object Factory instance we are
using.</param>
public SearchResultItemMapper(IObjectFactory objectFactory)
{
_objectFactory = objectFactory;
}
/// <summary>
/// Maps an entity from a header string.
/// </summary>
/// <param name="typeName">The name of the type to which we should map this
string.</param>
/// <param name="headerString">The header string.</param>
/// <returns>The entity object.</returns>
public object MapEntityFromHeaderString(Type type,string
headerString)
{
// Create the entity object which will be populated with data.
object entityObject = _objectFactory.CreateObject(type);
// [snip] – Populate the object properties using the .NET Reflection API.
}
What this code did was basically mapping up a given “header string”
(well, serialized object data is a better name for it) to an entity
object of ours. We used a custom class called the ObjectFactory for
this. This was before we started using the Castle Windsor container;
we were using Unity Container for the IoC stuff and combined it with
Dynamic Proxy to be able to get the proxying done.
Now, we’re in a more “pure” Castle Windsor mode, and as you are
suggesting, using the container from our components indicate that we
are doing something wrong… J
What we do need to be able to do in this code is to create objects, of
any type (well, deriving from a certain base class of ours – all such
entities are registered in the container, using mass-registration). We
then use reflection to populate these objects with data. This allows
the application to be pretty dynamic and not too hard-wired (well
yeah, you get the general picture…).
So what do we do? Is this a scenario for the Typed Factory facility?
As you see, we’re not getting called as a generic method, with a type
argument, but rather with a System.Type argument. This is because we
are deducing the type dynamically, depending on the data from an XML
configuration file or something like that.
Please, any advice will be welcome. This is the first application
we’re upgrading to this new version of our application framework, so
the design pattern should be set up properly for future projects as
well. Thanks in advance!
Best regards,
Per
--
You received this message because you are subscribed to the Google
Groups "Castle Project Users" group.
To post to this group, send email to
[email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.
--
You received this message because you are subscribed to the Google Groups "Castle
Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.