Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Friday, 24 January 2014
( EF6 | MVC5 | Unity | UoW )

http://blog.longle.net/2014/01/06/unit-of-work-unity-3-quick-start-video/

  • Entity Framework Power Tools
  • UoW & Repo Framework
  • Generating EF Mappings and POCO’s
  • Upgrading the stack to EF 6.2
  • Basic Customer CRUD Use Case with MVC Scafolding Template w/ Async
  • Refactoring the CustomerController to use UoW & Repo Framework w/ Async
  • Why the ICustomerService Approach?
  • Why Commit Unit of Work Outside the Service vs. versa
  • Quick Examples with Eager Loading, Filter, and Sorting

image

| | # 
# Tuesday, 21 January 2014
( IoC | Unity )

image

 

image

MVC Uses a Static Dependency Resolver.. to resolve all of its dependencies.  ie MVC will use our implementation.

Unity allows for

  • Constructor based injection (like we’ve used)
  • Property
  • Method

Can setup dependencies using

  • Code
  • Configuration (ie change without recompile)
  • Convention

Can use decorator pattern for more AOP.  And lifetime management eg singleton instance easily.

image
Going to include a bootstrapper class which makes things easier.

Looks like Unity.MVC5 doesn’t need to bootstrap file like in MVC4

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            UnityConfig.RegisterComponents();  

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            //var factory = new CustomControllerFactory();
            //ControllerBuilder.Current.SetControllerFactory(factory);
        }

which points to.. Ahh, Initialise in bootstrapper is just like the 2 lines below.

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();
            
        // register all your components with the container here
        // it is NOT necessary to register your controllers
            
        // e.g. container.RegisterType<ITestService, TestService>();
            
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}
image

Can see we are using Unity, but cant find the type. ie there is no registration!

public static void RegisterComponents()
{
    var container = new UnityContainer();
            
    // register all your components with the container here
    // it is NOT necessary to register your controllers
            
    // e.g. container.RegisterType<ITestService, TestService>();

    container.RegisterType<IProteinTrackingService, ProteinTrackingService>();
    container.RegisterType<IProteinRepository, ProteinRepository>();

    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}

Repetitive mapping code.. can use convention

public static void RegisterComponents()
{
    var container = new UnityContainer();
            
    // it is NOT necessary to register your controllers
    container.RegisterTypes(
        AllClasses.FromLoadedAssemblies(),
        WithMappings.FromMatchingInterface, // Convention of an I in front of interface name
        WithName.Default
        );  // Default not a singleton in Unity

    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
| | # 
# Monday, 20 January 2014
( IoC | Unity | Windsor )

http://simpleprogrammer.com/

Maybe this will be a simple way forward for the Winter Project?  He is principally using Unity.

http://pluralsight.com/training/Courses/Description/ioc-aspdotnet-mvc4

image

 

image

we can make our own controller Factory

image

Also likely that we’ll want to inject into our views.  ViewResult comes back from an Action.

Basics

image
Want to invert the normal control classes have on their dependencies

ie nomally if we new up a class within another class, then this ‘dependency’ is problematic

  • Higher level classes are affected by lower level

eg if create instance of Laptop, we’d need to create instances of Battery, WallOutlet and SolarCell.

 

  • Our laptop is dependant on the IPowerSource interface
  • But how to create an instance of laptop and have battery, walloutlet etc..

Most Popular

  • Ninject 1
  • Unity 2 (MS implementation)

image

We can do this manually with Constructor based DI, but tedious and if some of power sources had their own dependencies, then complex and error prone.

Container

  • Tell which Interfaces are mapped to which implementations
  • Resolve them for us
  • eg IpowerSource should be mapped to battery class
  • When ask IoC container for instance of our laptop class
  • it will automatically construct laptop class with battery dependency passed in.

Therefore we need to hook into the correct part of MVC pipeline.

 


Windsor in here.

image

image

running as admin didn’t work

Found that if I saved the app on an AltF4, then reloaded (with DA off) it worked.

Controller Factory vs Dependency Resolver

Going to use DR as using Filter injection

| | #