打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
打造简易的依赖注入框架(下)(此篇与Spring.NET无关,为自己手写IoC框架)
public class ObjectFactory
    {
        private IDictionary<string, object> objectDefine = new Dictionary<string, object>();
        private ObjectFactory(string fileName)
        {
            InstanceObjects(fileName);  // 实例IoC容器
            DiObjects(fileName);  // 属性注入
        }
        private static ObjectFactory instance;
        private static object lockHelper = new object();
        public static ObjectFactory Instance(string fileName)
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    instance = instance ?? new ObjectFactory(fileName);
                }
            }
            return instance;
        }
        /**//// <summary>
        /// 实例IoC容器
        /// </summary>
        /// <param name="fileName"></param>
        private void InstanceObjects(string fileName)
        {
            XElement root = XElement.Load(fileName);
            var objects = from obj in root.Elements("object")
                          select obj;
            //无参构造函数
            objectDefine = objects.Where(obj =>
                obj.Element("constructor-arg") == null).ToDictionary(
                    k => k.Attribute("id").Value, 
                    v => 
                    {
                        string typeName = v.Attribute("type").Value;  
                        Type type = Type.GetType(typeName);  
                        return Activator.CreateInstance(type);
                    }
                );
            //有参构造函数
            foreach (XElement item in objects.Where(obj => 
                obj.Element("constructor-arg") != null))
            {                                                                                                                                                  
                string id = item.Attribute("id").Value;
                string typeName = item.Attribute("type").Value;
                Type type = Type.GetType(typeName);
                var args = from property in type.GetConstructors()[0].GetParameters()
                           join el in item.Elements("constructor-arg")
                           on property.Name equals el.Attribute("name").Value
                           select Convert.ChangeType(el.Attribute("value").Value,
                           property.ParameterType);
                object obj = Activator.CreateInstance(type, args.ToArray());
                objectDefine.Add(id, obj);
            }
        }
        /**//// <summary>
        /// 属性注入
        /// </summary>
        /// <param name="fileName"></param>
        private void DiObjects(string fileName)
        {
            XElement root = XElement.Load(fileName);
            var objects = from obj in root.Elements("object")
                          select obj;
            foreach (KeyValuePair<string,object> item in objectDefine)
            {
                foreach (var el in objects.Where(e => 
                    e.Attribute("id").Value == item.Key).Elements("property"))
                {
                    Type type = item.Value.GetType();
                    //获取属性
                    foreach (PropertyInfo property in type.GetProperties())
                    {
                        if (property.Name == el.Attribute("name").Value)
                        {
                            if (el.Attribute("value") != null)
                            {
                                //设置属性值
                                property.SetValue(item.Value, 
                                    Convert.ChangeType(el.Attribute("value").Value, 
                                    property.PropertyType), null);
                            }
                            else if (el.Attribute("ref") != null)
                            {
                                object refObject = null;
                                if (objectDefine.ContainsKey(el.Attribute("ref").Value))
                                {
                                    refObject = objectDefine[el.Attribute("ref").Value];
                                }
                                //设置关联对象属性
                                property.SetValue(item.Value, refObject, null);
                            }
                        }
                    }
                }
            }
        }
        /**//// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetObject(string id)
        {
            object result = null;
            if (objectDefine.ContainsKey(id))
            {
                result = objectDefine[id];
            }
            return result;
        }
    }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
spring IoC
Method类invoke方法的使用
java的动态代理模式简单实例
java反射机制与动态代理(二)
Java - AOP编程入门--Java篇
基础加强第五天-- Java 反射
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服