Is there some helper to Release with TypedFactory in C# using construct?

17 views
Skip to first unread message

Michael S.

unread,
Nov 29, 2012, 2:51:26 PM11/29/12
to castle-pro...@googlegroups.com
Hello,
I've been thinking of some helper component that could save little keystrokes. And instead of writing this:

    IMyFactory Factory { get; set; }
   
    void foo()
    {
        ISilverOblogon oblogon = Factory.CreateSilverOblogon();

        try
        {
           // use oblogon
        }
        finally
        {
           Factory.Release(oblogon);
        }
    }

One could do

    FactoryScope<IMyFactory, ISilverOblogon> OblogonFactory { get; set; }
    // FactoryScope has Factory inside injected by container

    void bar()
    {
        using (var wrapper = OblogonFactory.AcquireObject())
        // FactoryScope finds and calls CreateSilverOblogon method
        {
            // use wrapper.Object which is ISilverOblogon
        } // wrapper.Dispose calls Factory.Release method
    }

Is there anything like this around? Does it make any sense at all?

Krzysztof Kozmic

unread,
Nov 29, 2012, 4:30:26 PM11/29/12
to castle-pro...@googlegroups.com
Nothing built in, since we don't want to force you to have to reference Windsor's assembly in your application code, but it's a trivial extension method and wrapper type, 10 LOC altogether to get this working yourself

-- 
Krzysztof Kozmic

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/mGweFc_e_JQJ.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

Michael S.

unread,
Nov 30, 2012, 10:14:41 PM11/30/12
to castle-pro...@googlegroups.com
Thanks for the hint! I would do it as extension method plus empty common interface plus wrapper since it's shorter and more straightforward than generic type like FactoryScope<> that needs to be registerd itself.
To unsubscribe from this group, send email to castle-project-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages