Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've just started working with Workflow (WF4) and have been playing with an idea of using it in MVC3.0 controller actions to see if it improves the maintainability of complex actions; also potentially giving the ability to make multiple DB calls in parallel to populate the output model. Along with this I've been looking at Async controller actions but I'm not sure I've really got my head around those. I know you can load workflow into a WorkflowApplication and call BeginRun(...) to get that to run asynchronously but I'm thinking that's the wrong approach; from looking online I've come up with the following implementation which does run as expected but I'm wondering if there's anything wrong with the code below or it will cause other issues that I've not thought about.

    [HttpPost]
    public void ConfigureAsync(ConfigureExportInputModel inputModel)
    {
        if (inputModel == null)
        {
            throw new ArgumentNullException("inputModel");
        }

        var arguments = new Dictionary<string, object> { { "ExportType", inputModel.ExportType } };
        var wf = new ErrorHandledExportWorkflow();

        AsyncManager.OutstandingOperations.Increment();
        ThreadPool.QueueUserWorkItem(o =>
                {
                    AsyncManager.Parameters["result"] = WorkflowInvoker.Invoke(wf, arguments)["MvcOutput"] as IDefineMvcOutput;
                    AsyncManager.OutstandingOperations.Decrement();
                });
    }

    public ActionResult ConfigureCompleted(IDefineMvcOutput result)
    {
        return this.ActionResultFactory.Create(this, result);
    }
share|improve this question
2  
Just FYI - C# 4 introduced the Code Contracts library so you can do Contract.Requires(inputModel != null) which is more semantic and has interesting AOP possibilities. For example you can have all these checks fire in development and testing and disabled to boost performance in production. There is also a VS extension that will attempt to do some basic analysis on the code contracts while your coding and point out gaps in logic. –  George Mauer Feb 7 '12 at 16:28
    
Thanks for the comment, I've seen code contracts and taken a quick look but not used them in any real code yet - they do sound really nice. I keep meaning to see if there's a way to unit test that a method implements a particular contract. I think you can put them on as attributes so it would be possible to do it that way; reflect over the method to confirm it has the expected contract(s) in place. –  Paul Hadfield Feb 7 '12 at 18:54
    
I think code contracts are complementary to unit tests, they don't need to and shouldn't be included by them. From what I understand they are inspired by projects like Spec# so they aim to eventually be an extension of the type system. As far as using them goes though, it takes like 5 seconds to learn, I very much recommend it. –  George Mauer Feb 7 '12 at 19:24
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.