Skip to content

Commit

Permalink
Adding model binding support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mallioch committed Jan 25, 2011
1 parent 869ca31 commit 0d0f092
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Mvc3StructureMapIoc/ModelBinderTypeMappingDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Collections.Generic;

namespace Mvc3StructureMapIoc
{
public class ModelBinderTypeMappingDictionary : Dictionary<Type, Type>
{
}
}
2 changes: 2 additions & 0 deletions Mvc3StructureMapIoc/Mvc3StructureMapIoc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ModelBinderTypeMappingDictionary.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StructureMapControllerActivator.cs" />
<Compile Include="StructureMapDependencyResolver.cs" />
<Compile Include="StructureMapModelBinderProvider.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
10 changes: 9 additions & 1 deletion Mvc3StructureMapIoc/StructureMapControllerActivator.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
using System;
using System.Web.Mvc;
using System.Web.Routing;
using StructureMap;

namespace Mvc3StructureMapIoc
{
public class StructureMapControllerActivator : IControllerActivator
{
public StructureMapControllerActivator(IContainer container)
{
_container = container;
}

private IContainer _container;

public IController Create(RequestContext requestContext, Type controllerType)
{
return DependencyResolver.Current.GetService(controllerType) as IController;
return _container.GetInstance(controllerType) as IController;
}
}
}
25 changes: 25 additions & 0 deletions Mvc3StructureMapIoc/StructureMapModelBinderProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Web.Mvc;
using StructureMap;

namespace Mvc3StructureMapIoc
{
public class StructureMapModelBinderProvider : IModelBinderProvider
{
public StructureMapModelBinderProvider(IContainer container)
{
_container = container;
}

private IContainer _container;

public IModelBinder GetBinder(Type modelType)
{
var typeMappings = _container.GetInstance<ModelBinderTypeMappingDictionary>();
if (typeMappings.ContainsKey(modelType))
return _container.GetInstance(typeMappings[modelType]) as IModelBinder;

return null;
}
}
}
9 changes: 9 additions & 0 deletions SampleWebsite/Code/AThing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace SampleWebsite.Code
{
public class AThing
{
public string SomeRandomValue { get; set; }
}
}
23 changes: 23 additions & 0 deletions SampleWebsite/Code/AThingModelBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Web.Mvc;

namespace SampleWebsite.Code
{
public class AThingModelBinder : DefaultModelBinder
{
public AThingModelBinder(IBar bar)
{
_bar = bar;
}

private IBar _bar;

protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
return new AThing()
{
SomeRandomValue = _bar.IPityTheFoo()
};
}
}
}
7 changes: 7 additions & 0 deletions SampleWebsite/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@ public ActionResult About()
{
return View();
}

public ActionResult ModelBindAThing(AThing thing)
{
ViewBag.Stuff = thing.SomeRandomValue;

return View();
}
}
}
8 changes: 5 additions & 3 deletions SampleWebsite/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

namespace SampleWebsite
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801

public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
Expand All @@ -39,9 +36,14 @@ protected void Application_Start()
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);

var modelBinderTypeMappingDictionary = new ModelBinderTypeMappingDictionary();
modelBinderTypeMappingDictionary.Add(typeof(AThing), typeof(AThingModelBinder));

IContainer container = new Container(x =>
{
x.For<IControllerActivator>().Use<StructureMapControllerActivator>();
x.For<IModelBinderProvider>().Use<StructureMapModelBinderProvider>();
x.For<ModelBinderTypeMappingDictionary>().Use(modelBinderTypeMappingDictionary);
x.For<IBar>().Use<Bar>();
});

Expand Down
5 changes: 5 additions & 0 deletions SampleWebsite/SampleWebsite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
<Reference Include="System.EnterpriseServices" />
</ItemGroup>
<ItemGroup>
<Compile Include="Code\AThing.cs" />
<Compile Include="Code\AThingModelBinder.cs" />
<Compile Include="Code\Bar.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Global.asax.cs">
Expand Down Expand Up @@ -99,6 +101,9 @@
<ItemGroup>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Home\ModelBindAThing.cshtml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
7 changes: 7 additions & 0 deletions SampleWebsite/Views/Home/ModelBindAThing.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@{
ViewBag.Title = "ModelBindAThing";
}

<h2>Model Bind a Thing</h2>

@ViewBag.Stuff
1 change: 1 addition & 0 deletions SampleWebsite/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Model Bind a Thing", "ModelBindAThing", "Home")</li>
</ul>

</div>
Expand Down

0 comments on commit 0d0f092

Please sign in to comment.