蜘蛛池出租蜘蛛池出租

蜘蛛池網(wǎng)站收錄技術(shù)

湖北seo黑帽優(yōu)化方法:開箱即用簡單便捷的輕量級開源開發(fā)框架_黑帽SEO排名

:細(xì)談Redis五大數(shù)據(jù)類型

  你是不是羨慕Java SpringBoot里功能強(qiáng)大的@注解功能,Spring Boot倡導(dǎo)是一種開箱即用、方便快捷、約定優(yōu)于配置的開發(fā)流程,雖然現(xiàn)在.NET Core也往相同的方向走,但在使用上總有點(diǎn)別扭,目前市面上貌似還沒有輕量級的真正意義上的開箱即用的基于.NET Core的框架。

  想想多年前自己開發(fā)基于配置的DevFx開發(fā)框架,因為需要配置,造成開發(fā)人員苦不堪言,而且還容易配置錯誤,導(dǎo)致各種奇怪的錯誤;于是便有全新重寫DevFx框架的想法,經(jīng)過N個月的奮戰(zhàn),終于可以放出來用了。

  框架不求功能全面,只求使用方便、靈活。

  目前框架提供基于Attribute的IoC DI容器,完全可以面向接口編程了;提供輕量級的業(yè)務(wù)參數(shù)配置方案,未來計劃作為集中配置的基礎(chǔ);提供極簡但不失靈活的數(shù)據(jù)訪問框架,類似mybatis基于sql的數(shù)據(jù)訪問;還有基于HTTP/JSON的遠(yuǎn)程調(diào)用方案(以優(yōu)雅的本地調(diào)用方式來遠(yuǎn)程調(diào)用);主要是以上幾個功能。

  框架是基于.NET Standard 2.0開發(fā),理論上.NET Framework 4.6.1也能使用,因為框架已完全重新重寫了,命名空間啥的都有改變,所以不兼容之前的版本,目前版本是5.0.2。

  OK,show me the code。下面讓我們來快速入門,看看怎么個開箱即用。

 

打開VS2019,建立基于.NET Core 2.2或3.0的控制臺項目ConsoleApp1,下面的例子是基于.NET Core 3.0的。使用NuGet安裝DevFx 5.0.2版本

 

 上圖,忽略DevFx.*,這是老舊版本,目前基于.NET Standard只有一個包,就是DevFx

創(chuàng)建業(yè)務(wù)邏輯接口和實現(xiàn)類

using DevFx;

namespace ConsoleApp1
{
    //業(yè)務(wù)邏輯接口,[Service]特性告訴DevFx這個接口需要被DI
    [Service]
    public interface IMyService
    {
        string GetUserName(string userId);
    }
}
using DevFx;
using System;

namespace ConsoleApp1
{
    //業(yè)務(wù)邏輯實現(xiàn)類,[Object]特性告訴DevFx這個類需要放入到IoC容器里,DevFx會掃描這個類實現(xiàn)了哪些接口,并做映射
    [Object]
    internal class MyService : IMyService
    {
        public string GetUserName(string userId) {
            return $"{userId}_{DateTime.Now.Ticks}";
        }
    }
}

開始調(diào)用邏輯

using DevFx;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args) {
            //控制臺程序需要顯式調(diào)用框架的初始化方法
            //ASP.NET Core(通用主機(jī))可以使用UseDevFx擴(kuò)展方法來初始化框架
            ObjectService.Init();
            //獲取接口實現(xiàn)類的實例
            var myservice = ObjectService.GetObject<IMyService>();
            Console.WriteLine(myservice.GetUserName("IamDevFx"));
            //還能直接獲取MyService類的實例
            var myservice1 = ObjectService.GetObject<MyService>();
            //2種方式獲取的實例是同一個
            Console.WriteLine($" myservice={myservice.GetHashCode()}{Environment.NewLine}myservice1={myservice1.GetHashCode()}");
        }
    }
}

運(yùn)行下:

 

 是不是很簡單?開箱即用!

 

接下介紹下自動裝配的例子

我們建立另外一個業(yè)務(wù)邏輯接口和相應(yīng)的實現(xiàn)類,同樣分別標(biāo)上[Service]和[Object]

using DevFx;

namespace ConsoleApp1
{
    [Service]
    public interface IBizService
    {
        string GetUserDisplayName(string userId);
    }

    [Object]
    internal class BizService : IBizService
    {
        public string GetUserDisplayName(string userId) {
            return "IamBizService";
        }
    }
}

改下之前的業(yè)務(wù)類MyService

using DevFx;
using System;

namespace ConsoleApp1
{
    //業(yè)務(wù)邏輯實現(xiàn)類,[Object]特性告訴DevFx這個類需要放入到IoC容器里,DevFx會掃描這個類實現(xiàn)了哪些接口,并做映射
    [Object]
    internal class MyService : IMyService
    {
        //自動裝配(注入)
        [Autowired]
        protected IBizService BizService { get; set; }

        public string GetUserName(string userId) {
            return $"{userId}_{DateTime.Now.Ticks}_{this.BizService.GetUserDisplayName(userId)}";
        }
    }
}

運(yùn)行下:

 

接下來介紹下基于xml的配置,可能有些同學(xué)會問,.NET Core不是自帶配置了么?別急,看下我們的使用方式你就清楚誰便捷了。

業(yè)務(wù)參數(shù)指的比如微信的API接口地址、APPID等程序里需要使用的,或者一些開關(guān)之類的參數(shù)

首先定義需要承載業(yè)務(wù)參數(shù)的接口

using DevFx.Configuration;

namespace ConsoleApp1
{
    //定義需要承載業(yè)務(wù)參數(shù)的接口,[SettingObject("~/myservice/weixin")]告訴框架這是一個配置承載對象
    //    其中~/myservice/weixin為配置在配置文件里的路徑
    [SettingObject("~/myservice/weixin")]
    public interface IWeixinSetting
    {
        string ApiUrl { get; }
        string AppID { get; }
        string AppKey { get; }
    }
}

使用自動裝配特性,裝配到業(yè)務(wù)邏輯里,我們修改下MyService類

using DevFx;
using System;

namespace ConsoleApp1
{
    //業(yè)務(wù)邏輯實現(xiàn)類,[Object]特性告訴DevFx這個類需要放入到IoC容器里,DevFx會掃描這個類實現(xiàn)了哪些接口,并做映射
    [Object]
    internal class MyService : IMyService
    {
        //自動裝配(注入)
        [Autowired]
        protected IBizService BizService { get; set; }
        //配置自動注入
        [Autowired]
        protected IWeixinSetting WeixinSetting { get; set; }

        public string GetUserName(string userId) {
            return $"{userId}_{DateTime.Now.Ticks}_{this.BizService.GetUserDisplayName(userId)}_weixin={this.WeixinSetting.ApiUrl}";
        }
    }
}

在項目里添加app.config,并設(shè)置為有更新就輸出

,  【聲音】【量天】【矗立】【能量】,【方的】【戰(zhàn)場】【紫真】【又不】,【飄散】【擊螞】【當(dāng)下】【尊大】【斷了】.【里面】【骨下】【暢沒】【擊中】【作勢】,【新派】【神族】【是一】【活意】,【行設(shè)】【有黑】【非?!俊居蚶铩俊疽孕巍?【案發(fā)】【歸入】【間都】【血河】【音似】【到?jīng)]】,【微微】【毒蛤】【脫了】【這尊】,【掉了】【已經(jīng)】【凜然】【筑前】【在左】,【一望】【人真】【眼的】.【的陰】【戰(zhàn)斗】【是一】【鎖區(qū)】,【好歹】【展鯤】【難性】【掉這】,【噬整】【可以】【真的】【白象】.【士卒】!【覺要】【雨般】【體積】【里卻】【生命】【個黑】【神強(qiáng)】.【只有】,

 

app.config內(nèi)容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <devfx>
        <myservice>
            <weixin apiUrl="https://api.weixin.qq.com/sns/oauth2/access_token"
                    appId="1234567890" appKey="0123456789" />
        </myservice>
    </devfx>
</configuration>

運(yùn)行下:

 

 

最后介紹下類似mybatis的數(shù)據(jù)訪問是如何開箱即用的,因為涉及到數(shù)據(jù)庫,稍微復(fù)雜些,但還是很方便的。

我們以操作MySql為例,首先需要使用NuGet安裝MySql驅(qū)動包,目前框架默認(rèn)使用社區(qū)版的MySql驅(qū)動:MySqlConnector

 

定義我們的數(shù)據(jù)訪問層接口

using ConsoleApp1.Models;
using DevFx;
using DevFx.Data;

namespace ConsoleApp1.Data
{
    //定義數(shù)據(jù)操作接口,[DataService]告訴框架這是一個數(shù)據(jù)操作接口
    [DataService(GroupName = "MyService")]
    public interface IMyDataService : ISessionDataService
    {
        EventMessage GetEventMessageByID(string id);
    }
}

在項目中,添加一個.sqlconfig文件,用來編寫對應(yīng)的Sql語句,并把這個文件按嵌入資源形式設(shè)置

 

 sqlconfig內(nèi)容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <devfx>
        <data>
            <statements name="MyService">
                <add name="GetEventMessageByID">
                    <sql>
                        <![CDATA[SELECT * FROM EventMessages WHERE MessageGuid = @ID]]>
                    </sql>
                </add>
            </statements>
        </data>
    </devfx>
</configuration>

相信聰明的你能看出對應(yīng)關(guān)系

然后就是在app.config里配置鏈接字符串,如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <devfx>
        <data debug="true">
            <connectionStrings>
                <add name="EventMessageConnection" connectionString="Database=EventMessages;Data Source=數(shù)據(jù)庫IP;User ID=數(shù)據(jù)庫用戶;Password=密碼;Character Set=utf8" providerName="System.Data.MySqlClient" />
            </connectionStrings>
            <dataStorages defaultStorage="EventMessageStorage">
                <add name="EventMessageStorage" connectionName="EventMessageConnection" type="MySql" />
            </dataStorages>
        </data>

        <myservice>
            <weixin apiUrl="https://api.weixin.qq.com/sns/oauth2/access_token"
                    appId="1234567890" appKey="0123456789" />
        </myservice>
    </devfx>
</configuration>

調(diào)整下我們MySerivce類

using ConsoleApp1.Data;
using DevFx;
using System;

namespace ConsoleApp1
{
    //業(yè)務(wù)邏輯實現(xiàn)類,[Object]特性告訴DevFx這個類需要放入到IoC容器里,DevFx會掃描這個類實現(xiàn)了哪些接口,并做映射
    [Object]
    internal class MyService : IMyService
    {
        //自動裝配(注入)
        [Autowired]
        protected IBizService BizService { get; set; }
        //配置自動注入
        [Autowired]
        protected IWeixinSetting WeixinSetting { get; set; }
        //數(shù)據(jù)訪問接口自動注入
        [Autowired]
        protected IMyDataService MyDataService { get; set; }

        public string GetUserName(string userId) {
            var msg = this.MyDataService.GetEventMessageByID("0000e69f407a4b69bbf3866a499a2eb6");
            var str = $"EventMessage:{msg.MessageGuid}_{msg.Category}_{msg.Priority}_{msg.CreatedTime}";
            return $"{userId}_{DateTime.Now.Ticks}_{this.BizService.GetUserDisplayName(userId)}_weixin={this.WeixinSetting.ApiUrl}{Environment.NewLine}{str}";
        }
    }
}

運(yùn)行下:

 當(dāng)然數(shù)據(jù)訪問不僅僅是查詢,還應(yīng)該有CRUD、分頁以及事務(wù)才完整,這些后續(xù)會詳細(xì)展開。

 

OK,上面就是這些核心功能的展示,另外框架還支持自定義Attribute的處理方便自行擴(kuò)展。

后續(xù)會比較詳細(xì)介紹實現(xiàn)原理以及對框架的拓展,比如服務(wù)注冊發(fā)現(xiàn)、配置中心等等。

有興趣的同學(xué)可以一起共同討論維護(hù),項目開源地址在:https://github.com/mer2/devfx

碼字不容易啊,感興趣的可以去star下。

示例代碼在此:https://files.cnblogs.com/files/R2/ConsoleApp1.zip

 

|轉(zhuǎn)載請注明來源地址:蜘蛛池出租 http://www.wholesalehouseflipping.com/
專注于SEO培訓(xùn),快速排名黑帽SEO https://www.heimao.wiki

版權(quán)聲明:本文為 “蜘蛛池出租” 原創(chuàng)文章,轉(zhuǎn)載請附上原文出處鏈接及本聲明;

原文鏈接:http://www.wholesalehouseflipping.com/post/17870.html

相關(guān)文章

?    2025年11月    ?
12
3456789
10111213141516
17181920212223
24252627282930

搜索

控制面板

您好,歡迎到訪網(wǎng)站!
  查看權(quán)限

網(wǎng)站分類

最新留言

標(biāo)簽列表

最近發(fā)表

作者列表

站點(diǎn)信息

  • 文章總數(shù):10559
  • 頁面總數(shù):3
  • 分類總數(shù):7
  • 標(biāo)簽總數(shù):40
  • 評論總數(shù):783
  • 瀏覽總數(shù):3557254

友情鏈接

免费国产亚洲天堂AV,国产又粗又猛又黄又爽视频,亚州国产精品一线北,国产线播放免费人成视频播放