From d60ced3e2fadc077b24a8f68beda7c6f3216aa4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 30 Dec 2016 22:46:28 +0300 Subject: [PATCH] MongoDb revision. --- .../AbpDesk/ConsoleDemo/BlogPostLister.cs | 10 +- .../AbpDesk/Blogging/BlogPost.cs | 7 ++ .../Repositories/MongoDB/MongoDbRepository.cs | 7 +- .../Volo/Abp/MongoDB/AbpMongoDbContext.cs | 1 + src/Volo.Abp/Volo/Abp/RandomHelper.cs | 97 +++++++++++++++++++ src/Volo.ExtensionMethods/Volo/Check.cs | 13 +++ 6 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 src/Volo.Abp/Volo/Abp/RandomHelper.cs diff --git a/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs b/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs index 55b0f2c56f..08b9f05e32 100644 --- a/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs +++ b/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs @@ -1,5 +1,8 @@ using System; +using System.Globalization; +using System.Linq; using AbpDesk.Blogging; +using Volo.Abp; using Volo.Abp.Domain.Repositories; using Volo.Abp.Uow; using Volo.DependencyInjection; @@ -24,7 +27,12 @@ namespace AbpDesk.ConsoleDemo using (var unitOfWork = _unitOfWorkManager.Begin()) { - //_blogPostRepository.Insert(new BlogPost("Hello World 3!", "Hello World 3......")); + //var blog = _blogPostRepository.FirstOrDefault(b => b.Title.StartsWith("Hello World 3!")); + //blog.SetTitle(blog.Title + "'"); + //blog.Comments.Add(new BlogPostComment("@john", "good post! " + DateTime.Now.ToString(CultureInfo.InvariantCulture), star: (byte)RandomHelper.GetRandom(1, 6))); + //_blogPostRepository.Update(blog); + + //_blogPostRepository.Insert(new BlogPost("Hello World 3!", DateTime.Now.ToString(CultureInfo.InvariantCulture))); foreach (var blogPost in _blogPostRepository) { diff --git a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/BlogPost.cs b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/BlogPost.cs index dc13aa207b..1980503505 100644 --- a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/BlogPost.cs +++ b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/BlogPost.cs @@ -33,6 +33,13 @@ namespace AbpDesk.Blogging Comments = new List(); } + public void SetTitle([NotNull] string title) + { + Check.NotNull(title, nameof(title)); + + Title = title; + } + public override string ToString() { return $"{base.ToString()}, Title = {Title}, Body = {Body.TruncateWithPostfix(32)}"; diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index 3f49b967bb..73e42bffce 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs @@ -39,7 +39,6 @@ namespace Volo.Abp.Domain.Repositories.MongoDB public override TEntity Insert(TEntity entity, bool autoSave = false) { - //TODO: What about assigning PK? Does mongodb handle it? Test! //TODO: Mongo has InsertMany & UpdateMany methods. Does them transactional? If so, we may consider to add them to IRepository! Collection.InsertOne(entity); @@ -48,17 +47,13 @@ namespace Volo.Abp.Domain.Repositories.MongoDB public override TEntity Update(TEntity entity) { - //TODO: How to update? TEST! - var filter = Builders.Filter.Eq(e => e.Id, entity.Id); - Collection.UpdateOne(filter, new ObjectUpdateDefinition(entity)); + Collection.ReplaceOne(filter, entity); return entity; } public override void Delete(TEntity entity) { - //TODO: How to delete? TEST! - var filter = Builders.Filter.Eq(e => e.Id, entity.Id); Collection.DeleteOne(filter); } diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs index 9cf85b703d..6225b90f3e 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs @@ -5,6 +5,7 @@ namespace Volo.Abp.MongoDB { public abstract class AbpMongoDbContext { + //TODO: Array of an EntityInfo object which contains EntityType and CollectionName private static readonly Type[] EmptyTypeList = new Type[0]; public virtual IReadOnlyList GetEntityCollectionTypes() diff --git a/src/Volo.Abp/Volo/Abp/RandomHelper.cs b/src/Volo.Abp/Volo/Abp/RandomHelper.cs new file mode 100644 index 0000000000..b843bba569 --- /dev/null +++ b/src/Volo.Abp/Volo/Abp/RandomHelper.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using JetBrains.Annotations; + +namespace Volo.Abp +{ + /// + /// A shortcut to use class. + /// Also provides some useful methods. + /// + public static class RandomHelper + { + private static readonly Random Rnd = new Random(); + + /// + /// Returns a random number within a specified range. + /// + /// The inclusive lower bound of the random number returned. + /// The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue. + /// + /// A 32-bit signed integer greater than or equal to minValue and less than maxValue; + /// that is, the range of return values includes minValue but not maxValue. + /// If minValue equals maxValue, minValue is returned. + /// + public static int GetRandom(int minValue, int maxValue) + { + lock (Rnd) + { + return Rnd.Next(minValue, maxValue); + } + } + + /// + /// Returns a nonnegative random number less than the specified maximum. + /// + /// The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero. + /// + /// A 32-bit signed integer greater than or equal to zero, and less than maxValue; + /// that is, the range of return values ordinarily includes zero but not maxValue. + /// However, if maxValue equals zero, maxValue is returned. + /// + public static int GetRandom(int maxValue) + { + lock (Rnd) + { + return Rnd.Next(maxValue); + } + } + + /// + /// Returns a nonnegative random number. + /// + /// A 32-bit signed integer greater than or equal to zero and less than . + public static int GetRandom() + { + lock (Rnd) + { + return Rnd.Next(); + } + } + + /// + /// Gets random of given objects. + /// + /// Type of the objects + /// List of object to select a random one + public static T GetRandomOf([NotNull] params T[] objs) + { + Check.NotNullOrEmpty(objs, nameof(objs)); + + return objs[GetRandom(0, objs.Length)]; + } + + /// + /// Generates a randomized list from given enumerable. + /// + /// Type of items in the list + /// items + public static List GenerateRandomizedList([NotNull] IEnumerable items) + { + Check.NotNull(items, nameof(items)); + + var currentList = new List(items); + var randomList = new List(); + + while (currentList.Any()) + { + var randomIndex = RandomHelper.GetRandom(0, currentList.Count); + randomList.Add(currentList[randomIndex]); + currentList.RemoveAt(randomIndex); + } + + return randomList; + } + } +} diff --git a/src/Volo.ExtensionMethods/Volo/Check.cs b/src/Volo.ExtensionMethods/Volo/Check.cs index b7882f6e87..e6ed115be0 100644 --- a/src/Volo.ExtensionMethods/Volo/Check.cs +++ b/src/Volo.ExtensionMethods/Volo/Check.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using JetBrains.Annotations; +using Volo.ExtensionMethods.Collections.Generic; namespace Volo { @@ -19,5 +21,16 @@ namespace Volo return value; } + + [ContractAnnotation("value:null => halt")] + public static ICollection NotNullOrEmpty(ICollection value, [InvokerParameterName] [NotNull] string parameterName) + { + if (value.IsNullOrEmpty()) + { + throw new ArgumentException(parameterName + " can not be null or empty!", parameterName); + } + + return value; + } } }