Просмотр исходного кода

1.增加对象创建工厂Factory类
2.反序列化不会调用对象的构造函数,可以用BeginInit函数替代
3.抽象出不同的大类,例如World,Unit,Buff等,均继承于GameObject类,所以都拥有增删组件的功能.不同的Unit会拥有不同的组件,同理World,Buff

tanghai 11 лет назад
Родитель
Сommit
450550d4ef
31 измененных файлов с 447 добавлено и 309 удалено
  1. 4 0
      CSharp/Config/UnitConfig/1.json
  2. 0 1
      CSharp/Game/Controller/Controller.csproj
  3. 0 14
      CSharp/Game/Controller/GameObjectFactory.cs
  4. 5 5
      CSharp/Game/Model/Buff.cs
  5. 10 8
      CSharp/Game/Model/BuffComponent.cs
  6. 1 7
      CSharp/Game/Model/GameObject.cs
  7. 0 70
      CSharp/Game/Model/GameObjectManager.cs
  8. 5 1
      CSharp/Game/Model/Model.csproj
  9. 0 4
      CSharp/Game/Model/NodeConfig.cs
  10. 25 0
      CSharp/Game/Model/Unit.cs
  11. 71 0
      CSharp/Game/Model/UnitComponent.cs
  12. 14 0
      CSharp/Game/Model/UnitConfig.cs
  13. 24 0
      CSharp/Game/Model/UnitFactory.cs
  14. 9 0
      CSharp/Game/Model/UnitType.cs
  15. 7 20
      CSharp/Game/Model/World.cs
  16. 8 7
      CSharp/Game/MongoDBTest/MongoDBTest.cs
  17. 0 16
      CSharp/Platform/Common/Base/AMongo.cs
  18. 1 1
      CSharp/Platform/Common/Base/Component.cs
  19. 40 21
      CSharp/Platform/Common/Base/Entity.cs
  20. 0 11
      CSharp/Platform/Common/Base/IMongo.cs
  21. 38 21
      CSharp/Platform/Common/Base/Object.cs
  22. 5 4
      CSharp/Platform/Common/Common.csproj
  23. 2 2
      CSharp/Platform/Common/Config/AConfig.cs
  24. 82 0
      CSharp/Platform/Common/Config/ConfigComponent.cs
  25. 0 86
      CSharp/Platform/Common/Config/ConfigManager.cs
  26. 17 0
      CSharp/Platform/Common/Event/AEventAttribute.cs
  27. 2 2
      CSharp/Platform/Common/Event/EventTrigger.cs
  28. 0 8
      CSharp/Platform/Common/Event/IEventAttribute.cs
  29. 17 0
      CSharp/Platform/Common/Factory/FactoryAttribute.cs
  30. 51 0
      CSharp/Platform/Common/Factory/FactoryComponent.cs
  31. 9 0
      CSharp/Platform/Common/Factory/IFactory.cs

+ 4 - 0
CSharp/Config/UnitConfig/1.json

@@ -0,0 +1,4 @@
+{
+	"_id": 1
+	"Type" : 1 
+}

+ 0 - 1
CSharp/Game/Controller/Controller.csproj

@@ -36,7 +36,6 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="BuffController.cs" />
-    <Compile Include="GameObjectFactory.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Properties\" />

+ 0 - 14
CSharp/Game/Controller/GameObjectFactory.cs

@@ -1,14 +0,0 @@
-using Model;
-
-namespace Controller
-{
-    public class GameObjectFactory
-    {
-        public static GameObject CreatePlayer()
-        {
-            GameObject gameObject = new GameObject();
-            gameObject.AddComponent<BuffComponent>();
-            return gameObject;
-        }
-    }
-}

+ 5 - 5
CSharp/Game/Model/Buff.cs

@@ -1,16 +1,16 @@
-using Common.Base;
+using Common.Config;
 using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
-    public class Buff: AMongo
+    public class Buff: GameObject
     {
         [BsonElement]
-        private int ConfigId { get; set; }
+        private int configId { get; set; }
 
         public Buff(int configId)
         {
-            this.ConfigId = configId;
+            this.configId = configId;
         }
 
         [BsonIgnore]
@@ -18,7 +18,7 @@ namespace Model
         {
             get
             {
-                return World.Instance.ConfigManager.Get<BuffConfig>(this.ConfigId);
+                return World.Instance.GetComponent<ConfigComponent>().Get<BuffConfig>(this.configId);
             }
         }
     }

+ 10 - 8
CSharp/Game/Model/BuffComponent.cs

@@ -10,7 +10,7 @@ namespace Model
     public class BuffComponent: Component
     {
         [BsonElement]
-        private HashSet<Buff> Buffs { get; set; }
+        private HashSet<Buff> buffs { get; set; }
 
         private Dictionary<ObjectId, Buff> buffIdDict { get; set; }
 
@@ -18,14 +18,16 @@ namespace Model
 
         public BuffComponent()
         {
-            this.Buffs = new HashSet<Buff>();
+            this.buffs = new HashSet<Buff>();
             this.buffIdDict = new Dictionary<ObjectId, Buff>();
             this.buffTypeMMap = new MultiMap<BuffType, Buff>();
         }
 
         public override void EndInit()
         {
-            foreach (var buff in this.Buffs)
+            base.EndInit();
+
+            foreach (var buff in this.buffs)
             {
                 this.buffIdDict.Add(buff.Id, buff);
                 this.buffTypeMMap.Add(buff.Config.Type, buff);
@@ -34,7 +36,7 @@ namespace Model
 
         public void Add(Buff buff)
         {
-            if (this.Buffs.Contains(buff))
+            if (this.buffs.Contains(buff))
             {
                 throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
             }
@@ -44,7 +46,7 @@ namespace Model
                 throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
             }
 
-            this.Buffs.Add(buff);
+            this.buffs.Add(buff);
             this.buffIdDict.Add(buff.Id, buff);
             this.buffTypeMMap.Add(buff.Config.Type, buff);
         }
@@ -76,7 +78,7 @@ namespace Model
                 return false;
             }
 
-            this.Buffs.Remove(buff);
+            this.buffs.Remove(buff);
             this.buffIdDict.Remove(buff.Id);
             this.buffTypeMMap.Remove(buff.Config.Type, buff);
 
@@ -91,8 +93,8 @@ namespace Model
 
         public void RemoveByType(BuffType type)
         {
-            Buff[] buffs = this.GetByType(type);
-            foreach (Buff buff in buffs)
+            Buff[] allbuffs = this.GetByType(type);
+            foreach (Buff buff in allbuffs)
             {
                 this.Remove(buff);
             }

+ 1 - 7
CSharp/Game/Model/GameObject.cs

@@ -2,13 +2,7 @@
 
 namespace Model
 {
-    public enum GameObjectType
+    public abstract class GameObject: Entity
     {
-        Player = 0,
-    }
-
-    public class GameObject: Entity
-    {
-        public GameObjectType Type { get; set; }
     }
 }

+ 0 - 70
CSharp/Game/Model/GameObjectManager.cs

@@ -1,70 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using MongoDB.Bson;
-
-namespace Model
-{
-    public class GameObjectManager
-    {
-        private readonly Dictionary<ObjectId, GameObject> gameObjects =
-                new Dictionary<ObjectId, GameObject>();
-
-        private readonly Dictionary<GameObjectType, Dictionary<ObjectId, GameObject>> typeGameObjects =
-                new Dictionary<GameObjectType, Dictionary<ObjectId, GameObject>>();
-
-        public void Add(GameObject gameObject)
-        {
-            this.gameObjects.Add(gameObject.Id, gameObject);
-            if (!this.typeGameObjects.ContainsKey(gameObject.Type))
-            {
-                this.typeGameObjects.Add(gameObject.Type, new Dictionary<ObjectId, GameObject>());
-            }
-            this.typeGameObjects[gameObject.Type].Add(gameObject.Id, gameObject);
-        }
-
-        public GameObject Get(ObjectId id)
-        {
-            GameObject gameObject = null;
-            this.gameObjects.TryGetValue(id, out gameObject);
-            return gameObject;
-        }
-
-        public GameObject[] GetOneType(GameObjectType type)
-        {
-            Dictionary<ObjectId, GameObject> oneTypeGameObjects = null;
-            if (!this.typeGameObjects.TryGetValue(type, out oneTypeGameObjects))
-            {
-                return new GameObject[0];
-            }
-            return oneTypeGameObjects.Values.ToArray();
-        }
-
-        public bool Remove(GameObject gameObject)
-        {
-            if (gameObject == null)
-            {
-                throw new ArgumentNullException("gameObject");
-            }
-            if (!this.gameObjects.Remove(gameObject.Id))
-            {
-                return false;
-            }
-            if (!this.typeGameObjects[gameObject.Type].Remove(gameObject.Id))
-            {
-                return false;
-            }
-            return true;
-        }
-
-        public bool Remove(ObjectId id)
-        {
-            GameObject gameObject = this.Get(id);
-            if (gameObject == null)
-            {
-                return false;
-            }
-            return this.Remove(gameObject);
-        }
-    }
-}

+ 5 - 1
CSharp/Game/Model/Model.csproj

@@ -47,12 +47,16 @@
   <ItemGroup>
     <Compile Include="Buff.cs" />
     <Compile Include="BuffComponent.cs" />
+    <Compile Include="UnitConfig.cs" />
     <Compile Include="BuffConfig.cs" />
+    <Compile Include="UnitFactory.cs" />
+    <Compile Include="UnitType.cs" />
     <Compile Include="BuffType.cs" />
     <Compile Include="GameObject.cs" />
-    <Compile Include="GameObjectManager.cs" />
+    <Compile Include="UnitComponent.cs" />
     <Compile Include="GlobalConfig.cs" />
     <Compile Include="NodeConfig.cs" />
+    <Compile Include="Unit.cs" />
     <Compile Include="World.cs" />
   </ItemGroup>
   <ItemGroup>

+ 0 - 4
CSharp/Game/Model/NodeConfig.cs

@@ -1,15 +1,11 @@
 using System.Collections.Generic;
 using Common.Config;
-using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
     public class NodeConfig: AConfig
     {
-        [BsonIgnoreIfNull]
         public List<string> Args { get; set; }
-
-        [BsonIgnoreIfNull]
         public List<NodeConfig> SubConfigs { get; set; }
     }
 

+ 25 - 0
CSharp/Game/Model/Unit.cs

@@ -0,0 +1,25 @@
+using Common.Config;
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Model
+{
+    public class Unit : GameObject
+    {
+        [BsonElement]
+        public int configId { get; set; }
+
+        [BsonIgnore]
+        public UnitConfig Config
+        {
+            get
+            {
+                return World.Instance.GetComponent<ConfigComponent>().Get<UnitConfig>(this.configId);
+            }
+        }
+
+        public Unit(int configId)
+        {
+            this.configId = configId;
+        }
+    }
+}

+ 71 - 0
CSharp/Game/Model/UnitComponent.cs

@@ -0,0 +1,71 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Common.Base;
+using MongoDB.Bson;
+
+namespace Model
+{
+    public class UnitComponent: Component
+    {
+        private readonly Dictionary<ObjectId, Unit> units =
+                new Dictionary<ObjectId, Unit>();
+
+        private readonly Dictionary<UnitType, Dictionary<ObjectId, Unit>> typeUnits =
+                new Dictionary<UnitType, Dictionary<ObjectId, Unit>>();
+
+        public void Add(Unit unit)
+        {
+            this.units.Add(unit.Id, unit);
+            if (!this.typeUnits.ContainsKey(unit.Config.Type))
+            {
+                this.typeUnits.Add(unit.Config.Type, new Dictionary<ObjectId, Unit>());
+            }
+            this.typeUnits[unit.Config.Type].Add(unit.Id, unit);
+        }
+
+        public Unit Get(ObjectId id)
+        {
+            Unit unit = null;
+            this.units.TryGetValue(id, out unit);
+            return unit;
+        }
+
+        public Unit[] GetOneType(UnitType type)
+        {
+            Dictionary<ObjectId, Unit> oneTypeUnits = null;
+            if (!this.typeUnits.TryGetValue(type, out oneTypeUnits))
+            {
+                return new Unit[0];
+            }
+            return oneTypeUnits.Values.ToArray();
+        }
+
+        public bool Remove(Unit unit)
+        {
+            if (unit == null)
+            {
+                throw new ArgumentNullException("unit");
+            }
+            if (!this.units.Remove(unit.Id))
+            {
+                return false;
+            }
+            if (!this.typeUnits[unit.Config.Type].Remove(unit.Id))
+            {
+                return false;
+            }
+            return true;
+        }
+
+        public bool Remove(ObjectId id)
+        {
+            Unit unit = this.Get(id);
+            if (unit == null)
+            {
+                return false;
+            }
+            return this.Remove(unit);
+        }
+    }
+}

+ 14 - 0
CSharp/Game/Model/UnitConfig.cs

@@ -0,0 +1,14 @@
+using Common.Config;
+
+namespace Model
+{
+    public class UnitConfig: AConfig
+    {
+        public UnitType Type { get; set; }
+    }
+
+    [Config]
+    public class UnitCategory : ACategory<UnitConfig>
+    {
+    }
+}

+ 24 - 0
CSharp/Game/Model/UnitFactory.cs

@@ -0,0 +1,24 @@
+using Common.Base;
+using Common.Factory;
+
+namespace Model
+{
+    public static class UnitFactory
+    {
+        public static Unit Create(UnitType unitType, int configId)
+        {
+            return World.Instance.GetComponent<FactoryComponent>().Create<Unit>((int) unitType, configId);
+        }
+    }
+
+    [FactoryAttribute(typeof (Unit), (int) UnitType.Player)]
+    public class UnitPlayerFactory: IFactory
+    {
+        public Entity Create(int configId)
+        {
+            Unit player = new Unit(configId);
+            player.AddComponent<BuffComponent>();
+            return player;
+        }
+    }
+}

+ 9 - 0
CSharp/Game/Model/UnitType.cs

@@ -0,0 +1,9 @@
+namespace Model
+{
+    public enum UnitType
+    {
+        Player = 0,
+        Npc = 1,
+        Dog = 2,
+    }
+}

+ 7 - 20
CSharp/Game/Model/World.cs

@@ -1,15 +1,12 @@
 using Common.Config;
+using Common.Factory;
 
 namespace Model
 {
-    public class World
+    public class World : GameObject
     {
         private static readonly World instance = new World();
 
-        private readonly ConfigManager configManager = ConfigManager.Instance;
-
-        private readonly GameObjectManager gameObjectManager = new GameObjectManager();
-
         public static World Instance
         {
             get
@@ -20,23 +17,13 @@ namespace Model
 
         private World()
         {
-            this.configManager.Load(typeof (World).Assembly);
-        }
+            this.AddComponent<UnitComponent>();
 
-        public ConfigManager ConfigManager
-        {
-            get
-            {
-                return this.configManager;
-            }
-        }
+            ConfigComponent configComponent = this.AddComponent<ConfigComponent>();
+            configComponent.Load(new[] { typeof (World).Assembly });
 
-        public GameObjectManager GameObjectManager
-        {
-            get
-            {
-                return this.gameObjectManager;
-            }
+            FactoryComponent factoryComponent = this.AddComponent<FactoryComponent>();
+            factoryComponent.Load(new[] { typeof(World).Assembly });
         }
     }
 }

+ 8 - 7
CSharp/Game/MongoDBTest/MongoDBTest.cs

@@ -1,6 +1,6 @@
 using System;
+using Common.Factory;
 using Common.Helper;
-using Controller;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Model;
 using MongoDB.Driver;
@@ -18,17 +18,18 @@ namespace MongoDBTest
             var client = new MongoClient(connectionString);
             var server = client.GetServer();
             var database = server.GetDatabase("test");
-            var collection = database.GetCollection<GameObject>("GameObjects");
+            var collection = database.GetCollection<Unit>("Unit");
 
-            GameObject player1 = GameObjectFactory.CreatePlayer();
-            player1.GetComponent<BuffComponent>().Add(new Buff(1));
+            Unit player1 = UnitFactory.Create(UnitType.Player, 1);
+            Buff buff = new Buff(1);
+            player1.GetComponent<BuffComponent>().Add(buff);
             player1["hp"] = 10;
 
             collection.Insert(player1);
 
-            var query = Query<GameObject>.EQ(e => e.Id, player1.Id);
-            GameObject player2 = collection.FindOne(query);
-
+            var query = Query<Unit>.EQ(e => e.Id, player1.Id);
+            Unit player2 = collection.FindOne(query);
+            
             Console.WriteLine(MongoHelper.ToJson(player2));
             Assert.AreEqual(MongoHelper.ToJson(player1), MongoHelper.ToJson(player2));
         }

+ 0 - 16
CSharp/Platform/Common/Base/AMongo.cs

@@ -1,16 +0,0 @@
-namespace Common.Base
-{
-    /// <summary>
-    /// 需要序列化并且需要存储 Key Value 数据的类继承此抽象类
-    /// </summary>
-    public abstract class AMongo: Object, IMongo
-    {
-        public virtual void BeginInit()
-        {
-        }
-
-        public virtual void EndInit()
-        {
-        }
-    }
-}

+ 1 - 1
CSharp/Platform/Common/Base/Component.cs

@@ -5,7 +5,7 @@ namespace Common.Base
     /// <summary>
     /// Component的Id与Owner Entity Id一样
     /// </summary>
-    public abstract class Component : AMongo
+    public abstract class Component : Object
     {
         private Entity owner;
 

+ 40 - 21
CSharp/Platform/Common/Base/Entity.cs

@@ -5,50 +5,56 @@ using MongoDB.Bson.Serialization.Attributes;
 
 namespace Common.Base
 {
-    public abstract class Entity : AMongo
+    public abstract class Entity : Object
     {
-        [BsonElement]
-        private HashSet<Component> Components { get; set; }
+        [BsonElement, BsonIgnoreIfNull]
+        private HashSet<Component> components;
 
-        private Dictionary<Type, Component> ComponentDict { get; set; }
+        private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
 
-        protected Entity()
+        public T AddComponent<T>() where T : Component, new()
         {
-            this.Components = new HashSet<Component>();
-            this.ComponentDict = new Dictionary<Type, Component>();
-        }
-
-        public void AddComponent<T>() where T : Component, new()
-        {
-            if (this.ComponentDict.ContainsKey(typeof (T)))
+            if (this.componentDict.ContainsKey(typeof (T)))
             {
                 throw new Exception(
                     string.Format("AddComponent, component already exist, id: {0}, component: {1}", 
                     this.Id, typeof(T).Name));
             }
+
+            if (this.components == null)
+            {
+                this.components = new HashSet<Component>();
+            }
+
             T t = new T { Owner = this };
-            this.Components.Add(t);
-            this.ComponentDict.Add(typeof (T), t);
+            this.components.Add(t);
+            this.componentDict.Add(typeof (T), t);
+            return t;
         }
 
         public void RemoveComponent<T>() where T : Component
         {
             Component t;
-            if (!this.ComponentDict.TryGetValue(typeof (T), out t))
+            if (!this.componentDict.TryGetValue(typeof (T), out t))
             {
                 throw new Exception(
                     string.Format("RemoveComponent, component not exist, id: {0}, component: {1}",
                     this.Id, typeof(T).Name));
             }
             
-            this.Components.Remove(t);
-            this.ComponentDict.Remove(typeof(T));
+            this.components.Remove(t);
+            this.componentDict.Remove(typeof(T));
+
+            if (this.components.Count == 0)
+            {
+                this.components = null;
+            }
         }
 
         public T GetComponent<T>() where T : Component
         {
             Component t;
-            if (!this.ComponentDict.TryGetValue(typeof (T), out t))
+            if (!this.componentDict.TryGetValue(typeof (T), out t))
             {
                 throw new Exception(
                     string.Format("GetComponent, component not exist, id: {0}, component: {1}",
@@ -59,15 +65,28 @@ namespace Common.Base
 
         public Component[] GetComponents()
         {
-            return this.Components.ToArray();
+            return this.components.ToArray();
+        }
+
+        public override void BeginInit()
+        {
+            base.BeginInit();
+            this.components = new HashSet<Component>();
+            this.componentDict = new Dictionary<Type, Component>();
         }
 
         public override void EndInit()
         {
-            foreach (Component component in this.Components)
+            base.EndInit();
+            if (this.components.Count == 0)
+            {
+                this.components = null;
+                return;
+            }
+            foreach (Component component in this.components)
             {
                 component.Owner = this;
-                this.ComponentDict.Add(component.GetType(), component);
+                this.componentDict.Add(component.GetType(), component);
             }
         }
     }

+ 0 - 11
CSharp/Platform/Common/Base/IMongo.cs

@@ -1,11 +0,0 @@
-using System.ComponentModel;
-
-namespace Common.Base
-{
-    /// <summary>
-    /// 需要序列化的成员继承此接口
-    /// </summary>
-    public interface IMongo: ISupportInitialize
-    {
-    }
-}

+ 38 - 21
CSharp/Platform/Common/Base/Object.cs

@@ -1,17 +1,18 @@
 using System.Collections.Generic;
+using System.ComponentModel;
 using MongoDB.Bson;
 using MongoDB.Bson.Serialization.Attributes;
 
 namespace Common.Base
 {
-    public abstract class Object
+    public abstract class Object: ISupportInitialize
     {
         [BsonId]
         public ObjectId Id { get; protected set; }
 
         [BsonElement]
         [BsonIgnoreIfNull]
-        private Dictionary<string, object> Values;
+        private Dictionary<string, object> values;
 
         protected Object()
         {
@@ -25,27 +26,27 @@ namespace Common.Base
 
         public object this[string key]
         {
+            get
+            {
+                return this.values[key];
+            }
             set
             {
-                if (this.Values == null)
+                if (this.values == null)
                 {
-                    this.Values = new Dictionary<string, object>();
+                    this.values = new Dictionary<string, object>();
                 }
-                this.Values[key] = value;
-            }
-            get
-            {
-                return this.Values[key];
+                this.values[key] = value;
             }
         }
 
         public T Get<T>(string key)
         {
-            if (!this.Values.ContainsKey(key))
+            if (!this.values.ContainsKey(key))
             {
                 return default(T);
             }
-            return (T) this.Values[key];
+            return (T) this.values[key];
         }
 
         public T Get<T>()
@@ -55,35 +56,51 @@ namespace Common.Base
 
         public void Set(string key, object obj)
         {
-            if (this.Values == null)
+            if (this.values == null)
             {
-                this.Values = new Dictionary<string, object>();
+                this.values = new Dictionary<string, object>();
             }
-            this.Values[key] = obj;
+            this.values[key] = obj;
         }
 
         public void Set<T>(T obj)
         {
-            if (this.Values == null)
+            if (this.values == null)
             {
-                this.Values = new Dictionary<string, object>();
+                this.values = new Dictionary<string, object>();
             }
-            this.Values[typeof (T).Name] = obj;
+            this.values[typeof (T).Name] = obj;
         }
 
         public bool Contain(string key)
         {
-            return this.Values.ContainsKey(key);
+            return this.values.ContainsKey(key);
         }
 
         public bool Remove(string key)
         {
-            bool ret = this.Values.Remove(key);
-            if (this.Values.Count == 0)
+            bool ret = this.values.Remove(key);
+            if (this.values.Count == 0)
             {
-                this.Values = null;
+                this.values = null;
             }
             return ret;
         }
+
+        public virtual void BeginInit()
+        {
+            if (this.values == null)
+            {
+                this.values = new Dictionary<string, object>();
+            }
+        }
+
+        public virtual void EndInit()
+        {
+            if (this.values.Count == 0)
+            {
+                this.values = null;
+            }
+        }
     }
 }

+ 5 - 4
CSharp/Platform/Common/Common.csproj

@@ -58,21 +58,22 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Base\IMongo.cs" />
     <Compile Include="Base\Entity.cs" />
     <Compile Include="Base\Component.cs" />
     <Compile Include="Base\MultiMap.cs" />
-    <Compile Include="Base\AMongo.cs" />
     <Compile Include="Config\ICategory.cs" />
     <Compile Include="Event\EventTrigger.cs" />
     <Compile Include="Event\Env.cs" />
-    <Compile Include="Event\IEventAttribute.cs" />
+    <Compile Include="Event\AEventAttribute.cs" />
     <Compile Include="Event\IEvent.cs" />
     <Compile Include="Base\Object.cs" />
     <Compile Include="Config\ConfigAttribute.cs" />
     <Compile Include="Config\ACategory.cs" />
-    <Compile Include="Config\ConfigManager.cs" />
+    <Compile Include="Config\ConfigComponent.cs" />
     <Compile Include="Config\AConfig.cs" />
+    <Compile Include="Factory\FactoryComponent.cs" />
+    <Compile Include="Factory\IFactory.cs" />
+    <Compile Include="Factory\FactoryAttribute.cs" />
     <Compile Include="Helper\BigIntegerHelper.cs" />
     <Compile Include="Helper\ByteHelper.cs" />
     <Compile Include="Helper\EnumHelper.cs" />

+ 2 - 2
CSharp/Platform/Common/Config/AConfig.cs

@@ -1,9 +1,9 @@
-using Common.Base;
+using System.ComponentModel;
 using MongoDB.Bson.Serialization.Attributes;
 
 namespace Common.Config
 {
-    public abstract class AConfig : IMongo
+    public abstract class AConfig : ISupportInitialize
     {
         [BsonId]
         public int Id { get; set; }

+ 82 - 0
CSharp/Platform/Common/Config/ConfigComponent.cs

@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using Common.Base;
+using Common.Logger;
+
+namespace Common.Config
+{
+    public class ConfigComponent: Component
+    {
+        public Dictionary<Type, ICategory> allConfig;
+
+        private Assembly[] assemblies;
+
+        public void Load(Assembly[] ass)
+        {
+            allConfig = new Dictionary<Type, ICategory>();
+            this.assemblies = ass;
+            foreach (Assembly assembly in assemblies)
+            {
+                Type[] types = assembly.GetTypes();
+                foreach (Type type in types)
+                {
+                    object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
+                    if (attrs.Length == 0)
+                    {
+                        continue;
+                    }
+
+                    object obj = (Activator.CreateInstance(type));
+
+                    ICategory iCategory = obj as ICategory;
+                    if (iCategory == null)
+                    {
+                        throw new Exception(
+                            string.Format("class: {0} not inherit from ACategory", type.Name));
+                    }
+                    iCategory.BeginInit();
+                    iCategory.EndInit();
+
+                    allConfig[iCategory.ConfigType] = iCategory;
+                }
+            }
+        }
+
+        public void Reload()
+        {
+            this.Load(this.assemblies);
+        }
+
+        public T Get<T>(int id) where T : AConfig
+        {
+            Type type = typeof (T);
+            ICategory configCategory;
+            if (!this.allConfig.TryGetValue(type, out configCategory))
+            {
+                throw new KeyNotFoundException(string.Format("ConfigComponent not found key: {0}", type.FullName));
+            }
+            return ((ACategory<T>) configCategory)[id];
+        }
+
+        public T[] GetAll<T>() where T : AConfig
+        {
+            Type type = typeof(T);
+            ICategory configCategory;
+            if (!this.allConfig.TryGetValue(type, out configCategory))
+            {
+                throw new KeyNotFoundException(string.Format("ConfigComponent not found key: {0}", type.FullName));
+            }
+            return ((ACategory<T>)configCategory).GetAll();
+        }
+
+        public T GetCategory<T>() where T : class, ICategory, new()
+        {
+            T t = new T();
+            Type type = t.ConfigType;
+            ICategory category;
+            bool ret = this.allConfig.TryGetValue(type, out category);
+            return ret? (T) category : null;
+        }
+    }
+}

+ 0 - 86
CSharp/Platform/Common/Config/ConfigManager.cs

@@ -1,86 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Reflection;
-using Common.Logger;
-
-namespace Common.Config
-{
-    public class ConfigManager
-    {
-        private static ConfigManager instance = new ConfigManager();
-
-        public static ConfigManager Instance
-        {
-            get
-            {
-                return instance;
-            }
-        }
-
-        public Dictionary<Type, ICategory> allConfig;
-
-        private ConfigManager()
-        {
-        }
-
-        public void Load(Assembly assembly)
-        {
-            var localAllConfig = new Dictionary<Type, ICategory>();
-            Type[] types = assembly.GetTypes();
-            foreach (Type type in types)
-            {
-                object[] attrs = type.GetCustomAttributes(typeof (ConfigAttribute), false);
-                Log.Debug(type.Name);
-                if (attrs.Length == 0)
-                {
-                    continue;
-                }
-
-                object obj = (Activator.CreateInstance(type));
-
-                ICategory iCategory = obj as ICategory;
-                if (iCategory == null)
-                {
-                    throw new Exception(
-                        string.Format("class: {0} not inherit from ACategory", type.Name));
-                }
-                iCategory.BeginInit();
-                iCategory.EndInit();
-
-                localAllConfig[iCategory.ConfigType] = iCategory;
-            }
-            this.allConfig = localAllConfig;
-        }
-
-        public T Get<T>(int id) where T : AConfig
-        {
-            Type type = typeof (T);
-            ICategory configCategory;
-            if (!this.allConfig.TryGetValue(type, out configCategory))
-            {
-                throw new KeyNotFoundException(string.Format("ConfigManager not found key: {0}", type.FullName));
-            }
-            return ((ACategory<T>) configCategory)[id];
-        }
-
-        public T[] GetAll<T>() where T : AConfig
-        {
-            Type type = typeof(T);
-            ICategory configCategory;
-            if (!this.allConfig.TryGetValue(type, out configCategory))
-            {
-                throw new KeyNotFoundException(string.Format("ConfigManager not found key: {0}", type.FullName));
-            }
-            return ((ACategory<T>)configCategory).GetAll();
-        }
-
-        public T GetCategory<T>() where T : class, ICategory, new()
-        {
-            T t = new T();
-            Type type = t.ConfigType;
-            ICategory category;
-            bool ret = this.allConfig.TryGetValue(type, out category);
-            return ret? (T) category : null;
-        }
-    }
-}

+ 17 - 0
CSharp/Platform/Common/Event/AEventAttribute.cs

@@ -0,0 +1,17 @@
+using System;
+
+namespace Common.Event
+{
+    [AttributeUsage(AttributeTargets.Class)]
+    public abstract class AEventAttribute : Attribute
+    {
+        public int Type { get; private set; }
+        public int Order { get; private set; }
+
+        protected AEventAttribute(int type, int order)
+        {
+            this.Type = type;
+            this.Order = order;
+        }
+    }
+}

+ 2 - 2
CSharp/Platform/Common/Event/EventTrigger.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 
 namespace Common.Event
 {
-    public class EventTrigger<T> where T : IEventAttribute
+    public class EventTrigger<T> where T : AEventAttribute
     {
         private readonly Dictionary<int, SortedDictionary<int, IEvent>> events;
 
@@ -28,7 +28,7 @@ namespace Common.Event
                             obj.GetType().FullName));
                 }
 
-                IEventAttribute iEventAttribute = (T) attrs[0];
+                AEventAttribute iEventAttribute = (T) attrs[0];
 
                 if (!this.events.ContainsKey(iEventAttribute.Type))
                 {

+ 0 - 8
CSharp/Platform/Common/Event/IEventAttribute.cs

@@ -1,8 +0,0 @@
-namespace Common.Event
-{
-    public class IEventAttribute
-    {
-        public int Type { get; set; }
-        public int Order { get; set; }
-    }
-}

+ 17 - 0
CSharp/Platform/Common/Factory/FactoryAttribute.cs

@@ -0,0 +1,17 @@
+using System;
+
+namespace Common.Factory
+{
+    [AttributeUsage(AttributeTargets.Class)]
+    public class FactoryAttribute : Attribute
+    {
+        public Type ClassType { get; private set; }
+        public int Type { get; private set; }
+
+        public FactoryAttribute(Type classType, int type)
+        {
+            this.ClassType = classType;
+            this.Type = type;
+        }
+    }
+}

+ 51 - 0
CSharp/Platform/Common/Factory/FactoryComponent.cs

@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using Common.Base;
+
+namespace Common.Factory
+{
+    public class FactoryComponent: Component
+    {
+        private Dictionary<Type, Dictionary<int, IFactory>> allConfig;
+
+        public void Load(IEnumerable<Assembly> assemblies)
+        {
+            allConfig = new Dictionary<Type, Dictionary<int, IFactory>>();
+            foreach (Assembly assembly in assemblies)
+            {
+                Type[] types = assembly.GetTypes();
+                foreach (Type type in types)
+                {
+                    object[] attrs = type.GetCustomAttributes(typeof(FactoryAttribute), false);
+                    if (attrs.Length == 0)
+                    {
+                        continue;
+                    }
+
+                    FactoryAttribute attribute = (FactoryAttribute)attrs[0];
+                    object obj = (Activator.CreateInstance(type));
+
+                    IFactory iFactory = obj as IFactory;
+                    if (iFactory == null)
+                    {
+                        throw new Exception(
+                            string.Format("class: {0} not inherit from IFactory", type.Name));
+                    }
+
+                    if (!allConfig.ContainsKey(attribute.ClassType))
+                    {
+                        allConfig[attribute.ClassType] = new Dictionary<int, IFactory>();
+                    }
+
+                    allConfig[attribute.ClassType][attribute.Type] = iFactory;
+                }
+            }
+        }
+
+        public T Create<T>(int type, int configId) where T: Entity
+        {
+            return (T) this.allConfig[typeof(T)][type].Create(configId);
+        }
+    }
+}

+ 9 - 0
CSharp/Platform/Common/Factory/IFactory.cs

@@ -0,0 +1,9 @@
+using Common.Base;
+
+namespace Common.Factory
+{
+    public interface IFactory
+    {
+        Entity Create(int configId);
+    }
+}