예제 #1
0
        // Methods ......................................................................
        private void RootCompositionByCode()
        {
            // Repositories
            _userConfigRepository    = new DataAccess.ConfigurationRepository();
            _timeConverterRepository = new Domain.Functionality.TimeConvertionFunctions();

            // Services (represent the seam in the architecture)
            _userConfigService    = new Domain.Services.UserConfigService(ConfigFilename, _userConfigRepository);
            _timeConverterService = new Domain.Services.TimeConverterService(_timeConverterRepository);
        }
예제 #2
0
        private void RootCompositionByConfiguration()
        {
            // How to refrence an assembly in teh config file by assembly qualified name
            // "{namespace}.{class name}, "{assembly name}"
            // Ref: https://jeremylindsayni.wordpress.com/2019/02/11/instantiating-a-c-object-from-a-string-using-activator-createinstance-in-net/

            // Repositories
            var confiRepoTypeName = ConfigurationManager.AppSettings["configurationRepository"];
            var configRepoType    = Type.GetType(confiRepoTypeName, true);

            _userConfigRepository =
                (Domain.Interfaces.Repositories.IUserConfigRepository)Activator.CreateInstance(configRepoType);

            var timeConvertionTypeName = ConfigurationManager.AppSettings["timeConvertionFunction"];
            var timeConvertionType     = Type.GetType(timeConvertionTypeName, true);

            _timeConverterRepository = (Domain.Interfaces.Repositories.ITimeConverterRepository)Activator.CreateInstance(timeConvertionType);

            // Services (represent the seam in the architecture)
            _userConfigService    = new Domain.Services.UserConfigService(ConfigFilename, _userConfigRepository);
            _timeConverterService = new Domain.Services.TimeConverterService(_timeConverterRepository);
        }
예제 #3
0
        private void RootCompositionByDIContainer()
        {
            // Repositories - Retreive repositories from App.Config
            var confiRepoTypeName = ConfigurationManager.AppSettings["configurationRepository"];
            var configRepoType    = Type.GetType(confiRepoTypeName, true);

            _userConfigRepository =
                (Domain.Interfaces.Repositories.IUserConfigRepository)Activator.CreateInstance(configRepoType);

            var timeConvertionTypeName = ConfigurationManager.AppSettings["timeConvertionFunction"];
            var timeConvertionType     = Type.GetType(timeConvertionTypeName, true);

            _timeConverterRepository =
                (Domain.Interfaces.Repositories.ITimeConverterRepository)Activator.CreateInstance(timeConvertionType);


            // Define container and register ITimeConverterRepository
            _container = new UnityContainer();

            // REGISTER
            // Register the repositories that are needed
            _container.RegisterInstance <Domain.Interfaces.Repositories.IUserConfigRepository>(_userConfigRepository);
            _container.RegisterInstance <Domain.Interfaces.Repositories.ITimeConverterRepository> (_timeConverterRepository);

            // Services (represent the seam in the architecture)
            // For the service help the container understand the repository type that will be passed
            // as parameter for the constructor.
            _container.RegisterType <Domain.Interfaces.Services.IUserConfigService, Domain.Services.UserConfigService>
                (new InjectionConstructor(ConfigFilename, _userConfigRepository));
            _container.RegisterType <Domain.Interfaces.Services.ITimeConverterService, Domain.Services.TimeConverterService>
                (new InjectionConstructor(_timeConverterRepository));

            // RESOLVE
            // Instantiate a ITimeConverterService
            _userConfigService    = _container.Resolve <Domain.Interfaces.Services.IUserConfigService>();
            _timeConverterService = _container.Resolve <Domain.Interfaces.Services.ITimeConverterService>();
        }