寫在新工作之前的一個下雨天。HelloData 開源net Orm框架。自創,希望大家喜歡。


 HelloData 歡迎您。

hello world是我們新入軟件這行必敲得一段字母,深得人心。HelloData是小型的ORM框架,想在開源的時間立一足,必須得到大家的認可才行。

現提供各種書寫方法,大家有想法就說,文章末會提供源碼的下載。

HelloData.FrameWork:為數據庫底層框架,支持多種數據庫操作,加入了BaseEntity與BaseLogic,BaseManager兩個有關業務邏輯的繼承方式。在數據庫生成model的時候使用T4生成,
並且生成的數據庫表對應的對象類為部分類(partial),如果需要擴展加入當前對象的多個部分類即可。這樣做的好處是將數據庫生成
的類與業務間的操作分離。BaseLogic的繼承BaseLogic<T>,T為操作數據庫表對象的泛型,里面包含的常用的新增,刪除,修改,獲取一個實體,獲取實體list
,BaseManager<T, TU>,T為操作邏輯類,TU為操作邏輯對象類。繼承后當前操作邏輯類為全局唯一實例,使用了單一模式,操作方法也是包含了那些常用的邏輯操作。

在gloab.cs或者program.cs里面加入全局的操作:

 

           //啟動日志模塊,支持log4net,nlog,console
           Logger.Current.SetLogger = new ConsoleLog();
            Logger.Current.IsOpenLog = true;
            Logger.CurrentLog.Info("INSTALLING");

            //設置數據庫連接執行狀況
            AppCons.LogSqlExcu = true;
            //設置第一個數據庫
            AppCons.SetDefaultConnect(new SQLliteHelper(), ConfigurationManager.AppSettings["ConnectionString1"]);
            //設置第二個數據庫
            //AppCons.SetSecondConnect(new MySqlHelper(), ConfigurationManager.AppSettings["ConnectionString1"]);
            //設置更多個數據庫
            //AppCons.SetMoreConnect(new SQLliteHelper(), ConfigurationManager.AppSettings["ConnectionString2"]);
            //是否需要數據庫全局參數化
            AppCons.IsParmes = false;
            //是否數據庫操作的緩存
            AppCons.IsOpenCache = false;
            //使用第三方的分布式緩存
            //AppCons.CurrentCache =new  RedisCache();
            //使用內置的webcache緩存
            AppCons.CurrentCache = new WebCache();

 

新增的例子:

 1 //demo 1:
 2             cms_userManager.Instance.Save(new cms_user()
 3             {
 4                 username = "test" + DateTime.Now.Millisecond,
 5                 password = "123456",
 6                 phone = "",
 7                 isadmin = true
 8             });
 9 //demo 2:
10          using (InserAction action = new InserAction("cms_user"))
11             {
12                 action.SqlKeyValue(cms_user.Columns.username, model.username);
13                 action.SqlKeyValue(cms_user.Columns.password, model.password);
14                 return action.Excute().ReturnCode;
15             }
16 //demo 3:
17            using (InserAction action = new InserAction(model))
18             {
19                 return action.Cast<cms_user>()
20                        .SqlInValues(user =>
21                                  new List<cms_user>()
22                                     {
23                                         new cms_user {password = "12345", phone = "123456"},
24                                         new cms_user {phone = "123242343"}
25                                     }
26                      ).UnCast().Excute().ReturnCode;
27             }

 修改的例子:

//demo 1:
 using (UpdateAction update = new UpdateAction(Entity))
            {
                update.SqlKeyValue(cms_user.Columns.createtime, null);
                update.SqlKeyValue(cms_user.Columns.password, "123456123");
                update.Cast<cms_user>()
                    .SqlValue(user => new cms_user { password = "123456", createtime = DateTime.Now })
                    .Where(user1 => user1.isadmin)
                    .UnCast().Excute();
                return update.ReturnCode;
            }
//demo 2:
   cms_userManager.Instance.Update(new cms_user()
            {
               
                username = "test" + DateTime.Now.Millisecond,
                password = "123456",
                phone = "",
                isadmin = true,
                //主鍵一定要加入
                id=12
            });

 查找的例子:

//demo 1:
 using (SelectAction select = new SelectAction(Entity))
            {

                select.SqlWhere(cms_user.Columns.username, "1", "2", ConditionEnum.And, RelationEnum.Between)
                       .SqlWhere(cms_user.Columns.password, password)
                       .SqlWhere(cms_user.Columns.isactive, true)
                       .SqlOrderBy(cms_user.Columns.createtime,OrderByEnum.Desc)
                       .SqlPageParms(1);
                return select.QueryEntity<cms_user>();
            }
//demo 2:
 using (SelectAction select = new SelectAction(Entity))
            {
                select.SqlWhere(cms_user.Columns.id, id, RelationEnum.LikeRight);
                select.SqlWhere(cms_user.Columns.isactive, true);
                select.SqlPageParms(1);
                select.Cast<cms_user>().
                     OrderBy(user => user.logintime, OrderByEnum.Asc)
                    .OrderBy(ui => ui.phone, OrderByEnum.Asc)
                    .GroupBy(u => new object[] { u.isadmin, u.logintime })
                    .Where(o => o.password == "12321" && o.logintime == DateTime.Now)
                   ;// .UnCast();

                return select.QueryEntity<cms_user>();
            }

 

未完待續。

1、HelloData之:數據庫model的生成。

 
3、 Net開源HelloData之:數據庫常用操作Demo
 

下載地址:https://github.com/xiaose1205/HelloData

 

 歡迎訪問我的新站:學習樹教育的第二入口


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM