daily pastebin goal
85%
SHARE
TWEET

C# WebAPI Dependency Configuration

joelmello Dec 2nd, 2016 3 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Autofac;
  2. using Autofac.Integration.WebApi;
  3. using Blacksmith.Core;
  4. using ExampleApp.Api.Auth;
  5. using ExampleApp.Api.Caching;
  6. using ExampleApp.Configuration;
  7. using ExampleApp.Manager.Contests;
  8. using ExampleApp.Manager.Funds;
  9. using ExampleApp.Manager.Paypal;
  10. using ExampleApp.Manager.Points;
  11. using ExampleApp.Manager.UserActions;
  12. using ExampleApp.Manager.Users;
  13. using ExampleApp.Manager.Wordlists;
  14. using ExampleApp.Model.Constants;
  15. using ExampleApp.Model.Models;
  16. using ExampleApp.Model.Models.ActivityLog;
  17. using ExampleApp.Model.Models.Paypal;
  18. using ExampleApp.Model.Models.SportRadar.NFL;
  19. using ExampleApp.Repository;
  20. using ExampleApp.Repository.Mongo;
  21. using System.Reflection;
  22. using System.Web.Http;
  23. using WebApi.OutputCache.V2;
  24.  
  25.  
  26. namespace ExampleApp.Api
  27. {
  28.     public static class ApiDependencyConfiguration
  29.     {
  30.         public static Autofac.IContainer Container { get; set; }
  31.  
  32.         public static void Configure(bool repositoriesReadOnly = false)
  33.         {
  34.             // Build service bus client
  35.             var credentials = IronMQConfiguration.GetConfiguration();
  36.             var ironBus = new Client(credentials.ProjectId, credentials.Token, credentials.Host);
  37.  
  38.             // Configure logging
  39.             {
  40.                 // Get credentials (checks build configuration Debug vs Release)
  41.                 var loggingCredentials = LoggingConfiguration.GetLoggingConfiguration();
  42.  
  43.                 // Configure logging provider
  44.                 Loggr.Utility.Configuration.UseSettings(new Loggr.Utility.Settings()
  45.                 {
  46.                     LogKey = loggingCredentials.LogKey,
  47.                     ApiKey = loggingCredentials.ApiKey
  48.                 });
  49.             }
  50.  
  51.             // Build container
  52.             var builder = new ContainerBuilder();
  53.             {
  54.                 /* Register repositories with IOC container */
  55.                 //Register controllers
  56.                 builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
  57.  
  58.                 // Model Repositories  
  59.                 var connectionString = DatabaseConfiguration.GetMongoConnectionString();
  60.                 builder.Register(c => new MongoRepository.MongoRepository<UserActivityLog>(connectionString)).InstancePerLifetimeScope();
  61.                 builder.Register(c => new MongoRepository.MongoRepository<ScheduledJob>(connectionString)).InstancePerLifetimeScope();
  62.                 builder.Register(c => new MongoRepository.MongoRepository<User>(connectionString)).InstancePerLifetimeScope();
  63.                 builder.Register(c => new MongoRepository.MongoRepository<CacheItem>(connectionString)).InstancePerLifetimeScope();
  64.                 builder.Register(c => new MongoRepository.MongoRepository<BalanceAccount>(connectionString)).InstancePerLifetimeScope();
  65.                 builder.Register(c => new MongoRepository.MongoRepository<Rank>(connectionString)).InstancePerLifetimeScope();
  66.                 builder.Register(c => new MongoRepository.MongoRepository<Contest>(connectionString)).InstancePerLifetimeScope();
  67.                 builder.Register(c => new MongoRepository.MongoRepository<ContestEntry>(connectionString)).InstancePerLifetimeScope();
  68.                 builder.Register(c => new MongoRepository.MongoRepository<IPNMessage>(connectionString)).InstancePerLifetimeScope();
  69.                 builder.Register(c => new MongoRepository.MongoRepository<ForgotPasswordRequest>(connectionString)).InstancePerLifetimeScope();
  70.                 builder.Register(c => new MongoRepository.MongoRepository<ContestInvite>(connectionString)).InstancePerLifetimeScope();
  71.                 builder.Register(c => new MongoRepository.MongoRepository<Withdrawal>(connectionString)).InstancePerLifetimeScope();
  72.                 builder.Register(c => new MongoRepository.MongoRepository<SportsEventsSchedule>(connectionString)).InstancePerLifetimeScope();
  73.                 builder.Register(c => new MongoRepository.MongoRepository<AthleteImage>(connectionString)).InstancePerLifetimeScope();
  74.                 builder.Register(c => new MongoRepository.MongoRepository<SportsDataMLBGameOutcome>(connectionString)).InstancePerLifetimeScope();
  75.                 builder.Register(c => new MongoRepository.MongoRepository<ContestAthletes>(connectionString)).InstancePerLifetimeScope();
  76.                 builder.Register(c => new MongoRepository.MongoRepository<StatsApiNHLAthlete>(connectionString)).InstancePerLifetimeScope();
  77.                 builder.Register(c => new MongoRepository.MongoRepository<SportsEventsSchedule>(connectionString)).InstancePerLifetimeScope();
  78.                 builder.Register(c => new MongoRepository.MongoRepository<FraudRiskFilter>(connectionString)).InstancePerLifetimeScope();
  79.                 builder.Register(c => new MongoRepository.MongoRepository<AccountCancellation>(connectionString)).InstancePerLifetimeScope();
  80.                 builder.Register(c => new MongoRepository.MongoRepository<BusinessMetric>(connectionString)).InstancePerLifetimeScope();
  81.                 builder.Register(c => new MongoRepository.MongoRepository<SportsEventsAthletePerformance>(connectionString)).InstancePerLifetimeScope();
  82.                 builder.Register(c => new MongoRepository.MongoRepository<ContestLobbyLightweight>(connectionString)).InstancePerLifetimeScope();
  83.                 builder.Register(c => new MongoRepository.MongoRepository<SportsDataNFLGameStatistics>(connectionString)).InstancePerLifetimeScope();
  84.                 builder.Register(c => new MongoRepository.MongoRepository<QueueConfiguration>(connectionString)).InstancePerLifetimeScope();
  85.                 builder.Register(c => new MongoRepository.MongoRepository<MaintenanceIssue>(connectionString)).InstancePerLifetimeScope();
  86.                 builder.Register(c => new MongoRepository.MongoRepository<SportsEventScheduleMatch>(connectionString)).InstancePerLifetimeScope();
  87.                 builder.Register(c => new MongoRepository.MongoRepository<ServiceStatus>(connectionString)).InstancePerLifetimeScope();
  88.                 builder.Register(c => new MongoRepository.MongoRepository<GameOutcomeNHL>(connectionString)).InstancePerLifetimeScope();
  89.                 builder.Register(c => new MongoRepository.MongoRepository<GameOutcomeNBA>(connectionString)).InstancePerLifetimeScope();
  90.                 builder.Register(c => new MongoRepository.MongoRepository<StatsApiNBAAthlete>(connectionString)).InstancePerLifetimeScope();
  91.                 builder.Register(c => new MongoRepository.MongoRepository<StatsApiNHLAthlete>(connectionString)).InstancePerLifetimeScope();
  92.                 builder.Register(c => new MongoRepository.MongoRepository<SportRadarNFLAthlete>(connectionString)).InstancePerLifetimeScope();
  93.                 builder.Register(c => new MongoRepository.MongoRepository<FinancialSummary>(connectionString)).InstancePerLifetimeScope();
  94.  
  95.                 builder.RegisterType<ActivityLogUserActionsRepository>().As<IActivityLogUserActionsRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  96.                 builder.RegisterType<ScheduledJobsRepository>().As<IScheduledJobsRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  97.                 builder.RegisterType<UserRepository>().As<IUserRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  98.                 builder.RegisterType<CacheItemsRepository>().As<ICacheItemsRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  99.                 builder.RegisterType<BalanceAccountRepository>().As<IBalanceAccountRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  100.                 builder.RegisterType<RankRepository>().As<IRankRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  101.                 builder.RegisterType<ContestRepository>().As<IContestRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  102.                 builder.RegisterType<ContestEntryRepository>().As<IContestEntryRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  103.                 builder.RegisterType<PaypalIPNRepository>().As<IPaypalIPNRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  104.                 builder.RegisterType<ForgotPasswordRequestRepository>().As<IForgotPasswordRequestRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  105.                 builder.RegisterType<ContestInviteRepository>().As<IContestInviteRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  106.                 builder.RegisterType<WithdrawalRepository>().As<IWithdrawalRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  107.                 builder.RegisterType<SportsEventsScheduleRepository>().As<ISportsEventsScheduleRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  108.                 builder.RegisterType<AthleteImageRepository>().As<IAthleteImageRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  109.                 builder.RegisterType<SportsDataMLBGameOutcomeRepository>().As<ISportsDataMLBGameOutcomeRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  110.                 builder.RegisterType<ContestAthletesRepository>().As<IContestAthletesRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  111.                 builder.RegisterType<StatsApiNHLAthleteRepository>().As<IStatsApiNHLAthleteRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  112.                 builder.RegisterType<SportsEventsScheduleRepository>().As<ISportsEventsScheduleRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  113.                 builder.RegisterType<FraudRiskFilterRepository>().As<IFraudRiskFilterRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  114.                 builder.RegisterType<AccountCancellationRepository>().As<IAccountCancellationRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  115.                 builder.RegisterType<BusinessMetricRepository>().As<IBusinessMetricRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  116.                 builder.RegisterType<SportsEventsAthletePerformanceRepository>().As<ISportsEventsAthletePerformanceRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  117.                 builder.RegisterType<ContestLobbyRepository>().As<IContestLobbyRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  118.                 builder.RegisterType<SportsDataNFLGameStatisticsRepository>().As<ISportsDataNFLGameStatisticsRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  119.                 builder.RegisterType<QueueConfigurationRepository>().As<IQueueConfigurationRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  120.                 builder.RegisterType<MaintenanceIssueRepository>().As<IMaintenanceIssueRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  121.                 builder.RegisterType<SportsEventScheduleMatchRepository>().As<ISportsEventScheduleMatchRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  122.                 builder.RegisterType<ServiceStatusRepository>().As<IServiceStatusRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  123.                 builder.RegisterType<GameOutcomeNHLRepository>().As<IGameOutcomeNHLRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  124.                 builder.RegisterType<GameOutcomeNBARepository>().As<IGameOutcomeNBARepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  125.                 builder.RegisterType<StatsApiNBAAthleteRepository>().As<IStatsApiNBAAthleteRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  126.                 builder.RegisterType<StatsApiNHLAthleteRepository>().As<IStatsApiNHLAthleteRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  127.                 builder.RegisterType<SportRadarNFLAthleteRepository>().As<ISportRadarNFLAthleteRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  128.                 builder.RegisterType<FinancialSummaryRepository>().As<IFinancialSummaryRepository>().WithParameter(RepoPropertyNames.RepoReadOnlyParameterName, repositoriesReadOnly).InstancePerRequest();
  129.  
  130.                 // Register the managers            
  131.                 builder.Register(c => new WordlistsManager(Startup.TracingEnabled)).InstancePerRequest();
  132.                 builder.Register(c => new UserActionsActivityLogManager(c.Resolve<IActivityLogUserActionsRepository>(), Startup.TracingEnabled, Startup.LogLocalActivity, Startup.LogIgnoredUserActivity)).InstancePerRequest();
  133.                 builder.Register(c => new UsersManager(c.Resolve<IUserRepository>(), Startup.TracingEnabled, c.Resolve<WordlistsManager>(), c.Resolve<IForgotPasswordRequestRepository>())).InstancePerDependency();
  134.  
  135.                 builder.Register(c => new ContestsSearchManager(c.Resolve<IContestRepository>(), c.Resolve<IContestEntryRepository>(), c.Resolve<IContestLobbyRepository>(), c.Resolve<IContestAthletesRepository>(), Startup.TracingEnabled)).InstancePerRequest();
  136.                 builder.Register(c => new FundsManager(c.Resolve<IUserRepository>(), c.Resolve<IBalanceAccountRepository>(), c.Resolve<UserActionsActivityLogManager>(), c.Resolve<PointsManager>(), c.Resolve<IPaypalIPNRepository>(), c.Resolve<IWithdrawalRepository>(), Startup.TracingEnabled)).InstancePerRequest();
  137.                 builder.Register(c => new PointsManager(c.Resolve<IUserRepository>(), c.Resolve<IRankRepository>(), c.Resolve<UserActionsActivityLogManager>(), Startup.TracingEnabled)).InstancePerRequest();
  138.                 builder.Register(c => new PaypalManager(Startup.TracingEnabled)).InstancePerRequest();
  139.                 builder.Register(c => new ContestCreationManager(c.Resolve<IContestRepository>(), c.Resolve<ISportsEventsScheduleRepository>(), c.Resolve<IContestAthletesRepository>(), Startup.TracingEnabled)).InstancePerRequest();
  140.  
  141.                 // Register more api specific managers
  142.                 builder.RegisterType<AuthManager>().PropertiesAutowired(PropertyWiringOptions.None).InstancePerRequest();
  143.  
  144.                 builder.RegisterType<AuthServerProvider>().PropertiesAutowired(PropertyWiringOptions.None).InstancePerRequest();
  145.  
  146.                 // Register servicebus
  147.                 builder.Register(c => ironBus).As<IClient>().SingleInstance();
  148.             }
  149.  
  150.             var container = builder.Build();
  151.             Container = container;
  152.  
  153.             // Create the dependency resolver.
  154.             var resolver = new AutofacWebApiDependencyResolver(container);
  155.  
  156.             // Configure Web API with the dependency resolver.
  157.             GlobalConfiguration.Configuration.DependencyResolver = resolver;
  158.  
  159.             // Cache provider for WebApi.CacheOutput
  160.             var cache = new MemoryCacheWithRemoveAll();
  161.             GlobalConfiguration.Configuration.CacheOutputConfiguration().RegisterCacheOutputProvider(() => cache);
  162.  
  163.         }
  164.     }
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top