Skip to content

Instantly share code, notes, and snippets.

@Fodsuk
Created May 30, 2012 13:52
Show Gist options
  • Save Fodsuk/2836453 to your computer and use it in GitHub Desktop.
Save Fodsuk/2836453 to your computer and use it in GitHub Desktop.
WorkflowController
public abstract class WorkflowController : BaseController
{
private string _workflowReference;
private readonly string _modelNamespace;
private readonly string _modelAssembly;
private IWorkflowKeyStore _workflowKeyStore;
private IWorkflowInvokerFactory _invokerFactory;
protected WorkflowController(string workflowReference, string modelNamespace, string modelAssembly, IWorkflowKeyStore workflowKeyStore, IWorkflowInvokerFactory workflowInvokerFactory)
{
_workflowReference = workflowReference;
_modelNamespace = modelNamespace;
_modelAssembly = modelAssembly;
_workflowKeyStore = workflowKeyStore;
_invokerFactory = workflowInvokerFactory;
}
public string WorkflowReference
{
get { return _workflowReference; }
}
public IWorkflowInvokerFactory InvokerFactory
{
get { return _invokerFactory; }
}
public IWorkflowKeyStore WorkflowKeyStore
{
get { return _workflowKeyStore; }
}
private IWorkflowInvoker _invoker;
public IWorkflowInvoker Invoker
{
get
{
if (_invoker == null)
_invoker = InvokerFactory.CreateWorkflowInvoker();
return _invoker;
}
}
private Guid? _currentWorkflowKey;
public Guid? CurrentWorkflowKey
{
get {
if (_currentWorkflowKey == null)
_currentWorkflowKey = WorkflowKeyStore.GetWorkflowKey(WorkflowReference);
return _currentWorkflowKey;
}
}
public void SetWorkflowKey(Guid workflowKey)
{
WorkflowKeyStore.StoreWorkflowKey(WorkflowReference, workflowKey);
}
private object FindModel(string name)
{
var modelFullNamespace = string.Format("{0}.{1},{2}", _modelNamespace, name, _modelAssembly);
var modelType = Type.GetType(modelFullNamespace);
if (modelType != null)
return Activator.CreateInstance(modelType);
return null;
}
public ViewResult RedirectToStep(string step, dynamic modelData)
{
var model = FindModel(step);
if (model == null)
return View(step);
ValueBinder.ConvertPropertyValues(model, modelData);
return View(step, model);
}
public bool ValidateModel(string modelName, FormCollection collection)
{
if (!String.IsNullOrEmpty(modelName))
{
dynamic viewmodel = FindModel(modelName);
TryUpdateModel(viewmodel, collection);
return ModelState.IsValid;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment