Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 4.12 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Custom scope in Windsor for shared REST resource in MVC3 framework?
  2. public ActionResult SomeDetails(int id, Thing thing)
  3. {
  4.        
  5. public class ThingModelBinder : DefaultModelBinder
  6. {
  7.     IThingFactory ThingFactory;
  8.  
  9.     public ThingModelBinder(IThingFactory thingFactory)
  10.     {
  11.         this.IThingFactory = thingFactory;
  12.     }
  13.  
  14.     protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
  15.     {
  16.         Thing thing = new Thing();
  17.  
  18.         string id_string = controllerContext.RouteData.Values["id"].ToString();
  19.         int id = 0;
  20.         Int32.TryParse(id_string, out id);
  21.         var thing = thingFactory.Get(id);
  22.  
  23.         return thing;
  24.     }
  25.  
  26.     public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  27.     {
  28.         if (bindingContext == null)
  29.         {
  30.             throw new ArgumentNullException("bindingContext");
  31.         }
  32.  
  33.         // custom section
  34.         object model = this.CreateModel(controllerContext, bindingContext, typeof(Thing));
  35.  
  36.         // set back to ModelBindingContext
  37.         bindingContext.ModelMetadata.Model = model;
  38.  
  39.         // call base class version
  40.         // this will use the overridden version of CreateModel() which calls the Repository
  41.         // object model = BindComplexModel(controllerContext, bindingContext);
  42.  
  43.         return base.BindModel(controllerContext, bindingContext);
  44.  
  45.     }
  46.        
  47. // Custom Model Binders
  48.     System.Web.Mvc.ModelBinders.Binders.Add(
  49.         typeof(MyCompany.Thing)
  50.         , new MyMvcApplication.ModelBinders.ThingModelBinder(
  51.             WindsorContainer.Resolve<IThingFactory>()
  52.             )
  53.         );
  54.        
  55. // This is the thing factory - it will create the thing if it has not already
  56. // been created with the given ID - if it is already created it will return
  57. // that instance
  58. public interface IThingFactory
  59. {
  60.     Thing Get(int id);
  61. }
  62.  
  63. // This is the thing - it has an ID and a method that you
  64. // can call that keeps track of how many times it has been
  65. // called (so you can be sure it is the same instance)
  66. public class Thing
  67. {
  68.     private int _count;
  69.  
  70.     public Thing(int id)
  71.     {
  72.         Id = id;
  73.     }
  74.  
  75.     public int Id { get; private set; }
  76.  
  77.     public int HowManyCalls()
  78.     {
  79.         return Interlocked.Increment(ref _count);
  80.     }
  81. }
  82.  
  83. // This is a typed factory selector to manage selecting the component from
  84. // the container by using the name ("Thing" followed by the ID)
  85. public class GetThingComponentSelector : ITypedFactoryComponentSelector
  86. {
  87.     public TypedFactoryComponent SelectComponent(MethodInfo method,
  88.                                                  Type type,
  89.                                                  object[] arguments)
  90.     {
  91.         return new TypedFactoryComponent("Thing" + arguments[0],
  92.                                          typeof(Thing),
  93.                                          new Arguments(arguments));
  94.     }
  95. }
  96.  
  97. // .... In the installer ....
  98.  
  99. // Register each thing with a different name that matches the ID
  100. // and register a custom component selector and the thing factory
  101. container
  102.     .AddFacility<TypedFactoryFacility>()
  103.     .Register(
  104.         Component
  105.             .For<Thing>()
  106.             .Named("Thing1"),
  107.         Component
  108.             .For<Thing>()
  109.             .Named("Thing2"),
  110.         Component
  111.             .For<GetThingComponentSelector>(),
  112.         Component
  113.             .For<IThingFactory>()
  114.             .AsFactory(c => c.SelectedWith<GetThingComponentSelector>()));
  115.  
  116. // ... Some demo code (you do not need to resolve the factory directly)
  117.  
  118. // Now resolve the same thing twice and then a different thing and make sure
  119. // Windsor has handled the lifestyle
  120. var thing = container.Resolve<IThingFactory>().Get(1);
  121. Console.WriteLine("ID should be 1 and is " + thing.Id
  122.     + ". Calls should be 1 and is " + thing.HowManyCalls());
  123.  
  124. thing = container.Resolve<IThingFactory>().Get(1);
  125. Console.WriteLine("ID should be 1 and is " + thing.Id
  126.     + ". Calls should be 2 and is " + thing.HowManyCalls());
  127.  
  128. thing = container.Resolve<IThingFactory>().Get(2);
  129. Console.WriteLine("ID should be 2 and is " + thing.Id
  130.     + ". Calls should be 1 and is " + thing.HowManyCalls());