最近 Enterprise Library 6
正式版发表了,距离达了本子又生一段时间.首先我们来拘禁下面这张模块图,实线表示强赖,虚线表示弱依赖(或可选的).
6.0本全面支持.net Framework 4.5.

图片 1

对立于前方5.0版,移除了下的模块:

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

对Cache可以考虑试用.net
framework基类库中的System.Runtime.Caching类似,或是Windows
Server AppFabric 缓存特性.

对此加密这无异于片,可参看.NET Framework Cryptography
Model来代替.

康宁模块可应用Claims-based来替代,更加信息但参考MSDN.

在场两个新的模块:

  • Transient Fault Handling Application Block
  • Semantic Logging Application Block

变化:

这次来一样件改成是那些模块的效益类似不再自动从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.  

   基于6.0版本实现面向方向的编程AOP,
只待做少改就可.以前就描写过千篇一律篇稿子基于Enterprise Library
4.1版本.
首选,在Nuget上生充斥相关Enterprise Library
6模块,参考下面的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配置节与日志,异常处理的部署节.
脚为Demo单大概的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);

    }

最终咱们就此基于xunit的UnitTest来证实一下,
AOP拦截对艺术参数的验证,之前Method限制了参数值的限量是1-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
6 配置控制台更新一下配备文件.

愿意对君软件开发有扶持.
汝或感兴趣的章:

动Unity2.0之Interceptor实现简单AOP
下EnterpriseLibrary的PIAB与Unity搭建筑简易AOP框架

作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
正文版权归作者和博客园共有,欢迎转载,但未经作者同意要保留这个段子声明,且以文章页面明显位置让闹原文连接,否则保留追究法律责任的权利。
拖欠篇也罢还要宣布在自家的独自博客中-Petter Liu
Blog。

相关文章

网站地图xml地图