自己写了个Ioc框架----SummerFramework

  好吧 我承认idea来自于spring. 一直非常讨厌java, .net领域虽然有spring.net这个.net的迁移版本.但仍然非常sick.闲极无聊之际索性自己写了个Ioc框架,以此打法寂寞之感. 当然,要做到注入,光一个稳定的容器还是不够的,需要熟悉整个请求访问的流程. 尤其在asp.net(或mvc)中,这就更加重要了.

 

项目主页 http://summerframework.codeplex.com/ 希望广大牛人多提提意见建议

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...

   这个项目我花了一个多月的时间,由于平时工作繁忙,未必有时间进行,到今天为止,也算是有了个雏形.为了做到Extensible,参考了不少牛人的代码,自己写代码水平也有所提高.在整个Ioc编写过程中,最为麻烦的其实并不在容器本身,而是在Singleton实现并且setProperty上.对于无状态的Service这本不是问题,但是如果是Page(asp.net)或则Controller(asp.net mvc)这就比较恶心.Context.ApplicationInstance当访问结束就被回收了. 而其属性本身访问是protected的,外部无法更改.或许解决这个办法只有自己写个类继承之,这样却会导致所有需要Singleton的类必须继承之,这不是我想要的.

  因此,目前的版本只能对非状态型的Object做 Singleton.各位如果有所解决方案,可以告诉我.

  总体上讲,这个Ioc还算是比较灵活的.

  如果是web,无论是普通asp.net还是asp.net mvc只需要少量更改global.ascx便可实现了容器的加载.

    public class Global : ExtensibleHttpApplicationContainer
    {
        
protected void Application_Start(object sender, EventArgs e)
        {
            
this.Extensions.Add(new SummerHttpApplication("context.xml"));
        }
    }

 

 

   更改web.config,让我自己来处理请求而不是asp.net本身

 <httpHandlers>

      <add verb="*" path="*.aspx" type="Summer.Web.Support.WebHandlerFactory"/>
</httpHandlers>
或则
<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>


通过配置xml,可以做到对指定aspx或controller进行service Setting或则在页面中可以直接取得相应注入服务的实例

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 );
        }
   }
 
public partial class _Default : System.Web.UI.Page
    {
        
public DateTimeMakerService Time { getset; }
        
protected void Page_Load(object sender, EventArgs e)
        {
           Response.Write(
"DateTimeMakerService.Create is " + Time.Create());
        }
   }

[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(); };
        }

 在windows Form中的设置,则web类似,只需在Program.main方法中加载容器即可

 

 

  


 

posted @ 2009-11-25 17:05  Edwin Tai  阅读(2714)  评论(20编辑  收藏  举报