3

After some time in Java with Spring, I'm trying to take all of IOC principles into some old and new C# Code, I've started using simple injector after reading some comparisons. I need different profiles in order to load different objects to my container. I have tried the following solution: configuring a variable in the app.config file with the name of Profile and then after reading it the code goes as follows:

        if (_profile.Equals("unit-test"))
        {
            _container.Register<ParkingService, ParkingService>(Lifestyle.Singleton);
            _container.Register<ITimeServices, TimeServicesMock>(Lifestyle.Singleton);
            _container.Register<IStorageServices, InMemoryStorage>(Lifestyle.Singleton);
        }

        else if (_profile.Equals("integration-test"))
        {
            _container.Register<ParkingService, ParkingService>(Lifestyle.Singleton);
            _container.Register<ITimeServices, TimeServices>(Lifestyle.Singleton);
            _container.Register<IStorageServices, MongoStorage>(Lifestyle.Singleton);
        }

Is there any out of the box solution for this or a better way of implementation?

Taldo
  • 103
  • 7
  • Btw. What are "profiles" exactly? – Steven Jan 06 '19 at 20:22
  • Maybe the phrasing was a bit off, but my question is as follows - in Java spring, I can create Beans (same as registering interfaces to implementations) according to different profiles. I can have a profile for unit testing and a different profile for integration testing and then the container will create different objects accordingly. the profile is passed to the application via a program argument/ environment variable.. – Taldo Jan 06 '19 at 20:28
  • I don't really understand what 'feature' it is you want, because it seems trivial to implement this, but perhaps I don't understand what 'profiles' means. Can you show you what you tried so far, and why this is not what you want. The closest thing that Simple Injector probably has to profiles is [packaging](https://www.nuget.org/packages/SimpleInjector.Packaging/) (see [the docs](https://simpleinjector.org/howto#package-registrations)). – Steven Jan 06 '19 at 21:29

0 Answers0