From vordrax, 1 Year ago, written in C#.
Embed
  1. using Autofac;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace SampleThing
  7. {
  8.     internal class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             IContainer container = BuildContainer();
  13.  
  14.             // Here is where we would wire up something to
  15.             // receive requests. It might be a message bus,
  16.             // it might be a WebApi controller (which has
  17.             // an autofac implementation that will DI for
  18.             // you with request-based scopes), or you might
  19.             // be using Quarts to fire timer-based events
  20.             // (which also has an Autofac implementation),
  21.             // or you might just be monitoring for Windows
  22.             // events. In any case,
  23.  
  24.             string file = RequestReceived();
  25.  
  26.             // If you are using something with a per-request
  27.             // autofac implementation, this is handled for you
  28.             using (ILifetimeScope scope = container.BeginLifetimeScope())
  29.             {
  30.                 // Begin the actual request processing
  31.                 var requestHandler = scope.Resolve<Presentation>();
  32.                 requestHandler.ProcessRequest(file);
  33.                 // Dependencies will be disposed of with the scope
  34.             }
  35.         }
  36.  
  37.         private static string RequestReceived()
  38.         {
  39.             return "An event will deliver this";
  40.         }
  41.  
  42.         private static IContainer BuildContainer()
  43.         {
  44.             ContainerBuilder builder = new ContainerBuilder();
  45.             builder
  46.                 .RegisterType<SampleContext>()
  47.                 .As<SampleContext>()
  48.                 // For WebApi, you can use InstancePerRequest()
  49.                 .InstancePerLifetimeScope();
  50.  
  51.             // For WebApi, this would be a controller
  52.             // and you wouldn't be configuring it
  53.             // here
  54.             builder
  55.                 .RegisterType<Presentation>()
  56.                 .As<Presentation>()
  57.                 .InstancePerLifetimeScope();
  58.             return builder.Build();
  59.         }
  60.     }
  61.  
  62.     public class Presentation
  63.     {
  64.         private SampleContext context;
  65.  
  66.         public Presentation(SampleContext context)
  67.         {
  68.             this.context = context;
  69.         }
  70.  
  71.         public void ProcessRequest(string file)
  72.         {
  73.             var accounts = file
  74.                 .Split('\n')
  75.                 .Select(line => ConvertToAccount(line));
  76.  
  77.             PersistAccounts(accounts);
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Parse line into an account
  82.         /// </summary>
  83.         /// <param name="line"></param>
  84.         /// <returns></returns>
  85.         private Account ConvertToAccount(string line)
  86.         {
  87.             throw new NotImplementedException();
  88.         }
  89.  
  90.         private void PersistAccounts(IEnumerable<Account> accounts)
  91.         {
  92.             context.Accounts.AddRange(accounts);
  93.             context.SaveChanges();
  94.         }
  95.     }
  96. }
  97.