public static Weapon GetWeapons(Hero fightingHero) { var serviceProvider3 = DIConfiguration.ConfigurazioneWeapon(); WeaponService weaponService = serviceProvider3.GetService <WeaponService>(); Console.WriteLine("Le armi a tua disposizione sono:"); weapon: //List<Weapon> weapons = new List<Weapon>; var weapons = weaponService.GetAllWeapons(fightingHero); for (int i = 0; i < weapons.Count(); i++) { Console.WriteLine("{0} - {1}", i, weapons[i].name); } Console.WriteLine("Inserisci il numero dell'arma che hai scelto:"); try { int index = Convert.ToInt32(Console.ReadLine()); Weapon w = weapons[index]; Console.WriteLine("Il tuo personaggio è un {0} di nome {1} la cui arma è {2}", fightingHero.classPerson, fightingHero.name, w.name); return(w); } catch (Exception e) { Console.WriteLine("Inserimento non valido"); goto weapon; } }
public static Monster GetMonster(Hero fightingHero) { var serviceProvider4 = DIConfiguration.ConfigurazioneMonster(); MonsterService MonsterService = serviceProvider4.GetService <MonsterService>(); var monster = MonsterService.GetAllMonster(fightingHero); //mi rende i mostri con livello uguale o mini var random = new Random(); var serviceProvider3 = DIConfiguration.ConfigurazioneLevel(); LevelService levelService = serviceProvider3.GetService <LevelService>(); var Levels = levelService.GetLivelliInfo(); int index = random.Next(monster.Count); Monster fightingMonster = monster[index]; for (int i = 0; i < Levels.Count; i++) { if (Levels[i].livello == fightingMonster.level) { fightingMonster.lifePoint = Levels[i].lifePoint; } } Console.WriteLine("Stai sfidando {0} la cui arma è {1}", fightingMonster.classPerson, fightingMonster.weapon.name); return(fightingMonster); }
public static (int, Player) MatchPredisposition() { Console.WriteLine(" BENVENUTO \nHero VS Monster"); var serviceProvider = DIConfiguration.ConfigurazionePlayer(); PlayerService playerService = serviceProvider.GetService <PlayerService>(); Console.WriteLine("Inserisci nome utente: "); string nomePlayer = Console.ReadLine(); List <Player> players = playerService.GetAllPlayer(); (bool answer, Player p) = PlayerService.IsRegistred(nomePlayer, players); //é registrato? true: si, false: no //se è registrato ricavo il suo ID if (answer == false) { //Se non é registrato, lo aggiungo e ricavo il suo ID var NewPlayer = PlayerService.Registration(nomePlayer); var pl = playerService.CreatePlayer(NewPlayer); p.ID = PlayerService.GetPlayerID(players, pl); //id = PlayerService.GetExistingPlayerID(players); } return(p.ID, p); }
public void TestResolvingMultipleImplementations() { logger.Trace("Entering TestResolvingMultipleImplementations Test"); var dependencies = new DIConfiguration(); dependencies.Register <IVehicle, Car>(true); dependencies.Register <IVehicle, Bike>(true); var provider = new DIContainer(dependencies); logger.Trace("Container created"); var resolved = provider.Resolve <IEnumerable <IVehicle> >() as List <object>; logger.Trace("Received resolve object"); try { Assert.AreEqual(resolved.Count, 2); Type[] actual = new Type[] { resolved[0].GetType(), resolved[1].GetType() }; Type[] expected = new Type[] { typeof(Car), typeof(Bike) }; CollectionAssert.AreEquivalent(actual, expected); } catch (Exception e) { logger.Error(e.Message); throw; } }
public static void CreateMonster() { var serviceProvider4 = DIConfiguration.ConfigurazioneMonster(); MonsterService MonsterService = serviceProvider4.GetService <MonsterService>(); Console.WriteLine("Categoria del mostro: "); string c = Console.ReadLine(); Console.WriteLine("Livello: "); int l = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Arma: "); string weaponName = Console.ReadLine(); Console.WriteLine("Punti danno dell'arma MUOOOSTROSA: "); int damagePoint = Convert.ToInt32(Console.ReadLine()); Monster m = new Monster() { classPerson = c, level = l, weapon = new Weapon { name = weaponName, damagePoint = damagePoint } }; MonsterService.CreateMonster(m); }
public void TestComplexNamedDependency() { logger.Trace("Entering TestComplexNamedDependency Test"); var dependencies = new DIConfiguration(); dependencies.Register <TDependency, TComplexNamedImplementation>(true); dependencies.Register <IAnimal, Dog>(true); dependencies.Register <IVehicle, Car>(true, ImplementationName.First); dependencies.Register <IVehicle, Bike>(true, ImplementationName.Second); var provider = new DIContainer(dependencies); logger.Trace("Container created"); var complexImplementation = (TComplexNamedImplementation)provider.Resolve <TDependency>(); logger.Trace("Received resolve object"); try { Assert.AreEqual(1, complexImplementation.vehicles.Count); Assert.That(complexImplementation.vehicles[0], Is.InstanceOf <Car>()); Assert.AreEqual(typeof(Dog), complexImplementation.animal.GetType()); } catch (Exception e) { logger.Error(e.Message); throw; } }
public void TestExceptionSituations() { logger.Trace("Entering TestExceptionSituations Test"); var dependencies = new DIConfiguration(); try { logger.Trace("Trying register dependency"); Assert.Throws <ArgumentException>(delegate { dependencies.Register <TDependency, TDependency>(true); }); } catch (Exception e) { logger.Error(e.Message); throw; } var provider = new DIContainer(dependencies); logger.Trace("Container created"); try { logger.Trace("Trying receive resolve object"); Assert.Throws <KeyNotFoundException> (delegate { var genericObject = (ServiceImpl <TDependency>)provider.Resolve <IService <TDependency> >(); }); } catch (Exception e) { logger.Error(e.Message); throw; } }
public void TestNamedImplementation() { logger.Trace("Entering TestNamedImplementation Test"); var dependencies = new DIConfiguration(); dependencies.Register <IVehicle, Car>(true, ImplementationName.First); dependencies.Register <IVehicle, Bike>(true, ImplementationName.Second); var provider = new DIContainer(dependencies); logger.Trace("Container created"); var objCar = provider.Resolve <IVehicle>(ImplementationName.First); logger.Trace("Received resolve object 1"); var objBike = provider.Resolve <IVehicle>(ImplementationName.Second); logger.Trace("Received resolve object 2"); try { Assert.That(objCar, Is.InstanceOf(typeof(Car))); Assert.That(objBike, Is.InstanceOf(typeof(Bike))); } catch (Exception e) { logger.Error(e.Message); throw; } }
public void TestOpenGenericDependencies() { logger.Trace("Entering TestOpenGenericDependencies Test"); var dependencies = new DIConfiguration(); dependencies.Register(typeof(IService <>), typeof(ServiceImpl <>)); dependencies.Register <TDependency, TImplementation>(true); var provider = new DIContainer(dependencies); logger.Trace("Container created"); var genericObject = (ServiceImpl <TDependency>)provider.Resolve <IService <TDependency> >(); logger.Trace("Received resolve object"); try { Assert.AreEqual(genericObject.repository.GetType(), typeof(TImplementation)); } catch (Exception e) { logger.Error(e.Message); throw; } }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); var builder = new ContainerBuilder(); // Get your HttpConfiguration. var config = GlobalConfiguration.Configuration; // Register your Web API controllers. builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterType <UnitOfWork>().As <IUnitOfWork>(); DIConfiguration.RegisterServices(builder);// builder.RegisterType<StudentService>().As<IStudentService>(); builder.RegisterType <FugaDbcontext>().As <FugaDbcontext>(); builder.RegisterType <FugaContext>().As <FugaContext>(); // OPTIONAL: Register the Autofac filter provider. builder.RegisterWebApiFilterProvider(config); // OPTIONAL: Register the Autofac model binder provider. builder.RegisterWebApiModelBinderProvider(); // Set the dependency resolver to be Autofac. var container = builder.Build(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { DIConfiguration.ConfigureDependencies(services, Configuration); // Add framework services. services.AddMvc(); }
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSingleton((s) => Mapper.Instance); Mapper.Initialize(AutoMapperProfileConfiguration.Configure); DIConfiguration.ConfigureDI(services); SetUpDataBase(services); }
protected void Application_Start() { ServerCredentialsConfiguration.Configure(); AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); DIConfiguration.Configure(); MetadataProvidersConfiguration.Configure(); FilterProvidersConfiguration.Configure(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.Filters.Add(typeof(GlobalExceptionFilter)); }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1); DIConfiguration.Configurate(services, Configuration); }
private static void WhenNotOverridden_ShouldReturnDefaultImplementation(Type interfaceType, Type defaultImplementationType) { var configuration = new DIConfiguration(); var mappedTypes = configuration .Build(); Assert.IsTrue(mappedTypes.ContainsKey(interfaceType)); Assert.AreEqual(defaultImplementationType, mappedTypes[interfaceType]); }
// // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching(UIApplication app, NSDictionary options) { Rg.Plugins.Popup.Popup.Init(); global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental"); global::Xamarin.Forms.Forms.Init(); global::Xamarin.Forms.FormsMaterial.Init(); DIServiceContainer.Container = DIConfiguration.Configure(); LoadApplication(new App()); return(base.FinishedLaunching(app, options)); }
private static void WhenOverridden_ShouldReturnOverriddenImplementationType(Type interfaceType, Type overriddenImplementationType, Action <OverridableServicesContainer> getServiceToOverride) { var configuration = new DIConfiguration(); var mappedTypes = configuration .SetImplementationType(getServiceToOverride) .Build(); Assert.IsTrue(mappedTypes.ContainsKey(interfaceType)); Assert.AreEqual(overriddenImplementationType, mappedTypes[interfaceType]); }
public void ConfigurationVerify_ComplexFail() { DIConfiguration sut = new DIConfiguration(); sut.Register = new Register[1]; sut.Register[0] = new Register(); sut.Register[0].Contract = "LightweightIOC.Tests.Mocks.ITesting, LightweightIOC.Tests"; sut.Register[0].Implementation = "LightweightIOC.Tests.Mocks.TestImpling, LightweightIOC.Tests"; sut.Verify(); }
public void ConfigurationVerify_ComplexOK() { DIConfiguration sut = new DIConfiguration(); sut.Register = new Register[1]; sut.Register[0] = new Register(); sut.Register[0].Contract = "LightweightIOC.Tests.Mocks.ITest, LightweightIOC.Tests, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null"; sut.Register[0].Implementation = "LightweightIOC.Tests.Mocks.TestImpl, LightweightIOC.Tests, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null"; sut.Verify(); }
public void IsRegistered_False() { DIConfiguration sut = new DIConfiguration(); sut.Register = new Register[1]; sut.Register[0] = new Register(); sut.Register[0].Contract = "LightweightIOC.Tests.Mocks.ITest, LightweightIOC.Tests, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null"; sut.Register[0].Implementation = "LightweightIOC.Tests.Mocks.TestImpl, LightweightIOC.Tests, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null"; Assert.IsFalse(sut.IsRegistered(typeof(IServiceProvider))); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); // static function to configure DI in separate project DIConfiguration.Configure(Configuration, services); // static function to configure DB Contexts in separate project ContextConfiguration.Configuration(Configuration, services); // In production, the React files will be served from this directory services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/build"; }); }
public void AbstractClassDependency() { var conf = new DIConfiguration(); conf.Register <BarBase, AbstrBar>(); var container = new DIContainer(conf); var bar = container.Resolve <BarBase>(); Assert.IsNotNull(bar); }
public void Build_ConfigureViewModelProvidersIn_ShouldMapAllViewModelFactories() { var types = new DIConfiguration() .ConfigureViewModelProvidersIn(this.GetType().Assembly) .Build(); var interfaceType = typeof(IViewModelFactory <DummyViewModel, DummyViewModelProviderArgument>); var implementationType = typeof(ViewModelFactory <DummyViewModel, DummyViewModelProviderArgument>); Assert.IsTrue(types.ContainsKey(interfaceType)); Assert.AreEqual(implementationType, types[interfaceType]); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); DIConfiguration.SetStringConnection(GetStringConnection()); DIConfiguration.Mapper(services); // In production, the React files will be served from this directory services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/build"; }); }
public void ResolveGenType() { var conf = new DIConfiguration(); conf.Register <IBar, BarFromABar>(); conf.Register(typeof(IGenBar <IBar>), typeof(IGenBar <IBar>)); var container = new DIContainer(conf); IGenBar <IBar> ogen = container.Resolve <IGenBar <IBar> >(); Assert.IsNotNull(ogen); }
public void IsNotSingleton() { DIConfiguration conf = new DIConfiguration(); conf.Register <IBar, BarFromABar>(); DIContainer container = new DIContainer(conf); IBar bar1 = container.Resolve <IBar>(); IBar bar2 = container.Resolve <IBar>(); Assert.AreNotEqual(bar1, bar2); }
public void IsSingleton() { var conf = new DIConfiguration(); conf.Register <IBar, Bar>().AsSingleton(); var container = new DIContainer(conf); var bar1 = container.Resolve <IBar>(); var bar2 = container.Resolve <IBar>(); Assert.AreEqual(bar1, bar2); }
public void IsNotSingleton() { var conf = new DIConfiguration(); conf.Register <IBar, Bar>().InstancePerRequest(); var container = new DIContainer(conf); var bar1 = container.Resolve <IBar>(); var bar2 = container.Resolve <IBar>(); Assert.AreNotEqual(bar1, bar2); }
public void DependencyInjectionThroughConstructor() { var conf = new DIConfiguration(); conf.Register <IBar, Bar>(); conf.Register <IFoo, Foo>(); var container = new DIContainer(conf); var foo = container.Resolve <IFoo>(); Assert.IsNotNull(foo.Bar); }
public void AbstractClassAdded() { DIConfiguration conf = new DIConfiguration(); try { conf.Register <ABar, ABar>(); Assert.Fail("registered class is abstract"); } catch (Exception e) { Assert.IsNotNull(e, e.Message); } }