diff --git a/EssentialTools/App_Start/NinjectWebCommon.cs b/EssentialTools/App_Start/NinjectWebCommon.cs new file mode 100644 index 0000000..7971432 --- /dev/null +++ b/EssentialTools/App_Start/NinjectWebCommon.cs @@ -0,0 +1,59 @@ +[assembly: WebActivator.PreApplicationStartMethod(typeof(EssentialTools.App_Start.NinjectWebCommon), "Start")] +[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(EssentialTools.App_Start.NinjectWebCommon), "Stop")] + +namespace EssentialTools.App_Start +{ + using System; + using System.Web; + + using Microsoft.Web.Infrastructure.DynamicModuleHelper; + + using Ninject; + using Ninject.Web.Common; + + public static class NinjectWebCommon + { + private static readonly Bootstrapper bootstrapper = new Bootstrapper(); + + /// + /// Starts the application + /// + public static void Start() + { + DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); + DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); + bootstrapper.Initialize(CreateKernel); + } + + /// + /// Stops the application. + /// + public static void Stop() + { + bootstrapper.ShutDown(); + } + + /// + /// Creates the kernel that will manage your application. + /// + /// The created kernel. + private static IKernel CreateKernel() + { + var kernel = new StandardKernel(); + kernel.Bind>().ToMethod(ctx => () => new Bootstrapper().Kernel); + kernel.Bind().To(); + + RegisterServices(kernel); + return kernel; + } + + /// + /// Load your modules or register your services here! + /// + /// The kernel. + private static void RegisterServices(IKernel kernel) + { + System.Web.Mvc.DependencyResolver.SetResolver(new EssentialTools.Infrastructure.NinjectDependencyResolver(kernel)); + } + } +} diff --git a/EssentialTools/App_Start/RouteConfig.cs b/EssentialTools/App_Start/RouteConfig.cs new file mode 100644 index 0000000..15c07d9 --- /dev/null +++ b/EssentialTools/App_Start/RouteConfig.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using System.Web.Routing; + +namespace EssentialTools +{ + public class RouteConfig + { + public static void RegisterRoutes(RouteCollection routes) + { + routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); + + routes.MapRoute( + name: "Default", + url: "{controller}/{action}/{id}", + defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } + ); + } + } +} diff --git a/EssentialTools/Controllers/HomeController.cs b/EssentialTools/Controllers/HomeController.cs new file mode 100644 index 0000000..fe31349 --- /dev/null +++ b/EssentialTools/Controllers/HomeController.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using EssentialTools.Models; + +namespace EssentialTools.Controllers +{ + public class HomeController : Controller + { + private IValueCalculator calc; + private Product[] products = + { + new Product {Name = "Kayak", Category = "Watersports", Price = 275M }, + new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M}, + new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M }, + new Product {Name = "Coner flag", Category = "Soccer", Price = 34.95M } + }; + + public HomeController(IValueCalculator calcParam) + { + calc = calcParam; + } + + public ActionResult Index() + { + //LinqValueCalculator calc = new LinqValueCalculator(); + //IValueCalculator calc = new LinqValueCalculator(); + //IKernel ninjectKernel = new StandardKernel(); + //ninjectKernel.Bind().To(); + //IValueCalculator calc = ninjectKernel.Get(); + + ShoppingCart cart = new ShoppingCart(calc) + { + Products = products + }; + + decimal totalValue = cart.CalculatorProdutTotal(); + + return View(totalValue); + } + } +} \ No newline at end of file diff --git a/EssentialTools/EssentialTools.csproj b/EssentialTools/EssentialTools.csproj new file mode 100644 index 0000000..9093cf0 --- /dev/null +++ b/EssentialTools/EssentialTools.csproj @@ -0,0 +1,173 @@ + + + + + + Debug + AnyCPU + + + 2.0 + {6605F0AA-CBB9-4E5D-A053-60EE3FD045A3} + {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + EssentialTools + EssentialTools + v4.5.2 + true + + + + + + + + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + + + true + pdbonly + true + bin\ + TRACE + prompt + 4 + + + + ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll + + + + ..\packages\Ninject.3.0.1.10\lib\net45-full\Ninject.dll + + + ..\packages\Ninject.Web.Common.3.0.0.7\lib\net45-full\Ninject.Web.Common.dll + + + ..\packages\Ninject.MVC3.3.0.0.6\lib\net45-full\Ninject.Web.Mvc.dll + + + + + + + + + + + + + + + + + + + ..\packages\WebActivator.1.5\lib\net40\WebActivator.dll + + + + + ..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll + + + ..\packages\Microsoft.AspNet.Webpages.3.2.3\lib\net45\System.Web.Webpages.dll + + + ..\packages\Microsoft.AspNet.Webpages.3.2.3\lib\net45\System.Web.Webpages.Deployment.dll + + + ..\packages\Microsoft.AspNet.Webpages.3.2.3\lib\net45\System.Web.Webpages.Razor.dll + + + ..\packages\Microsoft.AspNet.Webpages.3.2.3\lib\net45\System.Web.Helpers.dll + + + ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll + + + ..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll + + + + + + + + + + + + Global.asax + + + + + + + + + + + + + + Web.config + + + Web.config + + + + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + + + + + True + True + 1231 + / + http://localhost:1231/ + False + False + + + False + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/EssentialTools/Global.asax b/EssentialTools/Global.asax new file mode 100644 index 0000000..56dbebb --- /dev/null +++ b/EssentialTools/Global.asax @@ -0,0 +1 @@ +<%@ Application Codebehind="Global.asax.cs" Inherits="EssentialTools.MvcApplication" Language="C#" %> diff --git a/EssentialTools/Global.asax.cs b/EssentialTools/Global.asax.cs new file mode 100644 index 0000000..5c21db6 --- /dev/null +++ b/EssentialTools/Global.asax.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using System.Web.Routing; + +namespace EssentialTools +{ + public class MvcApplication : System.Web.HttpApplication + { + protected void Application_Start() + { + AreaRegistration.RegisterAllAreas(); + RouteConfig.RegisterRoutes(RouteTable.Routes); + } + } +} diff --git a/EssentialTools/Infrastructure/NinjectDependencyResolver.cs b/EssentialTools/Infrastructure/NinjectDependencyResolver.cs new file mode 100644 index 0000000..74a68de --- /dev/null +++ b/EssentialTools/Infrastructure/NinjectDependencyResolver.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using EssentialTools.Models; +using Ninject; + +namespace EssentialTools.Infrastructure +{ + public class NinjectDependencyResolver : IDependencyResolver + { + private IKernel kernel; + + public NinjectDependencyResolver(IKernel kernelParam) + { + kernel = kernelParam; + AddBindings(); + } + + public object GetService(Type serviceType) + { + return kernel.TryGet(serviceType); + } + + public IEnumerable GetServices(Type serviceType) + { + return kernel.GetAll(serviceType); + } + + private void AddBindings() + { + kernel.Bind().To(); + } + } +} \ No newline at end of file diff --git a/EssentialTools/Models/IValueCalculator.cs b/EssentialTools/Models/IValueCalculator.cs new file mode 100644 index 0000000..21bbdf0 --- /dev/null +++ b/EssentialTools/Models/IValueCalculator.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace EssentialTools.Models +{ + public interface IValueCalculator + { + decimal ValueProducts(IEnumerable products); + } +} \ No newline at end of file diff --git a/EssentialTools/Models/LinqValueCalculator.cs b/EssentialTools/Models/LinqValueCalculator.cs new file mode 100644 index 0000000..d4a7b55 --- /dev/null +++ b/EssentialTools/Models/LinqValueCalculator.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace EssentialTools.Models +{ + public class LinqValueCalculator : IValueCalculator + { + public decimal ValueProducts(IEnumerable products) + { + return products.Sum(p => p.Price); + } + } +} \ No newline at end of file diff --git a/EssentialTools/Models/Product.cs b/EssentialTools/Models/Product.cs new file mode 100644 index 0000000..74ce30f --- /dev/null +++ b/EssentialTools/Models/Product.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace EssentialTools.Models +{ + public class Product + { + public int ProductID { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public decimal Price { get; set; } + public string Category { get; set; } + } +} \ No newline at end of file diff --git a/EssentialTools/Models/ShoppingCart.cs b/EssentialTools/Models/ShoppingCart.cs new file mode 100644 index 0000000..6340fcc --- /dev/null +++ b/EssentialTools/Models/ShoppingCart.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace EssentialTools.Models +{ + public class ShoppingCart + { + private IValueCalculator calc; + + public ShoppingCart(IValueCalculator calcParam) + { + calc = calcParam; + } + + public IEnumerable Products { get; set; } + + public decimal CalculatorProdutTotal() + { + return calc.ValueProducts(Products); + } + } +} \ No newline at end of file diff --git a/EssentialTools/Properties/AssemblyInfo.cs b/EssentialTools/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e2966f3 --- /dev/null +++ b/EssentialTools/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("EssentialTools")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("EssentialTools")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("6605f0aa-cbb9-4e5d-a053-60ee3fd045a3")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/EssentialTools/Views/Home/Index.cshtml b/EssentialTools/Views/Home/Index.cshtml new file mode 100644 index 0000000..1455aff --- /dev/null +++ b/EssentialTools/Views/Home/Index.cshtml @@ -0,0 +1,18 @@ +@model decimal +@{ + Layout = null; +} + + + + + + + Index + + +
+ Total value is $@Model +
+ + diff --git a/EssentialTools/Views/web.config b/EssentialTools/Views/web.config new file mode 100644 index 0000000..a1139c1 --- /dev/null +++ b/EssentialTools/Views/web.config @@ -0,0 +1,42 @@ + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/EssentialTools/Web.Debug.config b/EssentialTools/Web.Debug.config new file mode 100644 index 0000000..fae9cfe --- /dev/null +++ b/EssentialTools/Web.Debug.config @@ -0,0 +1,30 @@ + + + + + + + + + + \ No newline at end of file diff --git a/EssentialTools/Web.Release.config b/EssentialTools/Web.Release.config new file mode 100644 index 0000000..da6e960 --- /dev/null +++ b/EssentialTools/Web.Release.config @@ -0,0 +1,31 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/EssentialTools/Web.config b/EssentialTools/Web.config new file mode 100644 index 0000000..253a82f --- /dev/null +++ b/EssentialTools/Web.config @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/EssentialTools/packages.config b/EssentialTools/packages.config new file mode 100644 index 0000000..4a64468 --- /dev/null +++ b/EssentialTools/packages.config @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/LanguageFeatures/App_Start/NinjectWebCommon.cs b/LanguageFeatures/App_Start/NinjectWebCommon.cs new file mode 100644 index 0000000..93f9c7f --- /dev/null +++ b/LanguageFeatures/App_Start/NinjectWebCommon.cs @@ -0,0 +1,58 @@ +[assembly: WebActivator.PreApplicationStartMethod(typeof(LanguageFeatures.App_Start.NinjectWebCommon), "Start")] +[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(LanguageFeatures.App_Start.NinjectWebCommon), "Stop")] + +namespace LanguageFeatures.App_Start +{ + using System; + using System.Web; + + using Microsoft.Web.Infrastructure.DynamicModuleHelper; + + using Ninject; + using Ninject.Web.Common; + + public static class NinjectWebCommon + { + private static readonly Bootstrapper bootstrapper = new Bootstrapper(); + + /// + /// Starts the application + /// + public static void Start() + { + DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); + DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); + bootstrapper.Initialize(CreateKernel); + } + + /// + /// Stops the application. + /// + public static void Stop() + { + bootstrapper.ShutDown(); + } + + /// + /// Creates the kernel that will manage your application. + /// + /// The created kernel. + private static IKernel CreateKernel() + { + var kernel = new StandardKernel(); + kernel.Bind>().ToMethod(ctx => () => new Bootstrapper().Kernel); + kernel.Bind().To(); + + RegisterServices(kernel); + return kernel; + } + + /// + /// Load your modules or register your services here! + /// + /// The kernel. + private static void RegisterServices(IKernel kernel) + { + } + } +} diff --git a/LanguageFeatures/LanguageFeatures.csproj b/LanguageFeatures/LanguageFeatures.csproj index ad7abb7..8c4e58b 100644 --- a/LanguageFeatures/LanguageFeatures.csproj +++ b/LanguageFeatures/LanguageFeatures.csproj @@ -106,7 +106,9 @@ - + + Designer + Web.config diff --git a/LearningMVC.sln b/LearningMVC.sln index f254683..963761e 100644 --- a/LearningMVC.sln +++ b/LearningMVC.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PartyInvites", "..\Lab\Part EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Razor", "Razor\Razor.csproj", "{58B43B8C-F613-4AA5-8A77-98A800578267}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EssentialTools", "EssentialTools\EssentialTools.csproj", "{6605F0AA-CBB9-4E5D-A053-60EE3FD045A3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {58B43B8C-F613-4AA5-8A77-98A800578267}.Debug|Any CPU.Build.0 = Debug|Any CPU {58B43B8C-F613-4AA5-8A77-98A800578267}.Release|Any CPU.ActiveCfg = Release|Any CPU {58B43B8C-F613-4AA5-8A77-98A800578267}.Release|Any CPU.Build.0 = Release|Any CPU + {6605F0AA-CBB9-4E5D-A053-60EE3FD045A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6605F0AA-CBB9-4E5D-A053-60EE3FD045A3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6605F0AA-CBB9-4E5D-A053-60EE3FD045A3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6605F0AA-CBB9-4E5D-A053-60EE3FD045A3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE