public virtual void Interpret(IStoreModel storeModel, ModelBuilder receiver)
        {
            CheckValue.NotNull(storeModel, nameof(storeModel));
            CheckValue.NotNull(receiver, nameof(receiver));

            var efEntities     = receiver.Model.GetEntityTypes();
            var configEntities = storeModel.GetStoreEntities();

            foreach (StoreEntityType configEntity in configEntities)
            {
                var clrType = configEntity.ClrType;

                foreach (var efEntity in efEntities)
                {
                    var efClrType = efEntity.ClrType;

                    if (clrType.IsAssignableFrom(efClrType) || TypeHelper.IsImplementedGenericInterface(efClrType, clrType))
                    {
                        foreach (var configStrategy in _options.Strategies)
                        {
                            configStrategy.Config(receiver, configEntity, efClrType);
                        }
                    }
                }
            }
        }
 public ClientCollectionCache(ISessionContainer sessionContainer, IStoreModel storeModel, ICosmosAuthorizationTokenProvider tokenProvider, IRetryPolicyFactory retryPolicy)
 {
     this.storeModel       = storeModel ?? throw new ArgumentNullException("storeModel");
     this.tokenProvider    = tokenProvider;
     this.retryPolicy      = retryPolicy;
     this.sessionContainer = sessionContainer;
 }
Пример #3
0
        internal Task <DocumentServiceResponse> ProcessMessageAsync(
            CosmosRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            DocumentServiceRequest serviceRequest = request.ToDocumentServiceRequest();

            //TODO: extrace auth into a separate handler
            string authorization = ((IAuthorizationTokenProvider)this.client.DocumentClient).GetUserAuthorizationToken(
                serviceRequest.ResourceAddress,
                PathsHelper.GetResourcePath(request.ResourceType),
                request.Method.ToString(), serviceRequest.Headers, AuthorizationTokenType.PrimaryMasterKey);

            serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization] = authorization;

            IStoreModel storeProxy = this.client.DocumentClient.GetStoreProxy(serviceRequest);

            if (request.OperationType == OperationType.Upsert)
            {
                return(this.ProcessUpsertAsync(storeProxy, serviceRequest, cancellationToken));
            }
            else
            {
                return(storeProxy.ProcessMessageAsync(serviceRequest, cancellationToken));
            }
        }
Пример #4
0
        private async Task <DocumentServiceResponse> ProcessUpsertAsync(IStoreModel storeProxy, DocumentServiceRequest serviceRequest, CancellationToken cancellationToken)
        {
            DocumentServiceResponse response = await storeProxy.ProcessMessageAsync(serviceRequest, cancellationToken);

            this.client.DocumentClient.CaptureSessionToken(serviceRequest, response);
            return(response);
        }
Пример #5
0
 public static bool IsLockByMe(this IStoreModel model, IIdentity identity)
 {
     if (!model.IsLock())
     {
         return(false);
     }
     return(model.LockBy == identity.GetUserName());
 }
Пример #6
0
        public static bool CanUnlock(this IStoreModel model, IIdentity identity)
        {
            if (model.IsLock())
            {
                return(true);
            }

            return(model.LockBy == identity.GetUserName());
        }
Пример #7
0
 public PartitionKeyRangeCache(IAuthorizationTokenProvider authorizationTokenProvider, IStoreModel storeModel, CollectionCache collectionCache)
 {
     this.routingMapCache = new AsyncCache <string, CollectionRoutingMap>(
         EqualityComparer <CollectionRoutingMap> .Default,
         StringComparer.Ordinal);
     this.authorizationTokenProvider = authorizationTokenProvider;
     this.storeModel      = storeModel;
     this.collectionCache = collectionCache;
 }
Пример #8
0
 public static bool Lock(this IStoreModel model, IIdentity identity)
 {
     if (!model.CanLock())
     {
         return(false);
     }
     model.LockBy = identity.GetUserName();
     model.LockAt = DateTime.Now;
     return(true);
 }
Пример #9
0
 public static bool Unlock(this IStoreModel model, IIdentity identity)
 {
     if (!model.CanUnlock(identity))
     {
         return(false);
     }
     model.LockBy = null;
     model.LockAt = null;
     return(true);
 }
        public ClientCollectionCache(IStoreModel storeModel, IAuthorizationTokenProvider tokenProvider, RetryPolicy retryPolicy)
        {
            if (storeModel == null)
            {
                throw new ArgumentNullException("storeModel");
            }

            this.storeModel    = storeModel;
            this.tokenProvider = tokenProvider;
            this.retryPolicy   = retryPolicy;
        }
Пример #11
0
 public PartitionKeyRangeCache(
     ICosmosAuthorizationTokenProvider authorizationTokenProvider,
     IStoreModel storeModel,
     CollectionCache collectionCache)
 {
     this.routingMapCache = new AsyncCacheNonBlocking <string, CollectionRoutingMap>(
         keyEqualityComparer: StringComparer.Ordinal);
     this.authorizationTokenProvider = authorizationTokenProvider;
     this.storeModel      = storeModel;
     this.collectionCache = collectionCache;
 }
Пример #12
0
        internal async Task <ResponseMessage> ProcessMessageAsync(
            RequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            DocumentServiceRequest serviceRequest = request.ToDocumentServiceRequest();

            ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow);

            serviceRequest.RequestContext.ClientRequestStatistics = clientSideRequestStatisticsTraceDatum;

            //TODO: extrace auth into a separate handler
            string authorization = await((ICosmosAuthorizationTokenProvider)this.client.DocumentClient).GetUserAuthorizationTokenAsync(
                serviceRequest.ResourceAddress,
                PathsHelper.GetResourcePath(request.ResourceType),
                request.Method.ToString(),
                serviceRequest.Headers,
                AuthorizationTokenType.PrimaryMasterKey,
                request.Trace);

            serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization] = authorization;

            IStoreModel storeProxy = this.client.DocumentClient.GetStoreProxy(serviceRequest);

            using (ITrace processMessageAsyncTrace = request.Trace.StartChild(
                       name: $"{storeProxy.GetType().FullName} Transport Request",
                       component: TraceComponent.Transport,
                       level: Tracing.TraceLevel.Info))
            {
                request.Trace = processMessageAsyncTrace;
                processMessageAsyncTrace.AddDatum("Client Side Request Stats", clientSideRequestStatisticsTraceDatum);

                DocumentServiceResponse response = null;
                try
                {
                    response = request.OperationType == OperationType.Upsert
                       ? await this.ProcessUpsertAsync(storeProxy, serviceRequest, cancellationToken)
                       : await storeProxy.ProcessMessageAsync(serviceRequest, cancellationToken);
                }
                finally
                {
                    processMessageAsyncTrace.UpdateRegionContacted(clientSideRequestStatisticsTraceDatum);
                }

                return(response.ToCosmosResponseMessage(
                           request,
                           serviceRequest.RequestContext.RequestChargeTracker));
            }
        }
Пример #13
0
 public static void SetUser(this IStoreModel model, IIdentity identity)
 {
     if (model.IsNew())
     {
         model.CreatedBy = identity.GetUserName();
         model.CreatedAt = DateTime.Now;
     }
     else
     {
         model.ModifiedBy = identity.GetUserName();
         model.ModifiedAt = DateTime.Now;
     }
 }
Пример #14
0
        internal async Task <ResponseMessage> ProcessMessageAsync(
            RequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            DocumentServiceRequest serviceRequest = request.ToDocumentServiceRequest();

            //TODO: extrace auth into a separate handler
            string authorization = await((ICosmosAuthorizationTokenProvider)this.client.DocumentClient).GetUserAuthorizationTokenAsync(
                serviceRequest.ResourceAddress,
                PathsHelper.GetResourcePath(request.ResourceType),
                request.Method.ToString(),
                serviceRequest.Headers,
                AuthorizationTokenType.PrimaryMasterKey,
                request.DiagnosticsContext);

            serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization] = authorization;

            IStoreModel storeProxy = this.client.DocumentClient.GetStoreProxy(serviceRequest);

            using (ITrace processMessageAsyncTrace = request.Trace.StartChild(
                       name: $"{storeProxy.GetType().FullName} Transport Request",
                       TraceComponent.Transport,
                       Tracing.TraceLevel.Info))
            {
                using (request.DiagnosticsContext.CreateScope(storeProxy.GetType().FullName))
                {
                    DocumentServiceResponse response = request.OperationType == OperationType.Upsert
                        ? await this.ProcessUpsertAsync(storeProxy, serviceRequest, cancellationToken)
                        : await storeProxy.ProcessMessageAsync(serviceRequest, cancellationToken);

                    return(response.ToCosmosResponseMessage(
                               request,
                               serviceRequest.RequestContext.RequestChargeTracker,
                               processMessageAsyncTrace));
                }
            }
        }
Пример #15
0
        internal async Task <ResponseMessage> ProcessMessageAsync(
            RequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            DocumentServiceRequest serviceRequest = request.ToDocumentServiceRequest();

            ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, request.Trace.Summary);

            serviceRequest.RequestContext.ClientRequestStatistics = clientSideRequestStatisticsTraceDatum;

            //TODO: extrace auth into a separate handler
            string authorization = await((ICosmosAuthorizationTokenProvider)this.client.DocumentClient).GetUserAuthorizationTokenAsync(
                serviceRequest.ResourceAddress,
                PathsHelper.GetResourcePath(request.ResourceType),
                request.Method.ToString(),
                serviceRequest.Headers,
                AuthorizationTokenType.PrimaryMasterKey,
                request.Trace);

            serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization] = authorization;

            IStoreModel storeProxy = this.client.DocumentClient.GetStoreProxy(serviceRequest);

            using (ITrace processMessageAsyncTrace = request.Trace.StartChild(
                       name: $"{storeProxy.GetType().FullName} Transport Request",
                       component: TraceComponent.Transport,
                       level: Tracing.TraceLevel.Info))
            {
                request.Trace = processMessageAsyncTrace;
                processMessageAsyncTrace.AddDatum("Client Side Request Stats", clientSideRequestStatisticsTraceDatum);

                DocumentServiceResponse response = null;
                try
                {
                    response = await storeProxy.ProcessMessageAsync(serviceRequest, cancellationToken);
                }
                catch (DocumentClientException dce)
                {
                    // Enrich diagnostics context in-case of auth failures
                    if (dce.StatusCode == System.Net.HttpStatusCode.Unauthorized || dce.StatusCode == System.Net.HttpStatusCode.Forbidden)
                    {
                        TimeSpan authProvideLifeSpan = this.client.DocumentClient.cosmosAuthorization.GetAge();
                        processMessageAsyncTrace.AddDatum("AuthProvider LifeSpan InSec", authProvideLifeSpan.TotalSeconds);
                    }

                    throw;
                }
                finally
                {
                    processMessageAsyncTrace.UpdateRegionContacted(clientSideRequestStatisticsTraceDatum);
                }

                ResponseMessage responseMessage = response.ToCosmosResponseMessage(
                    request,
                    serviceRequest.RequestContext.RequestChargeTracker);

                // Enrich diagnostics context in-case of auth failures
                if (responseMessage?.StatusCode == System.Net.HttpStatusCode.Unauthorized || responseMessage?.StatusCode == System.Net.HttpStatusCode.Forbidden)
                {
                    TimeSpan authProvideLifeSpan = this.client.DocumentClient.cosmosAuthorization.GetAge();
                    processMessageAsyncTrace.AddDatum("AuthProvider LifeSpan InSec", authProvideLifeSpan.TotalSeconds);
                }

                return(responseMessage);
            }
        }
 void IStoreModelExpression.Interpret <TReceiver>(IStoreModel storeModel, TReceiver receiver)
 => Interpret(storeModel, receiver as ModelBuilder);
Пример #17
0
 public StoreBusiness()
 {
     _istoreModel = new StoreModel();
 }
Пример #18
0
 public static bool IsNew(this IStoreModel model) => model.Key.IsNull();
Пример #19
0
 public StoreBusiness(IStoreModel istoreModel)
 {
     _istoreModel = istoreModel;
 }
Пример #20
0
 public static bool CanLock(this IStoreModel model) => model.LockBy.IsNull();
Пример #21
0
        /// <summary>
        /// Initializes a new instance of the ManageStoresViewModel class.
        /// </summary>
        /// <param name="storeModel"></param>
        public ManageStoresViewModel(IStoreModel storeModel)
        {
            this.storeModel = storeModel;

            #if DEBUG
            if (IsInDesignMode)
            {
                //this.UserStores = new ObservableCollection<StoreViewModel>() {
                //    new StoreViewModel(new Store() { ID = 123, City = "Toronto", StreetAddress = "123 Fake St.", Latitude = 43.0M, Longitude = -79.0M }),
                //    new StoreViewModel(new Store() { ID = 124, City = "Oakville", StreetAddress = "2280 River Oaks", Latitude = 43.5M, Longitude = -79.1M })
                //};

                //for (int i = 0; i < 20; i++)
                //{
                //    this.UserStores.Add(new StoreViewModel(new Store() { ID = i, City = "Toronto", StreetAddress = i + " Some Street", Latitude = 43M + i / 10, Longitude = -79M - i / 10 }));
                //}

                StoreViewModel newStoreViewModel1 = new StoreViewModel()
                {
                    Id = 123,
                    Latitude = 43.0M,
                    Longitude = -79.0M
                };

                newStoreViewModel1.SetAddress("123 Fake St.", "Toronto");

                StoreViewModel newStoreViewModel2 = new StoreViewModel()
                {
                    Id = 124,
                    Latitude = 43.5M,
                    Longitude = -79.1M
                };

                newStoreViewModel2.SetAddress("2280 River Oaks", "Oakville");

                this.SearchResults = new ObservableCollection<UserStoreViewModel>()
                {
                    new UserStoreViewModel(newStoreViewModel1)
                    {
                        UsedByUser = true,
                        Index = 1
                    },

                    new UserStoreViewModel(newStoreViewModel2)
                    {
                        UsedByUser = true,
                        Index = 2
                    }
                };

                for (int i = 0; i < 20; i++)
                {
                    StoreViewModel newStoreViewModel = new StoreViewModel()
                    {
                        Id = i,
                        Latitude = 43.0M + i / 10,
                        Longitude = -79.0M - i / 10
                    };

                    newStoreViewModel.SetAddress(i + " Fake St.", "City " + i);
                    this.SearchResults.Add(new UserStoreViewModel(newStoreViewModel)
                    {
                        UsedByUser = i % 2 == 0,
                        Index = i + 3
                    });
                }
            }
            #endif

            this.AddStoreCommand = new RelayCommand<UserStoreViewModel>((store) => this.AddStore(store));
            this.RemoveStoreCommand = new RelayCommand<StoreViewModel>((store) => this.RemoveStore(store));
            this.FindStoresCommand = new RelayCommand<string>((cityName) => this.FindStores(cityName));
            this.FindStoresByPostalCodeCommand = new RelayCommand(() => this.FindStoresByPostalCode());
            this.FindStoresByPersonalLocationCommand = new RelayCommand<PersonalLocationViewModel>((personalLocation) => this.FindStoresByPersonalLocation(personalLocation));
            this.SearchRadiusItems = new List<int>() { 5, 10, 15, 20, 2000 };
            this.SearchPersonalLocationRadius = this.SearchRadiusItems[0];
            this.SearchRadius = this.SearchRadiusItems[0];

            if (!IsInDesignMode)
            {
                this.SearchResults = new ObservableCollection<UserStoreViewModel>();
                this.PersonalLocations = new ObservableCollection<PersonalLocationViewModel>();
                this.UserStores = new ObservableCollection<StoreViewModel>();
                this.Refresh();
            }
        }
Пример #22
0
 public static bool IsLock(this IStoreModel model) => !model.LockBy.IsNullOrEmpty() && !model.LockAt.IsNull();