Working up AutoFactory

209 views
Skip to first unread message

Rookian

unread,
May 14, 2012, 9:10:03 AM5/14/12
to structuremap-users
Hello!

I am trying to working up AutoFactory.

AutoFactory is the counterpart to CastleWindsor's Typed Factory
Facility.

With AutoFactory you can achieve a design where you don't need to pass
an IContainer into a consumers constructor. This would be the Service
Locator AntiPattern that is not good because of unit tests would need
the DI container and you don't see the real depedencies, but only the
IContainer. Furthermore you don't need to drag StructureMap in more
than one project.

At the moment AutoFactory is rudimentary implemented, so a lot of
stuff does not work.

To working up AutoFactory I need to extend the IContext with the
following methods:
- ExplicitArgsExpression With(Type pluginType, object arg)
- IEnumerable<object> GetAllInstances(Type pluginType)

GetAllInstances is easy to implement, because it is already there.
"With" is more complex, because I would need IContainer or something
like that.

Can somebody help me to implement the With method for IContext?

After this I would adjust the AutoFactory.FactoryInterceptor.

Thanks for your help.

Tim Kellogg

unread,
May 14, 2012, 10:14:35 AM5/14/12
to structure...@googlegroups.com
Rookian,

I don't have a full answer for you, but I do have a couple thoughts. 

First, I assume you already thought about taking an `IEnumerable<TPluginType>` through the constructor to get a list of registered instances. For instance, you could have:

public class UnicornFactory {
    public UnicornFactory(IEnumerable<IUnicorn> knownUnicorns) { _unicorns = knownUnicorns }

    public IEnumerable<IUnicorn> GetAll() { return _unicorns; }
    public IUnicorn GetUnicornNamed(string name) {
        return _unicorns.FirstOrDefault(uni => uni.Name == name);
    }

This would be my first simplest-thing-possible implementation. This works as long as all UnicornFactory is registered as a longer lifespan (or equal to) all IUnicorn implementations. The reason is if UnicornFactory is registered as Singleton, all IUnicorns will be effectively Singleton also because their references are being held by UnicornFactory (it's misconfigured). This should work in just about every case, but if you need UnicornFactory to be registered as Singleton, you'll have to have an IContainer at some point.

Aside from those thoughts, I don't quite understand your question. I assume you found the StructureMap.AutoFactory assembly and are looking at the .CreateFactory() extension method.

--Tim




--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To post to this group, send email to structure...@googlegroups.com.
To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/structuremap-users?hl=en.


Rookian

unread,
Jun 2, 2012, 3:22:40 AM6/2/12
to structuremap-users
Hi Tim,

yes it is about the StructureMap AutoFactory CreateFactory() extension
method.
Your described way for lists is possible, but I would like to have the
possibilities like in CastleWindsor.
There the factory method signature is analyzed and with those meta
information the type will be resolved.
The list feature is easy to implement. But I don't know how I can add
the With functionality for methods like "IPresenter<IMainView>
Create(ViewModel model)"

I hope someone could help me to implement this feature into the
IContext , then I would adjust the AutoFactory.

Tim Kellogg

unread,
Jun 2, 2012, 8:18:40 AM6/2/12
to structure...@googlegroups.com
Hi Rookian,

I actually just ran into a situation where the feature you were requesting would be extremely convenient. So I made a pull request :)


If you don't mind, could you follow the link and add your feedback. Normally I would just push directly into the structuremap repo, but I wanted to start some public discussion to make sure this really is the right direction to go in. You can get the changes and compile & use for yourself by cloning git://github.com/tkellogg/structuremap.git and checking out the `autofactory-parameters` branch (the new github for windows makes this really easy).

Much thanks,
--Tim
Reply all
Reply to author
Forward
0 new messages