protected override void Load(ContainerBuilder builder)
        {
            RegistrationHelper            registrationHelper = new RegistrationHelper(builder);
            List <IEndPointConfiguration> endPoints          = EndPointUtilities.LoadEndPoints("appsettings.json").ToList();

            builder.RegisterInstance(endPoints).SingleInstance(); // Register the endpoints with Autofac. Most applications will not need to do this.

            // API Name is an arbitrary name of your choosing that AdaptiveClient uses to link interfaces (IUsersService) to the
            // EndPoints that expose them (Prod_SQL_01, Prod_WebAPI_01).  The API_Name used here must match the API_Name
            // of related EndPoints in EndPoints.json file.

            // Register the endpoints with AdaptiveClient. All applications using AdaptiveClient need to do this.  Always register endPoints before registering clients.
            registrationHelper.RegisterEndPoints(endPoints);

            registrationHelper.RegisterService <UsersService_WebAPI, IUsersService>(EndPointType.HTTP, API_Name.DemoAPI, DataProvider.HTTP);
            registrationHelper.RegisterService <UsersService_MSSQL, IUsersService>(EndPointType.InProcess, API_Name.DemoAPI, DataProvider.MSSQL);
            registrationHelper.RegisterService <UsersService_MySQL, IUsersService>(EndPointType.InProcess, API_Name.DemoAPI, DataProvider.MySQL);
            registrationHelper.RegisterLogger(logMessage => Logger.Message = logMessage);


            // EndPoint Validators
            // No servers for MSSQL or MySQL and we don't have an API url so register some mocks...
            Mock <IEndPointValidator> validatorMock = new Mock <IEndPointValidator>();

            validatorMock.Setup(x => x.IsInterfaceAlive(It.IsAny <IEndPointConfiguration>())).Returns(true);
            builder.RegisterInstance(validatorMock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + DataProvider.MSSQL);
            builder.RegisterInstance(validatorMock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + DataProvider.MySQL);
            builder.RegisterInstance(validatorMock.Object).Keyed <IEndPointValidator>(EndPointType.HTTP + DataProvider.HTTP);

            // In a real app...
            //registrationHelper.RegisterEndPointValidator<MSSQL_EndPointValidator>(EndPointType.InProcess, DataProvider.MSSQL);
            //registrationHelper.RegisterEndPointValidator<MySQL_EndPointValidator>(EndPointType.InProcess, DataProvider.MySQL);



            // WCF (will fail and fall back)
            var usersServiceMock = new Mock <IUsersService>();

            usersServiceMock.Setup(x => x.SaveUser(It.IsAny <User>())).Throws(new Exception("Cant find database server."));
            usersServiceMock.Setup(x => x.GetUserByID(It.IsAny <int>())).Throws(new Exception("Cant find database server."));
            builder.RegisterInstance(usersServiceMock.Object).Keyed <IUsersService>(EndPointType.WCF + DataProvider.HTTP);
            registrationHelper.RegisterLogger(logMessage => Logger.Message = logMessage);
        }
    public static void RegisterComponents(ContainerBuilder builder)
    {
        RegistrationHelper registrationHelper = new RegistrationHelper(builder);

        // Register endPoints before registering clients
        registrationHelper.RegisterEndPoints(EndPointUtilities.LoadEndPoints("appsettings.json"));

        // Register services
        registrationHelper.RegisterService<UsersService_WebAPI, IUsersService>(EndPointType.HTTP, API_Name.UsersAPI, DataProvider.WebAPI);
        registrationHelper.RegisterService<UsersService_MSSQL, IUsersService>(EndPointType.InProcess, API_Name.UsersAPI, DataProvider.MSSQL);
        registrationHelper.RegisterService<UsersService_MySQL, IUsersService>(EndPointType.InProcess, API_Name.UsersAPI, DataProvider.MySQL);

        // Register logger (optional)
        registrationHelper.RegisterLogger(logMessage => Console.WriteLine(logMessage.Substring(0, 203)));

        builder.RegisterType<Demo>();
    }
Esempio n. 3
0
        public static IContainer CreateContainer(IEnumerable <IEndPointConfiguration> endPoints, string apiName, Action <string> logger)
        {
            /*
             * AdaptiveClient filters the collection of EndPoints that is registered and only uses the ones where IsActive = true.
             * However, for this demo app we want to force AdaptiveClient to encounter EndPoints that throw exceptions.
             * To do this we create a copy of the EndPoints list (fakeEndPoints) and set IsActive to true for each EndPoint in the copied list.
             * This effectively fools AdaptiveClient into thinking that all EndPoints are valid and should be used.
             * At the end of this method we look in the original EndPoint list and create service mocks for EndPoints where IsActive = false.
             * The resulting functionality is that AdaptiveClient attempts to use every EndPoint that is registered.  When it encounters a mock
             * it fails and tries to use the next EndPoint.
             */

            List <IEndPointConfiguration> fakeEndPoints = new List <IEndPointConfiguration>();

            foreach (IEndPointConfiguration ep in endPoints)
            {
                fakeEndPoints.Add(new PresentationEndPoint(ep)
                {
                    IsActive = true
                });
            }

            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterModule(new AutofacModule());
            builder.RegisterModule(new LeaderAnalytics.AdaptiveClient.EntityFrameworkCore.AutofacModule());
            RegistrationHelper registrationHelper = new RegistrationHelper(builder);

            registrationHelper
            .RegisterEndPoints(fakeEndPoints)
            .RegisterModule(new Zamagon.Services.Common.AdaptiveClientModule());

            if (apiName == API_Name.BackOffice || apiName == null)
            {
                registrationHelper.RegisterModule(new Zamagon.Services.BackOffice.AdaptiveClientModule());
            }

            if (apiName == API_Name.StoreFront || apiName == null)
            {
                registrationHelper.RegisterModule(new Zamagon.Services.StoreFront.AdaptiveClientModule());
            }


            if (logger != null)
            {
                registrationHelper.RegisterLogger(logger);
            }

            // Create mocks
            Mock <IOrdersService> ordersServiceMock = new Mock <IOrdersService>();

            ordersServiceMock.Setup(x => x.GetOrders()).Throws(new Exception("Mock failure"));
            IOrdersService ordersService = ordersServiceMock.Object;

            Mock <IProductsService> productsServiceMock = new Mock <IProductsService>();

            productsServiceMock.Setup(x => x.GetProducts()).Throws(new Exception("Mock failure"));
            IProductsService productsService = productsServiceMock.Object;

            Mock <IEmployeesService> empServiceMock = new Mock <IEmployeesService>();

            empServiceMock.Setup(x => x.GetEmployees()).Throws(new Exception("Mock failure"));
            IEmployeesService empService = empServiceMock.Object;

            Mock <ITimeCardsService> tcServiceMock = new Mock <ITimeCardsService>();

            tcServiceMock.Setup(x => x.GetTimeCards()).Throws(new Exception("Mock failure"));
            ITimeCardsService tcService = tcServiceMock.Object;

            // look at the list of endPoints that was passed to us and register a mock for each
            // one that is not marked active

            foreach (IEndPointConfiguration ep in endPoints.Where(x => !x.IsActive))
            {
                builder.RegisterInstance(ordersService).Keyed <IOrdersService>(ep.EndPointType + ep.ProviderName);
                builder.RegisterInstance(productsService).Keyed <IProductsService>(ep.EndPointType + ep.ProviderName);
                builder.RegisterInstance(empService).Keyed <IEmployeesService>(ep.EndPointType + ep.ProviderName);
                builder.RegisterInstance(tcService).Keyed <ITimeCardsService>(ep.EndPointType + ep.ProviderName);
            }

            return(builder.Build());
        }