private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog?log = null, IGitRepository?repository = null, IFileSystem?fileSystem = null, IEnvironment?environment = null) => ConfigureServices(services => { services.AddSingleton <IGitVersionContextFactory, GitVersionContextFactory>(); services.AddSingleton(sp => { var options = sp.GetRequiredService <IOptions <GitVersionOptions> >(); var contextFactory = sp.GetRequiredService <IGitVersionContextFactory>(); return(new Lazy <GitVersionContext>(() => contextFactory.Create(options.Value))); }); if (log != null) { services.AddSingleton(log); } if (fileSystem != null) { services.AddSingleton(fileSystem); } if (repository != null) { services.AddSingleton(repository); } if (environment != null) { services.AddSingleton(environment); } var options = Options.Create(gitVersionOptions); services.AddSingleton(options); services.AddSingleton(RepositoryExtensions.ToGitRepositoryInfo(options)); });
private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog log = null, IGitRepository repository = null, IFileSystem fileSystem = null, IEnvironment environment = null) { return(ConfigureServices(services => { if (log != null) { services.AddSingleton(log); } if (fileSystem != null) { services.AddSingleton(fileSystem); } if (repository != null) { services.AddSingleton(repository); } if (environment != null) { services.AddSingleton(environment); } var options = Options.Create(gitVersionOptions); services.AddSingleton(options); services.AddSingleton(RepositoryExtensions.ToGitRepositoryInfo(options)); })); }
public async Task AddIfNew_AddsSecondByProperty() { TestEntity entity1 = new TestEntity(Guid.NewGuid()) { Value = "Foo" }; repository.Add(entity1); repository.SaveChanges(); TestEntity entity2 = new TestEntity(Guid.NewGuid()) { Value = "Bar" }; TestEntity result = await RepositoryExtensions.AddIfNewAsync(repository, x => x.Value, entity2); repository.SaveChanges(); Assert.Equal(entity2, result); Assert.Equal(2, repository.FindAll <TestEntity>().Count()); Assert.Contains(repository.FindAll <TestEntity>(), x => x == entity1); Assert.Contains(repository.FindAll <TestEntity>(), x => x == entity2); }
public void When_calling_QueryAsync_with_null_repository_Then_ArgumentNullException_is_thrown() { Assert.Multiple(() => { Assert.That(() => RepositoryExtensions.QueryAsync((IRepository <Foo>)null, q => q), Throws.InstanceOf <ArgumentNullException>()); Assert.That(() => RepositoryExtensions.QueryAsync((IRepository <Foo>)null, q => q, _fixture.CreateMany <string>()), Throws.InstanceOf <ArgumentNullException>()); }); }
public void Get_WhenRepositoryIsNull_ThrowsArgumentNullException <TKey>(TKey defaultKey) { // Arrange & Act & Assert var exception = Assert.Throws <ArgumentNullException>( () => RepositoryExtensions.Get <IEntity <TKey>, TKey>(null, x => x.Id.Equals(defaultKey)) ); Assert.Equal("repository", exception.ParamName); }
public async Task GetListModelAsync_WhenRepositoryIsNull_ThrowsArgumentNullException <TKey>(TKey defaultKey) { // Arrange & Act & Assert var exception = await Assert.ThrowsAsync <ArgumentNullException>( () => RepositoryExtensions.GetListAsync <FakeModel, IEntity <TKey>, TKey>(null, x => x.Id.Equals(defaultKey)) ); Assert.Equal("repository", exception.ParamName); }
/// <inheritdoc /> public IPollingJobConfiguration Add(Type implementationType) { RepositoryExtensions.ValidatePollingJobImplementationType(implementationType); RedisPollingJobConfiguration result = new RedisPollingJobConfiguration(implementationType); this.queues.Add(implementationType, result); return(result); }
public async Task <TDocumentStore> CreateDocumentRepositoryAsync <TDocumentStore>(Tenant tenant) where TDocumentStore : IRepository { if (!_typeMap.ContainsKey(typeof(TDocumentStore))) { new ArgumentOutOfRangeException($"{typeof(TDocumentStore).FullName} is not registered."); } return((TDocumentStore)(await RepositoryExtensions .CreateDocumentRepository(_typeMap[typeof(TDocumentStore)], tenant, this._configStore, this._logFactory) .ConfigureAwait(false))); }
/// <inheritdoc /> public void AddCopy(IPollingJobConfiguration source) { if (source == null) { throw new ArgumentNullException("source"); } RepositoryExtensions.ValidatePollingJobImplementationType(source.ImplementationType); this.queues.Add(source.ImplementationType, new RedisPollingJobConfiguration(source)); }
public async Task AddIfNew_AddsSingleById() { TestEntity entity = new TestEntity(Guid.NewGuid()); TestEntity result = await RepositoryExtensions.AddIfNewAsync(repository, entity); repository.SaveChanges(); Assert.Equal(entity, result); Assert.Equal(1, repository.FindAll <TestEntity>().Count()); Assert.Contains(repository.FindAll <TestEntity>(), x => x == entity); }
/// <inheritdoc /> public IPollingJobConfiguration this[Type implementationType] { get { RepositoryExtensions.ValidatePollingJobImplementationType(implementationType); IPollingJobConfiguration result; this.queues.TryGetValue(implementationType, out result); return(result); } }
public void Delete_ByIdsWhenRepositoryIsNull_ThrowsArgumentNullException <TKey>(TKey defaultKey) { // Arrange & Act & Assert var exception = Assert.Throws <ArgumentNullException>( () => RepositoryExtensions.Delete <IEntity <TKey>, TKey>( null, new List <TKey> { defaultKey } ) ); Assert.Equal("repository", exception.ParamName); }
public async Task AddIfNew_DoesntAddSingleDuplicateById() { TestEntity entity1 = new TestEntity(Guid.NewGuid()); repository.Add(entity1); repository.SaveChanges(); TestEntity entity2 = new TestEntity(entity1.Id); TestEntity result = await RepositoryExtensions.AddIfNewAsync(repository, entity2); repository.SaveChanges(); Assert.Equal(entity1, result); Assert.Equal(1, repository.FindAll <TestEntity>().Count()); Assert.Contains(repository.FindAll <TestEntity>(), x => x == entity1); }
/// <summary> /// Initializes a new instance of the CollectionViewModelBase class. /// </summary> /// <param name="unitOfWorkFactory">A factory used to create a unit of work instance.</param> /// <param name="getRepositoryFunc">A function that returns a repository representing entities of the given type.</param> /// <param name="projection">A LINQ function used to customize a query for entities. The parameter, for example, can be used for sorting data and/or for projecting data to a custom type that does not match the repository entity type.</param> /// <param name="newEntityInitializer">A function to initialize a new entity. This parameter is used in the detail collection view models when creating a single object view model for a new entity.</param> /// <param name="canCreateNewEntity">A function that is called before an attempt to create a new entity is made. This parameter is used together with the newEntityInitializer parameter.</param> /// <param name="ignoreSelectEntityMessage">A parameter used to specify whether the selected entity should be managed by PeekCollectionViewModel.</param> protected CollectionViewModelBase( IUnitOfWorkFactory <TUnitOfWork> unitOfWorkFactory, Func <TUnitOfWork, IRepository <TEntity, TPrimaryKey> > getRepositoryFunc, Func <IRepositoryQuery <TEntity>, IQueryable <TProjection> > projection, Action <TEntity> newEntityInitializer, Func <bool> canCreateNewEntity, bool ignoreSelectEntityMessage ) : base(unitOfWorkFactory, getRepositoryFunc, projection) { RepositoryExtensions.VerifyProjection(CreateRepository(), projection); this.newEntityInitializer = newEntityInitializer; this.canCreateNewEntity = canCreateNewEntity; this.ignoreSelectEntityMessage = ignoreSelectEntityMessage; if (!this.IsInDesignMode()) { RegisterSelectEntityMessage(); } }
public async Task GetAsync_WhenRepositoryIsNull_ThrowsArgumentNullException <TEntity, TKey>( TKey defaultKey, TEntity entity, Expression <Func <TEntity, bool> > filterExpression) where TEntity : class, IEntity <TKey> { // Arrange & Act & Assert var exception = await Assert.ThrowsAsync <ArgumentNullException>( () => RepositoryExtensions.GetAsync <TEntity, TKey>(null, filterExpression) ); if (entity != null && entity.Id != null) { Assert.IsType(defaultKey.GetType(), entity.Id); } Assert.Equal("repository", exception.ParamName); }
protected InstantFeedbackCollectionViewModelBase( IUnitOfWorkFactory <TUnitOfWork> unitOfWorkFactory, Func <TUnitOfWork, IRepository <TEntity, TPrimaryKey> > getRepositoryFunc, Func <IRepositoryQuery <TEntity>, IQueryable <TProjection> > projection, Func <bool> canCreateNewEntity = null) { this.unitOfWorkFactory = unitOfWorkFactory; this.canCreateNewEntity = canCreateNewEntity; this.getRepositoryFunc = getRepositoryFunc; this.Projection = projection; this.helperRepository = CreateRepository(); RepositoryExtensions.VerifyProjection(helperRepository, projection); this.source = unitOfWorkFactory.CreateInstantFeedbackSource(getRepositoryFunc, Projection); this.Entities = InstantFeedbackSourceViewModel.Create(() => helperRepository.Count(), source); if (!this.IsInDesignMode()) { OnInitializeInRuntime(); } }
public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) => RepositoryExtensions.RunSafe(() => { this.log.Info("Fetching remote refs to see if there is a pull request ref"); // FIX ME: What to do when Tip is null? if (Head.Tip == null) { return; } var headTipSha = Head.Tip.Sha; var remote = RepositoryInstance.Network.Remotes.Single(); var reference = GetPullRequestReference(auth, remote, headTipSha); var canonicalName = reference.CanonicalName; var referenceName = ReferenceName.Parse(reference.CanonicalName); this.log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'."); if (referenceName.IsTag) { this.log.Info($"Checking out tag '{canonicalName}'"); Checkout(reference.Target.Sha); } else if (referenceName.IsPullRequest) { var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/"); this.log.Info($"Creating fake local branch '{fakeBranchName}'."); Refs.Add(fakeBranchName, headTipSha); this.log.Info($"Checking local branch '{fakeBranchName}' out."); Checkout(fakeBranchName); } else { var message = $"Remote tip '{canonicalName}' from remote '{remote.Url}' doesn't look like a valid pull request."; throw new WarningException(message); } });
public Branch CreateBranch(string branchName) => RepositoryExtensions.CreateBranch(repository, branchName);
/// <summary> /// Creates an Linq.Expression for the Searchable properties /// </summary> /// <param name="searchCriteria">The search string</param> /// <param name="genericType">The ParameterExpression that represents the Entity</param> /// <returns>Expression</returns> public Expression GetSearchRestrictions(object searchCriteria, ParameterExpression genericType) { Expression restrictions = null; if (searchCriteria != null) { foreach (var sc in typeof(TEntity).GetProperties()) { if (sc.IsDefined(typeof(SearchAbleAttribute), true)) { var searchAtts = (SearchAbleAttribute[])sc.GetCustomAttributes(typeof(SearchAbleAttribute), true); //walk each searchable attribute on the property foreach (var att in searchAtts) { var propertyName = string.IsNullOrEmpty(att.AliasName) ? sc.Name : att.AliasName; var propertyType = typeof(TEntity).FollowPropertyPath(propertyName).PropertyType; //check for special cases where a string cannot be converted to the type specifically if (FieldCanBeSearch(propertyType, searchCriteria)) { var key = typeof(TEntity).GetPropertyExpressionFromSubProperty(propertyName, genericType); var value = Expression.Constant(RepositoryExtensions.ChangeType(searchCriteria, propertyType)); Expression addedExpression = null; switch (att.SearchType) { case SearchAbleType.Equal: addedExpression = RepositoryExtensions.NullableEqual(key, value); break; case SearchAbleType.NotEqual: addedExpression = RepositoryExtensions.NullableNotEqual(key, value); break; case SearchAbleType.GreaterThan: addedExpression = RepositoryExtensions.NullableGreaterThan(key, value); break; case SearchAbleType.GreaterThanEqual: addedExpression = RepositoryExtensions.NullableGreaterThanOrEqualTo(key, value); break; case SearchAbleType.LessThan: addedExpression = RepositoryExtensions.NullableLessThan(key, value); break; case SearchAbleType.LessThanEqual: addedExpression = RepositoryExtensions.NullableLessThanOrEqualTo(key, value); break; case SearchAbleType.Contains: var method = typeof(string).GetMethod("Contains", new[] { typeof(string) }); var someValue = Expression.Constant(searchCriteria, typeof(string)); addedExpression = Expression.Call(key, method, someValue); break; case SearchAbleType.StartsWith: var methodsw = typeof(string).GetMethod("StartsWith", new[] { typeof(string) }); var swValue = Expression.Constant(searchCriteria, typeof(string)); addedExpression = Expression.Call(key, methodsw, swValue); break; case SearchAbleType.EndsWith: var methodew = typeof(string).GetMethod("EndsWith", new[] { typeof(string) }); var ewValue = Expression.Constant(searchCriteria, typeof(string)); addedExpression = Expression.Call(key, methodew, ewValue); break; } //add the new expression to the list of restrictions restrictions = restrictions == null ? addedExpression : Expression.OrElse(restrictions, addedExpression); } } } } } return(restrictions); }
public TProjection FindLocalProjectionByKey(TPrimaryKey primaryKey) { var primaryKeyEqualsExpression = RepositoryExtensions.GetProjectionPrimaryKeyEqualsExpression <TEntity, TProjection, TPrimaryKey>(Repository, primaryKey); return(Entities.AsQueryable().FirstOrDefault(primaryKeyEqualsExpression)); }
/// <summary> /// Gets the Linq.Expression that will represent the AdvancedPaging query /// </summary> /// <param name="model">The AdvancedPageModel</param> /// <param name="genericType">The ParameterExpression that represents the Entity</param> /// <returns>The Expression for the AdvancedPage</returns> public Expression GetAdvancedSearchRestrictions(AdvancedPageModel model, ParameterExpression genericType) { Expression restrictions = null; if (model.AdvancedSearch == null) { return(restrictions); } foreach (var adv in model.AdvancedSearch) { var valueA = (object)(adv.IntValue.HasValue ? adv.IntValue.Value : adv.Value); var key = typeof(TEntity).GetPropertyExpressionFromSubProperty(adv.PropertyName, genericType); //if (key.Type == typeof(int)) //{ // key = ((MemberExpression)key).ConvertToType(TypeCode.String); //}; var propertyType = typeof(TEntity).FollowPropertyPath(adv.PropertyName).PropertyType; var value = valueA != null?Expression.Constant(RepositoryExtensions.ChangeType(valueA, propertyType)) : null; Expression addedExpression = null; switch (adv.TypeOfSearch) { case AdvancedSearchType.IsNull: addedExpression = RepositoryExtensions.NullableEqual(key, Expression.Constant(null)); break; case AdvancedSearchType.IsNotNull: addedExpression = RepositoryExtensions.NullableNotEqual(key, Expression.Constant(null)); break; case AdvancedSearchType.In: addedExpression = RepositoryExtensions.InExpression <TEntity>(genericType, adv.PropertyName, adv.ListValue); break; case AdvancedSearchType.NotIn: addedExpression = Expression.Not(RepositoryExtensions.InExpression <TEntity>(genericType, adv.PropertyName, adv.ListValue)); break; case AdvancedSearchType.Equal: addedExpression = RepositoryExtensions.NullableEqual(key, value); break; case AdvancedSearchType.NotEqual: addedExpression = RepositoryExtensions.NullableNotEqual(key, value); break; case AdvancedSearchType.LessThan: addedExpression = RepositoryExtensions.NullableLessThan(key, value); break; case AdvancedSearchType.LessThanEqual: addedExpression = RepositoryExtensions.NullableLessThanOrEqualTo(key, value); break; case AdvancedSearchType.GreaterThan: addedExpression = RepositoryExtensions.NullableGreaterThan(key, value); break; case AdvancedSearchType.GreaterThanEqual: addedExpression = RepositoryExtensions.NullableGreaterThanOrEqualTo(key, value); break; case AdvancedSearchType.Between: var lowerBound = Expression.GreaterThanOrEqual(key, Expression.Constant(Convert.ChangeType(adv.Value, propertyType))); var upperBound = Expression.LessThanOrEqual(key, Expression.Constant(Convert.ChangeType(adv.Value2, propertyType))); addedExpression = Expression.AndAlso(lowerBound, upperBound); break; case AdvancedSearchType.NotLike: addedExpression = Expression.Not(RepositoryExtensions.Contains(key, valueA)); break; case AdvancedSearchType.Like: default: addedExpression = RepositoryExtensions.Contains(key, valueA); break; } //add the new expression to the list restrictions = restrictions == null ? addedExpression : Expression.AndAlso(restrictions, addedExpression); } return(restrictions); }
public void Trying_to_mock_null_instance_should_fail_with_descriptive_error_message() { Assert.Throws <ArgumentNullException>( () => RepositoryExtensions.Expect <object>(null, x => x.ToString())); }
/// <inheritdoc /> public void Remove(Type implementationType) { RepositoryExtensions.ValidatePollingJobImplementationType(implementationType); this.queues.Remove(implementationType); }
public void Fetch(string remote, IEnumerable <string> refSpecs, AuthenticationInfo auth, string?logMessage) => RepositoryExtensions.RunSafe(() => Commands.Fetch((Repository)RepositoryInstance, remote, refSpecs, GetFetchOptions(auth), logMessage));
/// <summary> /// 转换主键类型。 /// </summary> /// <param name="value"></param> /// <returns></returns> protected virtual object ParseEntityKey(string value) { return(RepositoryExtensions.ParseEntityKey <TKey>(value)); }
protected TPrimaryKey GetProxyPrimaryKey(object threadSafeProxy) { var expression = RepositoryExtensions.GetProjectionPrimaryKeyExpression <TEntity, TProjection, TPrimaryKey>(helperRepository); return(GetProxyPropertyValue(threadSafeProxy, expression)); }
public void Checkout(string commitOrBranchSpec) => RepositoryExtensions.RunSafe(() => Commands.Checkout(RepositoryInstance, commitOrBranchSpec));