* SubClassTest.cs: chore: 完善 Subclass 实现和子类化

* LCObject.cs:
* LeanCloud.cs:
* LCCompositionalCondition.cs:
oneRain 2020-02-26 11:16:21 +08:00
parent 05047115d1
commit cbdb4e409e
4 changed files with 83 additions and 32 deletions

View File

@ -1,49 +1,100 @@
using NUnit.Framework;
using LeanCloud;
using System.Threading.Tasks;
using System.Collections.Generic;
using LeanCloud.Storage;
namespace LeanCloud.Test {
[AVClassName("Account")]
public class Account : AVObject {
[AVFieldName("name")]
public string Name {
internal class Hello : LCObject {
internal World World => this["objectValue"] as World;
internal Hello() : base("Hello") { }
}
internal class World : LCObject {
internal string Content {
get {
return GetProperty<string>("Name");
}
set {
SetProperty(value, "Name");
return this["content"] as string;
} set {
this["content"] = value;
}
}
[AVFieldName("balance")]
public int Balance {
internal World() : base("World") { }
}
internal class Account : LCObject {
internal int Balance {
get {
return GetProperty<int>("Balance");
}
set {
SetProperty(value, "Balance");
return (int)this["balance"];
} set {
this["balance"] = value;
}
}
internal Account() : base("Account") { }
}
[TestFixture]
public class SubClassTest {
[SetUp]
public void SetUp() {
Utils.InitNorthChina(true);
Logger.LogDelegate += Utils.Print;
LeanCloud.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
}
[TearDown]
public void TearDown() {
Logger.LogDelegate -= Utils.Print;
}
[Test]
public async Task SubClass() {
AVObject.RegisterSubclass<Account>();
AVQuery<Account> query = new AVQuery<Account>();
IEnumerable<Account> accounts = await query.FindAsync();
foreach (Account account in accounts) {
Assert.NotNull(account.Name);
Assert.Greater(account.Balance, 0);
TestContext.Out.WriteLine($"{account.Name}, {account.Balance}");
public async Task Create() {
LCObject.RegisterSubclass<Account>("Account", () => new Account());
Account account = new Account();
account.Balance = 1000;
await account.Save();
TestContext.WriteLine(account.ObjectId);
Assert.NotNull(account.ObjectId);
}
[Test]
public async Task Query() {
LCObject.RegisterSubclass<Account>("Account", () => new Account());
LCQuery<Account> query = new LCQuery<Account>("Account");
query.WhereGreaterThan("balance", 500);
List<Account> list = await query.Find();
TestContext.WriteLine(list.Count);
Assert.Greater(list.Count, 0);
foreach (Account account in list) {
Assert.NotNull(account.ObjectId);
}
}
[Test]
public async Task Delete() {
LCObject.RegisterSubclass<Account>("Account", () => new Account());
Account account = new Account() {
Balance = 1024
};
await account.Save();
await account.Delete();
}
[Test]
public async Task Include() {
LCObject.RegisterSubclass<Hello>("Hello", () => new Hello());
LCObject.RegisterSubclass<World>("World", () => new World());
LCQuery<Hello> helloQuery = new LCQuery<Hello>("Hello");
helloQuery.Include("objectValue");
Hello hello = await helloQuery.Get("5e0d55aedd3c13006a53cd87");
World world = hello.World;
TestContext.WriteLine(hello.ObjectId);
Assert.AreEqual(hello.ObjectId, "5e0d55aedd3c13006a53cd87");
TestContext.WriteLine(world.ObjectId);
Assert.AreEqual(world.ObjectId, "5e0d55ae21460d006a1ec931");
Assert.AreEqual(world.Content, "7788");
}
}
}

View File

@ -171,8 +171,7 @@ namespace LeanCloud.Storage.Internal.Query {
{ "limit", Limit }
};
if (conditionList != null && conditionList.Count > 0) {
// TODO json
dict["where"] = Encode();
dict["where"] = JsonConvert.SerializeObject(Encode());
}
if (orderByList != null && orderByList.Count > 0) {
dict["order"] = string.Join(",", orderByList);

View File

@ -262,10 +262,11 @@ namespace LeanCloud.Storage {
return this;
}
public static void RegisterSubclass(string className, Type type, Func<LCObject> constructor) {
LCSubclassInfo subclassInfo = new LCSubclassInfo(className, type, constructor);
public static void RegisterSubclass<T>(string className, Func<LCObject> constructor) where T : LCObject {
Type classType = typeof(T);
LCSubclassInfo subclassInfo = new LCSubclassInfo(className, classType, constructor);
subclassNameDict[className] = subclassInfo;
subclassTypeDict[type] = subclassInfo;
subclassTypeDict[classType] = subclassInfo;
}
void ApplyOperation(string key, ILCOperation op) {

View File

@ -29,9 +29,9 @@ namespace LeanCloud {
throw new ArgumentException(nameof(appKey));
}
// 注册 LeanCloud 内部子类化类型
LCObject.RegisterSubclass(LCUser.CLASS_NAME, typeof(LCUser), () => new LCUser());
LCObject.RegisterSubclass(LCRole.CLASS_NAME, typeof(LCRole), () => new LCRole());
LCObject.RegisterSubclass(LCFile.CLASS_NAME, typeof(LCFile), () => new LCFile());
LCObject.RegisterSubclass<LCUser>(LCUser.CLASS_NAME, () => new LCUser());
LCObject.RegisterSubclass<LCRole>(LCRole.CLASS_NAME, () => new LCRole());
LCObject.RegisterSubclass<LCFile>(LCFile.CLASS_NAME, () => new LCFile());
HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion);
}