Project Description
A lightweight IOC framework in .net which can run in both Windows Form and Web Form.
You can create singleton or unsingleton serivce instance in a easier way !

Feature:
  1. Light weight IOC container. You can control your container easily!
  2. Enable in both Windows Application and Web Application
  3. Asp.net mvc enable (both in 1.0 and 2.0)
  4. Aop support
  5. More features to add...

Introduce:
Here are some arguments that prove a need in SummerFramework:

Asp.net

The global.asax should inherit ExtensibleHttpApplicationContainer instead of HttpApplication
    public class Global : ExtensibleHttpApplicationContainer
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            this.Extensions.Add(new SummerHttpApplication("context.xml"));
        }
    }
Update web.config
<httpHandlers>
      <add verb="*" path="*.aspx" type="Summer.Web.Support.WebHandlerFactory"/>
</httpHandlers>
After that, the context in 'context.xml' will be loaded.
If there is any configuration about the asp.net page you request,when you request the page, all the properties will be set.
   public partial class _Default : System.Web.UI.Page
    {
        public DateTimeMakerService Time { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
           Response.Write("DateTimeMakerService.Create is " + Time.Create());
        }
   }
You can get the instance of service in another method
using Summer.Web;
   public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var b = (DateTimeMakerService)this.GetDefaultContextObject("dateTimeMaker");
            var c = b.Create();
            Response.Write("DateTimeMakerService.Create is " + c );
        }
   }

Asp.net mvc

The global.asax is the same as the asp.net's.
  protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            this.Extensions.Add(new SummerHttpApplication("context.xml"));
        }
Update web.config
<httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="UrlRoutingModule" type="Summer.Web.Mvc.UrlRoutingModule"/>
</httpModules>
In controller, you can write some properties like asp.net's.
    [HandleError]
    public class HomeController : Controller
    {
        public RandomNumberMakerService Number { get; set; }
        
        public ActionResult Index()
        {
            var a = (RandomNumberMakerService)this.GetDefaultContextObject("random");
            var b = a.Make();
            ViewData["Message"] = "Welcome to ASP.NET MVC! "+b;
            ViewData["Service"] =  "The Service Return " + Number.Make();
            return View();        
       }
   }

Windows Form

You should load the container when the application start.
[STAThread]
        static void Main()
        {
            SummerWindowsApplication summer = new SummerWindowsApplication("context.xml");
            summer.ApplicationStart();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            Application.ApplicationExit += delegate(object sender, EventArgs e) { summer.ApplicationEnd(); };
        }
In you own form, you can get the service by the method
    public partial class Form1 : Form
    {
        private RandomNumberMakerService m_randomService;
        public Form1()
        {
            InitializeComponent();
            this.m_randomService = (RandomNumberMakerService)this.GetDefaultContextObject("random");
        }

About the container config file

You can define a xml file like the formatter belong:
<?xml version="1.0" encoding="utf-8" ?>
<summer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ioc>
    <root class="Summer.Windows.Context.WindowsContextContainer,Summer.Windows.dll"/>
    <containers>
      <object name="default" class="Summer.Core.Ioc.Context"></object>
    </containers>
    <contexts>
      <context name="default">
        <object name="random" ref="randomImpl"></object>
        <object name="randomImpl" class="WindowsIocDemo.Services.RandomNumberMakerService,WindowsIocDemo.exe" singleton="true"></object>
      </context>
    </contexts>
  </ioc>
</summer>
The ioc node is to define the container about Ioc.
  • root--The base container
if windows application, you should use
<root class="Summer.Windows.Context.WindowsContextContainer,Summer.Windows.dll"/>
else you should use
<root class="Summer.Web.Context.WebContextContainer,Summer.Web.dll"/>
  • containers--The container initialization
Generally, windows application should only define
<object name="default" class="Summer.Core.Ioc.Context"></object>
while the web should define
<object name="config" class="Summer.Core.Ioc.Context"></object>
<object name="default" class="Summer.Core.Ioc.Context"></object>
  • contexts--The context that contains contextObject
You can define all of the context and contextObject
  <context name="config">
        <object name="/default.aspx">
          <property name="Time" value="dateTimeMaker"></property>
          <property name="Number" value="numberMaker"></property>
          <property name="TimeNumber" value="stringMaker"></property>
        </object>
      </context>
      <context name="default">
        <object name="testRef" class="" ref="test"></object>
        <object name="test" class="System.Text.StringBuilder,mscorlib.dll"></object>
        <object name="dateTimeMaker" class="WebIocDemo.Services.DateTimeMakerService,WebIocDemo.dll" singleton="false"></object>
        <object name="numberMaker" class="WebIocDemo.Services.NumberMakerService,WebIocDemo.dll"></object>
        <object name="stringMaker" class="WebIocDemo.Services.DateTimeMakerService,WebIocDemo.dll" singleton="false">
          <property name="Number" value="numberMaker"></property>
        </object>
      </context>
In asp.net,the context named "config" is to define all the pageUrl (without request params) and in asp.net mvc,it is to define the controller name (about route mapping), while in windows application it is not used now.
The context named "default" is to define the service instance.
  • object--The instance
You can define name and class in it. The keyword "ref' is for reference object.
You can use "sigleton=false" to create intance on every request or the instance will be created only when the first request.
The default of sigleton is true, that means the all objects exception the object with state (such as page or cotroller) would be created only once if the singleton is true or default
You can use both "class path,library name" to define serivce's class or only "class path" to define service in current library.

Aop

Your class can inherit AbstractAOPbject to let Aop enable.
You can use LogAttribute or InterceptorAttribute to intercept the method defined in the config file.
        [Log(true)]
        [Interceptor]
        public class LopAOPDemo : AbstractAOPbject
        {
            public int Add(int x, int y)
            {
                return x + y;
            }
            public DateTime GetTime()
            {
                return DateTime.Now;
            }
        }
If using InterceptorAttribute, you should write a class inherited IAdvice to define the action before run and end run.(see at sample)
<?xml version="1.0"?>
<summer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <aop>
    <aspect value="Log">
      <advice type="Before" class="Summer.Log.LogAdvice,Summer.dll">
        <pointcut>Add</pointcut>
        <pointcut>GetTime</pointcut>
      </advice>
      <advice type="After" class="Summer.Log.LogAdvice,Summer.dll">
        <pointcut>Add</pointcut>
        <pointcut>GetTime</pointcut>
      </advice>
    </aspect>
    <aspect value="Interceptor">
      <advice type="Before" class="AopDemo.InterceptorDemoAdvice,AopDemo.exe">
        <pointcut>Add</pointcut>
        <pointcut>GetTime</pointcut>
      </advice>
      <advice type="After" class="AopDemo.InterceptorDemoAdvice,AopDemo.exe">
        <pointcut>Add</pointcut>
        <pointcut>GetTime</pointcut>
      </advice>
    </aspect>
  </aop>
</summer>

Contract:
Edwin Tai
edwin19861218@hotmail.com
http://www.dumuzi.cn

Last edited Nov 25, 2009 at 7:32 AM by edwin1986, version 11