天天看點

快速開發~Rafy架構的初步認識

                         當我們開始使用EF的同時,是不是就會更好的認識了其他的ORM架構,最近接觸了Rafy的使用,感覺還是蠻有興趣去學習的,雖然最初的我到現在看的并不深入,但是我個人感覺還是可以簡單地做一些總結的啦,或許語言不多,但是更多的想總結下它的使用,之前沒接觸的友友也可以簡單地認識一下啦,嘿嘿。

             要是想使用Rafy架構,其實很簡單的,隻需要打開VS然後在工具欄中選擇擴充和更新然後搜尋Rafy.SDK安裝即可。

快速開發~Rafy架構的初步認識

                                 Rafy架構的簡單使用

                說起來使用,其實還是很簡單的入門的,當我們建立完我們的解決方案後我們就建立一個屬于自己的項目的啦,我們在添加項目時會發現如下的選項:

快速開發~Rafy架構的初步認識

                                       然後可以先選擇Rafy架構建立項目,看下實體類的建立如下:

快速開發~Rafy架構的初步認識

                                       DBIPlugin類的内容,其實是設定一個資料庫的名字:

快速開發~Rafy架構的初步認識

                                      DBIEntity類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using Rafy;
using Rafy.ComponentModel;
using Rafy.Domain;
using Rafy.Domain.ORM;
using Rafy.Domain.Validation;
using Rafy.MetaModel;
using Rafy.MetaModel.Attributes;
using Rafy.MetaModel.View;
using Rafy.ManagedProperty;

namespace DBI
{
    [Serializable]
    public abstract class DBIEntity : LongEntity
    {
        #region 構造函數

        protected DBIEntity() { }

        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
        protected DBIEntity(SerializationInfo info, StreamingContext context) : base(info, context) { }

        #endregion
    }

    [Serializable]
    public abstract class DBIEntityList : EntityList { }

    public abstract class DBIEntityRepository : EntityRepository
    {
        protected DBIEntityRepository() { }
    }

    [DataProviderFor(typeof(DBIEntityRepository))]
    public class DBIEntityRepositoryDataProvider : RdbDataProvider
    {
        protected override string ConnectionStringSettingName
        {
            get { return DBIPlugin.DbSettingName; }
        }
    }

    public abstract class DBIEntityConfig<TEntity> : EntityConfig<TEntity> { }
}      

                                        說起來DBIEntity這個類,其實是一個實體類的基類,例如我們建立一個實體類User,右擊添加選擇第一個即可,同時如下:

快速開發~Rafy架構的初步認識

                                        然後會出現一個針對這個類的選擇:

快速開發~Rafy架構的初步認識

                                       一般情況下我們選擇同時生成倉庫,當然我們也可以僅僅添加實體,之後再選擇添加倉庫也可以的啦,嘿嘿,看下User實體類:

/// <summary>
    /// 使用者
    /// </summary>
    [RootEntity, Serializable]
    public partial class User : DBIEntity
    {
        #region 構造函數

        public User() { }

        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
        protected User(SerializationInfo info, StreamingContext context) : base(info, context) { }

        #endregion

        #region 引用屬性

        #endregion

        #region 組合子屬性

        public static readonly ListProperty<UserChildList> UserChildListProperty = P<User>.RegisterList(e => e.UserChildList);
        public UserChildList UserChildList
        {
            get { return this.GetLazyList(UserChildListProperty); }
        }

        public static readonly ListProperty<PermissionList> PermissionListProperty = P<User>.RegisterList(e => e.PermissionList);
        public PermissionList PermissionList
        {
            get { return this.GetLazyList(PermissionListProperty); }
        }

        #endregion

        #region 一般屬性
        public static readonly Property<string> NameProperty = P<User>.Register(e => e.Name);
        /// <summary>
        /// 名稱
        /// </summary>
        public string Name
        {
            get { return this.GetProperty(NameProperty); }
            set { this.SetProperty(NameProperty, value); }
        }

        public static readonly Property<int> AgeProperty = P<User>.Register(e => e.Age);
        /// <summary>
        ///年齡
        /// </summary>
        public int Age
        {
            get { return this.GetProperty(AgeProperty); }
            set { this.SetProperty(AgeProperty, value); }
        }
        #endregion

        #region 隻讀屬性

        #endregion
    }

    /// <summary>
    /// 使用者 清單類。
    /// </summary>
    [Serializable]
    public partial class UserList : DBIEntityList { }

    /// <summary>
    /// 使用者 倉庫類。
    /// 負責 使用者 類的查詢、儲存。
    /// </summary>
    public partial class UserRepository : DBIEntityRepository
    {
        /// <summary>
        /// 單例模式,外界不可以直接構造本對象。
        /// </summary>
        protected UserRepository() { }


        [RepositoryQuery]
        public virtual UserList GetByName(string name,PagingInfo pagingInfo)
        {
            var q = this.CreateLinqQuery();
            q = q.Where(e => e.Name == name);
            return (UserList)this.QueryData(q, pagingInfo);
        }

        [RepositoryQuery]
        public virtual EntityList GetBySql(string name,PagingInfo pagingInf)
        {
            FormattedSql sql = @"SELECT * FROM [dbo].[User] WHERE a.NAME ={0}";
            sql.Parameters.Add(name);
            return (UserList)(this.DataQueryer as RdbDataQueryer).QueryData(sql);
        }
    }

    /// <summary>
    /// 使用者 配置類。
    /// 負責 使用者 類的實體中繼資料的配置。
    /// </summary>
    internal class UserConfig : DBIEntityConfig<User>
    {
        /// <summary>
        /// 配置實體的中繼資料
        /// </summary>
        protected override void ConfigMeta()
        {
            //配置實體的所有屬性都映射到資料表中。
            Meta.MapTable().MapAllProperties();
        }
    }
}      

                                     其實添加完就已經設定了很多自己代碼段的,在添加屬性時也友善了很多,下面簡單地寫一下常用的屬性添加的快捷鍵,如下:

                RafyProperty+Tab鍵:一般屬性的添加;

                RafyPropertyExtension+Tab鍵:擴充屬性的添加;

                RafyPropertyReadOnly+Tab鍵:隻讀屬性的添加;

                RafyPropertyReference+Tab鍵:引用屬性的添加;

                RafyPropertyReferenceNullable+Tab鍵:可空引用屬性的添加;

                我隻能簡單地寫一寫常用的啦,嘿嘿。

                上面再添加完實體類後,我們可以看到檔案DomainModel1.odml,其實他就是實體的關系圖,有了它我們實體的屬性關系已經類之間的關系就更加的清晰明了,下面簡單地說一下添加方式,像上面一樣右擊選擇添加,會出現如下頁面,選擇後添加即可:

快速開發~Rafy架構的初步認識

                                     進入到檔案中如下,選擇添加實體類,左圖是當實體類有所更新時,可以重新整理實體類的,點選添加實體類的按鈕然後出現有圖所示,選擇呈現的實體類,我們可以全部選擇也可以。

快速開發~Rafy架構的初步認識
快速開發~Rafy架構的初步認識

                                   當我們選擇後關系圖如下所示:

快速開發~Rafy架構的初步認識

                                    我這裡隻是簡單地添加了幾個實體類而已,嘿嘿,為了展示odml類檔案的圖示。

               這裡就是簡單的總結了Rafy的簡單地使用,接下來可能會寫一個小的demo的啦。

快速開發~Rafy架構的初步認識