Browse Source

MongoDb revision.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
d60ced3e2f
  1. 10
      src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs
  2. 7
      src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/BlogPost.cs
  3. 7
      src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs
  4. 1
      src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs
  5. 97
      src/Volo.Abp/Volo/Abp/RandomHelper.cs
  6. 13
      src/Volo.ExtensionMethods/Volo/Check.cs

10
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)
{

7
src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/BlogPost.cs

@ -33,6 +33,13 @@ namespace AbpDesk.Blogging
Comments = new List<BlogPostComment>();
}
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)}";

7
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<TEntity>.Filter.Eq(e => e.Id, entity.Id);
Collection.UpdateOne(filter, new ObjectUpdateDefinition<TEntity>(entity));
Collection.ReplaceOne(filter, entity);
return entity;
}
public override void Delete(TEntity entity)
{
//TODO: How to delete? TEST!
var filter = Builders<TEntity>.Filter.Eq(e => e.Id, entity.Id);
Collection.DeleteOne(filter);
}

1
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<Type> GetEntityCollectionTypes()

97
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
{
/// <summary>
/// A shortcut to use <see cref="Random"/> class.
/// Also provides some useful methods.
/// </summary>
public static class RandomHelper
{
private static readonly Random Rnd = new Random();
/// <summary>
/// Returns a random number within a specified range.
/// </summary>
/// <param name="minValue">The inclusive lower bound of the random number returned.</param>
/// <param name="maxValue">The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.</param>
/// <returns>
/// 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.
/// </returns>
public static int GetRandom(int minValue, int maxValue)
{
lock (Rnd)
{
return Rnd.Next(minValue, maxValue);
}
}
/// <summary>
/// Returns a nonnegative random number less than the specified maximum.
/// </summary>
/// <param name="maxValue">The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero.</param>
/// <returns>
/// 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.
/// </returns>
public static int GetRandom(int maxValue)
{
lock (Rnd)
{
return Rnd.Next(maxValue);
}
}
/// <summary>
/// Returns a nonnegative random number.
/// </summary>
/// <returns>A 32-bit signed integer greater than or equal to zero and less than <see cref="int.MaxValue"/>.</returns>
public static int GetRandom()
{
lock (Rnd)
{
return Rnd.Next();
}
}
/// <summary>
/// Gets random of given objects.
/// </summary>
/// <typeparam name="T">Type of the objects</typeparam>
/// <param name="objs">List of object to select a random one</param>
public static T GetRandomOf<T>([NotNull] params T[] objs)
{
Check.NotNullOrEmpty(objs, nameof(objs));
return objs[GetRandom(0, objs.Length)];
}
/// <summary>
/// Generates a randomized list from given enumerable.
/// </summary>
/// <typeparam name="T">Type of items in the list</typeparam>
/// <param name="items">items</param>
public static List<T> GenerateRandomizedList<T>([NotNull] IEnumerable<T> items)
{
Check.NotNull(items, nameof(items));
var currentList = new List<T>(items);
var randomList = new List<T>();
while (currentList.Any())
{
var randomIndex = RandomHelper.GetRandom(0, currentList.Count);
randomList.Add(currentList[randomIndex]);
currentList.RemoveAt(randomIndex);
}
return randomList;
}
}
}

13
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<T> NotNullOrEmpty<T>(ICollection<T> value, [InvokerParameterName] [NotNull] string parameterName)
{
if (value.IsNullOrEmpty())
{
throw new ArgumentException(parameterName + " can not be null or empty!", parameterName);
}
return value;
}
}
}

Loading…
Cancel
Save