Skip to content

Commit

Permalink
Added IDependencyResolver, static class DependencyResolver for settin…
Browse files Browse the repository at this point in the history
…g current resolver, a default implementation (DefaultDependencyResolver) and implemented everything into the framework for controllers and filters.
  • Loading branch information
Andreas committed Jan 11, 2011
1 parent f8e0792 commit c5f246c
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/ufront/web/mvc/Controller.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Controller extends ControllerBase, implements IActionFilter, implements IA
private function getInvoker()
{
if (_invoker == null)
_invoker = new ControllerActionInvoker(ModelBinders.binders, ControllerBuilder.current);
_invoker = new ControllerActionInvoker(ModelBinders.binders, ControllerBuilder.current, DependencyResolver.current);

return _invoker;
}
Expand Down
9 changes: 6 additions & 3 deletions src/ufront/web/mvc/ControllerActionInvoker.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ import thx.collections.Set;
using thx.collections.UIterable;

class ControllerActionInvoker implements IActionInvoker
{
public function new(binders : ModelBinderDictionary, controllerBuilder : ControllerBuilder)
{

public function new(binders : ModelBinderDictionary, controllerBuilder : ControllerBuilder, dependencyResolver : IDependencyResolver)
{
this.binders = binders;
this.controllerBuilder = controllerBuilder;
this.dependencyResolver = dependencyResolver;
}

public var controllerBuilder : ControllerBuilder;
public var binders : ModelBinderDictionary;
public var valueProvider : IValueProvider;
public var dependencyResolver : IDependencyResolver;

// public var error(default, null) : Error;

Expand Down Expand Up @@ -257,7 +260,7 @@ class ControllerActionInvoker implements IActionInvoker
var c = self.getAttributeClass(key);
if (c == null) return null;

var instance = Type.createInstance(c, []);
var instance = self.dependencyResolver.getService(c);
var args = hash.get(key);

//trace('Creating ' + Type.getClassName(c));
Expand Down
2 changes: 1 addition & 1 deletion src/ufront/web/mvc/ControllerBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ControllerBuilder
packages = new List();
attributes = new List();

setControllerFactory(new DefaultControllerFactory(this));
setControllerFactory(new DefaultControllerFactory(this, new DefaultDependencyResolver()));
}

public function getControllerFactory() : IControllerFactory
Expand Down
11 changes: 8 additions & 3 deletions src/ufront/web/mvc/DefaultControllerFactory.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ import thx.error.NullArgument;

class DefaultControllerFactory implements IControllerFactory {
var _controllerBuilder : ControllerBuilder;
public function new(controllerBuilder : ControllerBuilder)
var _dependencyResolver : IDependencyResolver;

// TODO: IControllerActivator as ControllerBuilder
public function new(controllerBuilder : ControllerBuilder, dependencyResolver : IDependencyResolver)
{
NullArgument.throwIfNull(controllerBuilder, "controllerBuilder");

_controllerBuilder = controllerBuilder;
_dependencyResolver = dependencyResolver;
}

public function createController(requestContext : RequestContext, controllerName : String) : IController
{
var cls = UString.ucfirst(controllerName) + "Controller";
for (pack in _controllerBuilder.packages)
{
var fullname = pack + "." + cls;
var fullname = pack + "." + cls;
var type = Type.resolveClass(fullname);

if (type != null)
{
var controller = Type.createInstance(type, []); // TODO: Dependency injection support
var controller = _dependencyResolver.getService(type);
if (Std.is(controller, IController)) return controller;
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/ufront/web/mvc/DefaultDependencyResolver.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ufront.web.mvc;
import thx.error.Error;

/**
* ...
* @author Andreas Soderlund
*/

class DefaultDependencyResolver implements IDependencyResolver
{
public function new();

public function getService<T>(type : Class<T>) : T
{
try
{
return Type.createInstance(type, []);
}
catch(e : Dynamic)
{
return null;
}
}

public function getServices<T>(serviceType : Class<T>) : Iterable<T>
{
return new List<T>();
}
}
18 changes: 18 additions & 0 deletions src/ufront/web/mvc/DependencyResolver.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ufront.web.mvc;
import thx.error.Error;

/**
* ...
* @author Andreas Soderlund
*/

class DependencyResolver
{
public static var current : IDependencyResolver = new DefaultDependencyResolver();

public static function setResolver(resolver : IDependencyResolver)
{
DependencyResolver.current = resolver;
return resolver;
}
}
12 changes: 12 additions & 0 deletions src/ufront/web/mvc/IDependencyResolver.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ufront.web.mvc;

/**
* ...
* @author Andreas Soderlund
*/

interface IDependencyResolver
{
function getService<T>(serviceType : Class<T>) : T;
function getServices<T>(serviceType : Class<T>) : Iterable<T>;
}
2 changes: 1 addition & 1 deletion test/ufront/web/mvc/TestAll.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TestAll
ControllerBuilder.current.attributes.remove("ufront.web.mvc.attributes");
ControllerBuilder.current.attributes.add("ufront.web.mvc.attributes");

controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current);
controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current, DependencyResolver.current);

return new ControllerContext(controller, TestAll.getRequestContext(action));
}
Expand Down
2 changes: 1 addition & 1 deletion test/ufront/web/mvc/TestAuthorizeAttribute.hx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class TestAuthorizeAttribute
controller = new TestController();

var valueProvider = new RouteDataValueProvider(new ControllerContext(controller, context));
controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current);
controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current, DependencyResolver.current);
}

function execute()
Expand Down
6 changes: 3 additions & 3 deletions test/ufront/web/mvc/TestControllerBindings.hx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class TestControllerBindings
controller = new TestController();

var valueProvider = new RouteDataValueProvider(new ControllerContext(controller, context));
controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current);
controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current, DependencyResolver.current);
}

function execute()
Expand Down Expand Up @@ -199,7 +199,7 @@ class TestControllerBindings
var binders = new ModelBinderDictionary();
binders.add(Date, new SpecialDateBinder());

controller.invoker = new ControllerActionInvoker(binders, ControllerBuilder.current);
controller.invoker = new ControllerActionInvoker(binders, ControllerBuilder.current, DependencyResolver.current);

context.routeData.data.set("action", "bindDate");
context.routeData.data.set("date", "Millenium");
Expand All @@ -214,7 +214,7 @@ class TestControllerBindings
var binders = new ModelBinderDictionary();
binders.add(Date, new SpecialDateBinder());

controller.invoker = new ControllerActionInvoker(binders, ControllerBuilder.current);
controller.invoker = new ControllerActionInvoker(binders, ControllerBuilder.current, DependencyResolver.current);

context.routeData.data.set("action", "bindDate");
context.routeData.data.set("date", "Some other value");
Expand Down
2 changes: 1 addition & 1 deletion test/ufront/web/mvc/TestControllerFilters.hx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class TestControllerFilters
this.controller = controller == null ? new TestController() : controller;

var valueProvider = new RouteDataValueProvider(new ControllerContext(this.controller, context));
this.controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current);
this.controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current, DependencyResolver.current);
}

function execute()
Expand Down
12 changes: 6 additions & 6 deletions test/ufront/web/mvc/TestDefaultControllerFactory.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TestDefaultControllerFactory
var builder = new ControllerBuilder();
builder.packages.add("ufront.web.mvc");
builder.packages.add("ufront.web.mvc.test");
var factory = new DefaultControllerFactory(builder);
var factory = new DefaultControllerFactory(builder, new DefaultDependencyResolver());

// Note that "Controller" is auto-appended.
var controller = factory.createController(TestAll.getRequestContext(), "Mock");
Expand All @@ -50,7 +50,7 @@ class TestDefaultControllerFactory
var builder = new ControllerBuilder();
builder.packages.add("ufront.web.mvc.test");
builder.packages.add("ufront.web.mvc");
var factory = new DefaultControllerFactory(builder);
var factory = new DefaultControllerFactory(builder, new DefaultDependencyResolver());

var controller = factory.createController(TestAll.getRequestContext(), "Mock");
Assert.notNull(controller);
Expand All @@ -61,7 +61,7 @@ class TestDefaultControllerFactory
{
var builder = new ControllerBuilder();
builder.packages.add("ufront.web.mvc");
var factory = new DefaultControllerFactory(builder);
var factory = new DefaultControllerFactory(builder, new DefaultDependencyResolver());

var controller = cast(factory.createController(TestAll.getRequestContext(), "Mock"), ufront.web.mvc.MockController);
Assert.isFalse(controller.disposed);
Expand All @@ -76,7 +76,7 @@ class TestDefaultControllerFactory
{
var builder = new ControllerBuilder();
builder.packages.add("ufront.web.mvc.test");
var factory = new DefaultControllerFactory(builder);
var factory = new DefaultControllerFactory(builder, new DefaultDependencyResolver());

var controller = cast(factory.createController(TestAll.getRequestContext(), "Mock"), ufront.web.mvc.test.MockController);
Assert.isFalse(controller.disposed);
Expand All @@ -88,7 +88,7 @@ class TestDefaultControllerFactory
{
var builder = new ControllerBuilder();
builder.packages.add("ufront.web.mvc");
var factory = new DefaultControllerFactory(builder);
var factory = new DefaultControllerFactory(builder, new DefaultDependencyResolver());

Assert.raises(function() factory.createController(TestAll.getRequestContext(), "TestDefaultControllerFactory"), Error);
}
Expand All @@ -97,7 +97,7 @@ class TestDefaultControllerFactory
{
var builder = new ControllerBuilder();
builder.packages.add("ufront.web.mvc");
var factory = new DefaultControllerFactory(builder);
var factory = new DefaultControllerFactory(builder, new DefaultDependencyResolver());

Assert.raises(function() factory.createController(TestAll.getRequestContext(), "Fake"), Error);
}
Expand Down
2 changes: 1 addition & 1 deletion test/ufront/web/mvc/TestIActionFilter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class TestIActionFilter
controller = new TestController();

var valueProvider = new RouteDataValueProvider(new ControllerContext(controller, context));
controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current);
controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current, DependencyResolver.current);
}

function execute()
Expand Down
2 changes: 1 addition & 1 deletion test/ufront/web/mvc/TestIAuthorizationFilter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class TestIAuthorizationFilter
controller = new TestController();

var valueProvider = new RouteDataValueProvider(new ControllerContext(controller, context));
controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current);
controller.invoker = new ControllerActionInvoker(new ModelBinderDictionary(), ControllerBuilder.current, DependencyResolver.current);
}

function execute()
Expand Down

0 comments on commit c5f246c

Please sign in to comment.