由配置文件反射提供业务实体

博主从Community Server借鉴方法,用于解决业务实体由多数据库提供或代码移植问题。通过定义数据实体类、多个映射类,在配置文档填入类名,读配置文档反射提供业务实体,客户端调用。移植扩展时,只需改变配置文件type值,可轻松实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近一段时间一直在看Community Server, 从中抄了不少宝到项目里去。

场景: 业务实体由多个不同数据库提供,或是为了方便代码移植到不同数据库上去

借鉴方法: 在CS中采取了从配置文件读取映射类的方法,可以非常灵活的定义数据源。

举例步骤:  1 定义数据实体类User
            public class User:IPeople
          {
                private int _settingID;
                private string _userID;
                private string _userName;
                private string _time;
                 private string _language;
               
                属性:
            }
         
            2 定义多个映射类,对应不同数据库   

             public interface IUserProvider
            {
                  IPeople  GetUserByID(int ID);
                  IPeople  GetNameByID(int ID);
                  IPeople  GetUserByName(string name);
                  .....
            }

            public class UserProviderBySQL:DBHelper,IUserProvider
           {
                  .......
           }

           public class UserProviderByOracle:DBHelper,IUserProvider
           {
                  .......
           }

         3 在配置文档里填入类名
           <?xml version="1.0" encoding="utf-8" ?>
           <root>
           <add name="UserAppDAL" type="AppDAL.UserProviderBySQL"></add>    
          </root>
   
         4 读配置文档然后通过反射来提供业务实体
          

None.gifpublic class ConfigManager
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif           dot.gifdot.gif..
InBlock.gif
InBlock.gif  
public static object GetObjectByName(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif                   
dot.gif{
InBlock.gif                        XmlDocument d 
= new XmlDocument();
InBlock.gif                        d.Load(filename);
InBlock.gif                        
foreach (XmlNode n in d.SelectSingleNode("root").ChildNodes) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                                     
if (n.Attributes["name"].Value == name) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                                     
dot.gif{
InBlock.gif                                     typeStr 
= n.Attributes["type"].Value;
ExpandedSubBlockEnd.gif                                     }

ExpandedSubBlockEnd.gif                          }

InBlock.gif
InBlock.gif                        
if(typeStr == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                                     
throw new TestException("没有找到你要调用的类");
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        Type controlType 
= Type.GetType(typeStr,true);     //由反射得到类型
InBlock.gif
                        object obj = Activator.CreateInstance(controlType);
InBlock.gif   
InBlock.gif                         
return obj;
ExpandedSubBlockEnd.gif                 }

InBlock.gif                        
InBlock.gif                  dot.gifdot.gifdot.gif      
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

         5  客户端调用
                  

None.gifpublic class Users
ExpandedBlockStart.gifContractedBlock.gif                 
dot.gif{
InBlock.gif                        dot.gifdot.gifdot.gifdot.gif.
InBlock.gif                       
public static AppDAL.IPeople GetUserByName(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                                      AppDAL.IPeople  _user 
= null;
InBlock.gif     
InBlock.gif                                      cacheName 
= "nameKey_"+name;
InBlock.gif
InBlock.gif                                      _user 
= ExtendedCache.Get(cacheName) as AppDAL.IPeople; //先查缓存
InBlock.gif
                                      if(_user == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                     
dot.gif{
InBlock.gif                                         
//由配置文件得到映射类   
InBlock.gif
                                        IUserProvider _userProvider = 
InBlock.gif                                       ConfigManagerDAL.GetObjectByName (
"UserAppDAL")  as IUserProvider;         
InBlock.gif                                        _user 
= _userProvider.GetUserByName(name);   
InBlock.gif                                        ExtendedCache.Insert(cacheName, _user);
ExpandedSubBlockEnd.gif                                         }

InBlock.gif                                     
return _user;
InBlock.gif
ExpandedSubBlockEnd.gif                       }

InBlock.gif                       dot.gifdot.gif..
ExpandedBlockEnd.gif                 }

None.gif
None.gif

 
          
          6 移植扩展
                只需要改变配置文件中的type值即可更换映射类如
                <add name="UserAppDAL" type="AppDAL.UserProviderByOracle"></add>
                可以轻松实现扩展和移植

转载于:https://www.cnblogs.com/wull66/archive/2005/12/14/296873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值