最近公司集50多号开发人员的人力围绕一个系统做开发,框架是免不了要统一的,公司提供的架构,利于分工合作,便于维护,扩展,升级,其中使用了到微软的企业库来解藕系统,只是因为框架封装,于是在网上学习了一个类似的搭建示例,贴在这里主要是为了记录与分享,希望可以帮助到一些朋友。

示例主要讲了一个根据接口反射了实体类对象的方法,而注册这种映射是写在配置文件里面的。

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IClass" type="Unity.Demo.IClass, Unity.Demo"/>
<alias alias="MyClass" type="Unity.Demo.MyClass, Unity.Demo"/>
<container name="First">
<register type="IClass" mapTo="MyClass"/>
</container>
</unity>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>

接口文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Unity.Demo
{
public interface IClass
{
string ShowInfo();
}
}

实现类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Unity.Demo
{
public class MyClass : IClass
{
public string ShowInfo()
{
return "Hello World!";
}
}
}

测试页面:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration; namespace Unity.Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } static IUnityContainer container = new UnityContainer();
public void InitData()
{
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
container.LoadConfiguration(section, "First"); IClass classInfo = container.Resolve<IClass>();
TextBox1.Text = classInfo.ShowInfo();
} protected void Button1_Click(object sender, EventArgs e)
{
InitData();
}
}
}

以上的方法就是使用了微软企业库的unity达到了根据IClass接口名,反射了对象,取得了对象里面的实现方法效果,方便后面进行扩展和维护。

aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Unity.Demo.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="获得信息" /> </div>
</form>
</body>
</html>

源码下载

使用微软企业库5.0提供的unity配置解藕系统demo(源码)的更多相关文章

  1. [EntLib]微软企业库5.0 学习之路——第一步、基本入门

    话说在大学的时候帮老师做项目的时候就已经接触过企业库了但是当初一直没明白为什么要用这个,只觉得好麻烦啊,竟然有那么多的乱七八糟的配置(原来我不知道有配置工具可以进行配置,请原谅我的小白). 直到去年在 ...

  2. 微软企业库5.0 学习之路——第八步、使用Configuration Setting模块等多种方式分类管理企业库配置信息

    在介绍完企业库几个常用模块后,我今天要对企业库的配置文件进行处理,缘由是我打开web.config想进行一些配置的时候发现web.config已经变的异常的臃肿(大量的企业库配置信息充斥其中),所以决 ...

  3. 微软企业库5.0 学习之路——第七步、Cryptographer加密模块简单分析、自定义加密接口及使用—下篇

    在上一篇文章中, 我介绍了企业库Cryptographer模块的一些重要类,同时介绍了企业库Cryptographer模块为我们提供的扩展接口,今天我就要根据这些 接口来进行扩展开发,实现2个加密解密 ...

  4. 微软企业库5.0 学习之路——第六步、使用Validation模块进行服务器端数据验证

    前端时间花了1个多星期的时间写了使用jQuery.Validate进行客户端验证,但是那仅仅是客户端的验证,在开发项目的过程中,客户端的信息永远是不可信的,所以我们还需要在服务器端进行服务器端的验证已 ...

  5. 微软企业库5.0 学习之路——第四步、使用缓存提高网站的性能(EntLib Caching)

    首先先补习下企业库的Caching Application Block的相关知识: 1.四大缓存方式,在Caching Application Block中,主要提供以下四种保存缓存数据的途径,分别是 ...

  6. 微软企业库5.0 学习之路——第二步、使用VS2010+Data Access模块建立多数据库项目

    现在我就开始进入学习之路的第二步——Data Access模块,这个模块是企业库中被使用频率最高的模块,它很好的封装了数据库操作应用,为我们进行多数据库系统开发提供了便利,只需更改配置文件就 可以很快 ...

  7. 权限管理系统源码分析(ASP.NET MVC 4.0 + easyui + EF6.0 + MYSQL/MSSQLSERVER +微软企业库5.0+日志绶存)

    系统采用最先进技术开发: (ASP.NET MVC 4.0 + easyui + EF6.0 + MYSQL/MSSQLSERVER +微软企业库5.0+日志绶存) 大家可以加我QQ讨论 309159 ...

  8. 微软企业库5.0 学习之路——第五步、介绍EntLib.Validation模块信息、验证器的实现层级及内置的各种验证器的使用方法——下篇

    一.独立验证器 我上篇中我将AndCompositeValidator和OrCompositeValidator归为独立验证器,这2个验证器主要是为了第一类验证服务,可以进行多种验证组合在一起进行复杂 ...

  9. 微软企业库5.0学习-Security.Cryptography模块

    一.微软企业库加密应用模块提供了两种加密: 1.Hash providers :离散加密,即数据加密后无法解密 2.Symmetric Cryptography Providers:密钥(对称)加密法 ...

随机推荐

  1. 【2012.1.24更新】不要再在网上搜索eclipse的汉化包了!

    转自:http://blog.csdn.net/gqqnb/article/details/6412364 2012.1.24更新 增加了“安装方法” eclipse是一个程序开发平台,它本身并不限制 ...

  2. (转载)设计模式学习笔记(十一)——Facade外观模式

    (转载)http://www.cnblogs.com/kid-li/archive/2006/07/10/446904.html Facade外观模式,是一种结构型模式,它主要解决的问题是:组件的客户 ...

  3. 计数方法,博弈论(扫描线,树形SG):HDU 5299 Circles Game

    There are n circles on a infinitely large table.With every two circle, either one contains another o ...

  4. webbrowser控件事件

    Beforenavigate2: Fired before navigate occurs in the given WebBrowser(window or frameset element). T ...

  5. HDU-1019 Least Common Multiple

    http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (Java/ ...

  6. HDU1394-Minimum Inversion Number

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number Time Limit: 2000/1000 MS (Ja ...

  7. linux时间设置相关

    1.查询时间命令:date 2.设置日期:date -s mm/dd/yyyy 3.设置时间:date -s HH:MM:SS 4.将当前时间及日期写入BIOS,避免重启失效:hwclock -w 5 ...

  8. PHP 获取时间的各种处理方式!

    今天写下php中,如何通过各种方法 获取当前系统时间.时间戳,并备注各种格式的含义,可灵活变通.1.获取当前时间方法date()很简单,这就是获取时间的方法,格式为:date($format, $ti ...

  9. Ruby on Rails Session 1: How to Build a Ruby on Rails on the Ubuntu.

    About Ruby on Rails Ruby on Rails is an application stack that provides developers with a framework ...

  10. 机器学习笔记1——Introduction

    Introduction What is Machine Learning? Two definitions of Machine Learning are offered. Arthur Samue ...