Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Created September 23, 2014 23:10
Show Gist options
  • Save aaronpowell/7936cdb7936b393fcb3a to your computer and use it in GitHub Desktop.
Save aaronpowell/7936cdb7936b393fcb3a to your computer and use it in GitHub Desktop.
F# workshop ioc
namespace FSharpWeb.Controllers
open System.Web.Mvc
open Services
type CustomersController(service: CustomerService) =
inherit Controller()
member x.Index () =
x.View(service.GetCustomers ())
member x.Post (ids: int seq) =
service.UpgradeCustomers(ids);
x.RedirectToAction("Index")
namespace FSharpWeb
open System
open System.Net.Http
open System.Web
open System.Web.Http
open System.Web.Mvc
open System.Web.Routing
open System.Web.Optimization
open Autofac
open Autofac.Integration.Mvc
open Modules
type BundleConfig() =
static member RegisterBundles (bundles:BundleCollection) =
bundles.Add(ScriptBundle("~/bundles/jquery").Include([|"~/Scripts/jquery-{version}.js"|]))
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(ScriptBundle("~/bundles/modernizr").Include([|"~/Scripts/modernizr-*"|]))
bundles.Add(ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"))
bundles.Add(StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"))
/// Route for ASP.NET MVC applications
type Route = {
controller : string
action : string
id : UrlParameter }
type HttpRoute = {
controller : string
id : RouteParameter }
type Global() =
inherit System.Web.HttpApplication()
static member RegisterWebApi(config: HttpConfiguration) =
// Configure routing
config.MapHttpAttributeRoutes()
config.Routes.MapHttpRoute(
"DefaultApi", // Route name
"api/{controller}/{id}", // URL with parameters
{ controller = "{controller}"; id = RouteParameter.Optional } // Parameter defaults
) |> ignore
// Configure serialization
config.Formatters.XmlFormatter.UseXmlSerializer <- true
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver <- Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
// Additional Web API settings
static member RegisterFilters(filters: GlobalFilterCollection) =
filters.Add(new HandleErrorAttribute())
static member RegisterRoutes(routes:RouteCollection) =
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
{ controller = "Home"; action = "Index"; id = UrlParameter.Optional } // Parameter defaults
) |> ignore
member x.Application_Start() =
AreaRegistration.RegisterAllAreas()
GlobalConfiguration.Configure(Action<_> Global.RegisterWebApi)
Global.RegisterFilters(GlobalFilters.Filters)
Global.RegisterRoutes(RouteTable.Routes)
BundleConfig.RegisterBundles BundleTable.Bundles
let builder = ContainerBuilder()
builder.RegisterModule(ServiceRegistrationModule()) |> ignore
builder.RegisterControllers(Reflection.Assembly.GetExecutingAssembly()) |> ignore
let container = builder.Build()
DependencyResolver.SetResolver(AutofacDependencyResolver(container)) |> ignore
module Modules
open Autofac
open Services
type ServiceRegistrationModule() =
inherit Module()
override this.Load (builder: ContainerBuilder) =
builder.RegisterType(typedefof<CustomerService>) |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment