初學必看,NFine框架結構加MVC快速開發平臺登錄流程梳理(附源碼)

就在上週,我們公司的大佬讓我們熟悉一下NFine框架,參考NFine開發平臺學習,於是按照步驟一步步的在電腦上部署平臺環境,發佈測試。

首先是映入眼中的是登錄界面

輸入賬號密碼,登錄

界面還是挺友善的,舒適好看,隨便點了一點發現功能幾乎都沒有實現。。

接下來當然是打開項目代碼,查看項目目錄結構了,如下圖:

先給大家講一下主要的目錄結構,還有對應的功能作用,如果大家以前學過java的一些框架像SSH,SSM或者瞭解MVC設計模式都很容易理解並且上手操作的:

 

01Common 基礎結構層

包含NFine.Code(底層核心類)和NFine.Data(數據層),這些都是固定的,我們不需要修改,比如NFine,Data下面的這些接口IRepositoryBase.cs等,我們可以聲明倉庫接口去繼承這些接口,然後用對應類去實現,通過生成類對象進行方法調用,接下來會提。

展示一下IRepositoryBase接口裏面的內容:

public interface IRepositoryBase : IDisposable
{
        IRepositoryBase BeginTrans();
        int Commit();
        int Insert<TEntity>(TEntity entity) where TEntity : class, new();
        int Insert<TEntity>(List<TEntity> entitys) where TEntity : class, new();
        int Update<TEntity>(TEntity entity) where TEntity : class, new();
        int Delete<TEntity>(TEntity entity) where TEntity : class, new();
        int Delete<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class, new();
        TEntity FindEntity<TEntity>(object keyValue) where TEntity : class, new();
        TEntity FindEntity<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class, new();
        IQueryable<TEntity> IQueryable<TEntity>() where TEntity : class, new();
        IQueryable<TEntity> IQueryable<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class, new();
        List<TEntity> FindList<TEntity>(string strSql) where TEntity : class, new();
        List<TEntity> FindList<TEntity>(string strSql, DbParameter[] dbParameter) where TEntity : class, new();
        List<TEntity> FindList<TEntity>(Pagination pagination) where TEntity : class, new();
        List<TEntity> FindList<TEntity>(Expression<Func<TEntity, bool>> predicate, Pagination pagination) where TEntity : class, new();
}

03 Domain應用服務

1、NFine.Domain中的03 Entity 裏面都是一些bean,類似於javabean,包含表單bean,數據bean,結果bean等

比如我們待會會涉及到的UserEntity

 public class UserEntity : IEntity<UserEntity>, ICreationAudited, IDeleteAudited, IModificationAudited
{
        [DatabaseAttribute("varchar(50)", true, true, false, "", "主鍵")]
        public string F_Id { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "賬戶")]
        public string F_Account { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "姓名")]
        public string F_RealName { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "呢稱")]
        public string F_NickName { get; set; }
        [DatabaseAttribute("text", false, false, false, null, "頭像")]
        public string F_HeadIcon { get; set; }
        [DatabaseAttribute("tinyint", false, false, false, null, "性別")]
        public bool? F_Gender { get; set; }
        [DatabaseAttribute("datetime", false, false, false, null, "生日")]
        public DateTime? F_Birthday { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "手機")]
        public string F_MobilePhone { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "郵箱")]
        public string F_Email { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "微信")]
        public string F_WeChat { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "主管主鍵")]
        public string F_ManagerId { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "安全級別")]
        public int? F_SecurityLevel { get; set; }
        [DatabaseAttribute("text", false, false, false, null, "個性簽名")]
        public string F_Signature { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "組織主鍵")]
        public string F_OrganizeId { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "部門主鍵")]
        public string F_DepartmentId { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "角色主鍵")]
        public string F_RoleId { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "崗位主鍵")]
        public string F_DutyId { get; set; }
        [DatabaseAttribute("tinyint", false, false, false, null, "是否管理員")]
        public bool? F_IsAdministrator { get; set; }
        [DatabaseAttribute("int(4)", false, false, false, null, "排序碼")]
        public int? F_SortCode { get; set; }
        [DatabaseAttribute("tinyint", false, false, false, null, "刪除標誌")]
        public bool? F_DeleteMark { get; set; }
        [DatabaseAttribute("tinyint", false, false, false, null, "有效標誌")]
        public bool? F_EnabledMark { get; set; }
        [DatabaseAttribute("varchar(500)", false, false, false, null, "描述")]
        public string F_Description { get; set; }
        [DatabaseAttribute("datetime", false, false, false, null, "創建日期")]
        public DateTime? F_CreatorTime { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "創建用戶主鍵")]
        public string F_CreatorUserId { get; set; }
        [DatabaseAttribute("datetime", false, false, false, null, "最後修改時間")]
        public DateTime? F_LastModifyTime { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "最後修改用戶")]
        public string F_LastModifyUserId { get; set; }
        [DatabaseAttribute("datetime", false, false, false, null, "刪除時間")]
        public DateTime? F_DeleteTime { get; set; }
        [DatabaseAttribute("varchar(50)", false, false, false, null, "刪除用戶")]
        public string F_DeleteUserId { get; set; }
}

2、NFine.Mapping則是添加映射,將我們的每個Entity與它對應的表關聯並指明主鍵

如UserEntity對應的UserMap

public class UserMap : EntityTypeConfiguration<UserEntity>
{
   public UserMap()
   {
       this.ToTable("Sys_User");
       this.HasKey(t => t.F_Id);
   }
}

Sys_User就是對應的表,而F_Id就是主鍵

 

3、NFine.Domain裏的04 IRepository則是倉庫接口的聲明(上面講NFine.Data時提到過),這些接口都會繼承對應NFine.Data的IRepositoryBase接口

依舊看一下對應的IUserRepository

public interface IUserRepository : IRepositoryBase<UserEntity>
{
        void DeleteForm(string keyValue);
        void SubmitForm(UserEntity userEntity, UserLogOnEntity userLogOnEntity, string keyValue);
}

有了倉庫接口聲明,肯定要有對應的實現類,這樣我們就可以直接使用接口裏定義的方法了

4、NFine.Repository就是定義對應的實現類

我們也來看看UserRepository裏面長啥樣

public class UserRepository : MySqlRepositoryBase<UserEntity>, IUserRepository
{
        public void DeleteForm(string keyValue)
        {
            ...
        }
        public void SubmitForm(UserEntity userEntity, UserLogOnEntity userLogOnEntity, string keyValue)
        {
            ...
        }
}

5、NFine.Application這個就是業務邏輯層了,所有業務邏輯都是在這裏編寫

比如我們接下來要講的登錄功能的業務邏輯就是在這裏編碼實現的,我們來看看:

由於代碼有點多,我就截圖好了,CheckLogin這個方法就是寫的登錄邏輯

 

04 Web應用程序

毫無疑問這裏就是視圖層和控制層兩者結合了

先來看看控制層,和Spring MVC的名命方式一樣,叫做Controller

比如登錄的控制器就是LoginController

視圖層就是放在views下面了,也就是我們的一些html

 

好了,大概的結構就是這樣,我們現在來講一下它的登錄功能,把整個流程跑一下,你或許可以進一步理解它的工作流程

 

第一步當然是看首頁了

1、採用點擊登錄按鈕,ajax()方法觸發調用控制器

2、執行LoginController的CheckLogin()方法

3、調用業務邏輯層裏的UserApp類的CheckLogin()方法,我們可以看到它聲明瞭倉庫接口的對象,並將對應實現的對象賦值。

這時就可以調用我們Data數據層的方法了

4、調用數據層的方法,通過用戶名查詢到對應的UserEntity,然後用賬戶獲取了用戶的日誌信息Entity,驗證密鑰,相等的話就更新這個Entity,將用戶最新登錄信息日誌更新,然後返回UserEntity(相當於結果bean)給我們的業務層。

5、業務層看返回的UserEntity是否爲null,是的話就說明登錄失敗,不爲null,則登錄成功,然後做一些初始化操作,發一個轉向信息給視圖層,轉到對應的頁面。

 

講了這麼多,希望對剛學習NFine的你們有所幫助,有問題可以留言,我最遲會在一天內幫你們解答。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章