最近 Enterprise Library 6
正式版公布了,距离上了本子又有1段时间.首先大家来看上面那张模块图,实线表示强正视,虚线表示弱注重(或可选的).
陆.0版本全面协理.net Framework 4.5.

对于加密那壹块,可参看.NET Framework Cryptography
Model
来代替.

争执于前方伍.0本子,移除了上边包车型客车模块:

  • Transient Fault Handling Application Block
  • Semantic Logging Application Block

XML 1

对于Cache能够设想试用.net
framework基类库中的System.Runtime.Caching类,或是Windows
Server App法布里c 缓存天性.

变化:

这一次有壹项改成是那三个模块的效力类不再自动从Unity创造了,也正是辅导也不重视Unity容器组件.
那是分别5.0版本的,今后应有删除上边代码:

AddNewExtension<EnterpriseLibraryCoreExtension>

譬如,使用Logging Application block模块,那须要先选择静态方法注册一下:

                //From Entlib 6 document:Exception Handling Application Block objects can no longer be created 

                // automatically from the Unity DI container. 

                IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();

                LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);

                Logger.SetLogWriter(logWriterFactory.Create());

再十分小概出现如此的不得了:

The LogWriter has not been set for the Logger static class. Set it
invoking the Logger.SetLogWriter method.  

   基于陆.0版本完成面向方向的编制程序AOP,
只需做不难改动即可.从前曾写过一篇小说基于Enterprise Library
4.1版本

首要选用,在Nuget上下载相关Enterprise Library
陆模块,参考上边包车型大巴Nuget的XML文件:

<?xml version="1.0" encoding="utf-8"?>

<packages>

  <package id="CommonServiceLocator" version="1.0" targetFramework="net45" />

  <package id="EnterpriseLibrary.Common" version="6.0.1304.0" targetFramework="net45" />

  <package id="EnterpriseLibrary.ExceptionHandling" version="6.0.1304.0" targetFramework="net45" />

  <package id="EnterpriseLibrary.ExceptionHandling.Logging" version="6.0.1304.0" targetFramework="net45" />

  <package id="EnterpriseLibrary.Logging" version="6.0.1304.0" targetFramework="net45" />

  <package id="EnterpriseLibrary.PolicyInjection" version="6.0.1304.0" targetFramework="net45" />

  <package id="EnterpriseLibrary.Validation" version="6.0.1304.0" targetFramework="net45" />

  <package id="Unity" version="3.0.1304.0" targetFramework="net45" />

  <package id="Unity.Interception" version="3.0.1304.0" targetFramework="net45" />

  <package id="xunit" version="1.9.1" targetFramework="net45" />

</packages>

用二个ServiceFactory来解析Interface,其静态构造方法是这么的,那只是以此类的片断代码:

        static ServiceFactory()

        {

            IUnityContainer container = new UnityContainer();

 

            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EntLib.Config");

            var map = new ExeConfigurationFileMap { ExeConfigFilename = path };

            var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

 

            try

            {

                var section = (UnityConfigurationSection)config.GetSection("unity");

                section.Configure(container, "DefContainer");

 

                //From Entlib 6 document:Exception Handling Application Block objects can no longer be created 

                // automatically from the Unity DI container. 

                IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();

                LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);

                Logger.SetLogWriter(logWriterFactory.Create());

 

                ExceptionPolicyFactory exceptionPolicyFactory = new ExceptionPolicyFactory(configurationSource);

                ExceptionPolicy.SetExceptionManager(exceptionPolicyFactory.CreateManager());

            }

            catch (InvalidOperationException ioe)

            {

                throw;

            }

 

            serviceLocator = new UnityServiceLocator(container);

        }

地点的代码是以前边Assembly目录,加载三个EntLib.Config的XML配置文件,读取个中的Unity配置节与日志,非凡处理的布署节.
下边以色列德国姆o个大约的Interface,
大家在Method加上相应横切的Attribute,分别用作
表明,日志,分外处理:

    [ServiceContract]

    public interface IService1

    {

        [ValidationCallHandler]

        [LogCallHandler(BeforeMessage = "before GetData", AfterMessage = "after GetData",

           IncludeCallStack = true)]

        [ExceptionCallHandler("MyPolicy")]

        [OperationContract]

        string GetData([RangeValidator(1, RangeBoundaryType.Inclusive, 200, RangeBoundaryType.Inclusive)]

            int value);

 

        [OperationContract]

        CompositeType GetDataUsingDataContract(CompositeType composite);

    }

XML,谈到底大家用基于xunit的UnitTest来证实一下,
AOP拦截对艺术参数的求证,此前Method限制了参数值的范围是一-200,第一个情势故意接触那个逻辑.
 

        [Fact]

        public void GetDataWithCuttingConcern()

        {

            //Arrange

            var service = ServiceFactory.GetInstance<WcfService1.IService1>();

            //Act

            string result=service.GetData(100);

 

            //Assert

            Assert.NotNull(result);

        }

 

        [Fact]

        public void GetDataShouldAriseValidationException()

        {

            //Arrange and act

            var service = ServiceFactory.GetInstance<WcfService1.IService1>();

 

            //Assert

            Assert.Throws<ArgumentValidationException>(()=>service.GetData(300));

        }

出于篇幅有限,基于XML的Enterprise
Library配置文件不在那儿就贴了,请参见在此之前的文章,使用时可用EntLib
陆 配置控制台更新一下布局文件.

期望对您软件开发有帮衬.
您也许感兴趣的文章:

行使Unity二.0的Interceptor达成简单AOP
使用EnterpriseLibrary的PIAB与Unity搭建简易AOP框架

作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归笔者和今日头条共有,欢迎转发,但未经小编同意必须保留此段注解,且在小说页面鲜明地方给出最初的文章连接,不然保留追究法律权利的职分。
该作品也同时发表在本人的单独博客中-Petter Liu
Blog

与会八个新的模块:

The Caching Application Block
The Cryptography Application Block
The Security Application Block

安全模块能够选择Claims-based来代表,尤其音讯可参考MSDN.

相关文章

网站地图xml地图