30 Jul 2009

如何判断一个类型是nullable类型,并得知它的原始类型

有些时候我们需要判断某个类型是否是Nullable类型,并且可能需要知道它的原始类型,
比如在做些ORM相关工作时,就需要这方面的应用,如下代码使用可以实现这个要求

class Program
    {
        static void Main( string[] args )
        {
            RL( );

        Type type =  typeof( DateTime? );
        Type originalType;

        if ( IsNullable( type , out originalType ) )
        {
            WL( originalType.ToString( ) );
        }
        else
        {
            WL( "NOT Nullable" );
        }

        type =  typeof( DateTime );

        if ( IsNullable( type , out originalType ) )
        {
            WL( originalType.ToString( ) );
        }
        else
        {
            WL( "NOT Nullable" );
        }

        RL( );
    }

    private static bool IsNullable( Type p , out Type originalType )
    {
        bool result = false;

        if ( p.IsGenericType && p.GetGenericTypeDefinition( ) == typeof( Nullable<> ) )
        {
            result = true;
            originalType = p.GetGenericArguments( )[0];
        }
        else
        {
            originalType = null;
        }

        return result;
    }

    #region Helper methods

    private static void WL( object text , params object[] args )
    {
        Console.WriteLine( text.ToString( ) , args );
    }

    private static void RL( )
    {
        Console.ReadLine( );
    }

    private static void Break( )
    {
        System.Diagnostics.Debugger.Break( );
    }

    #endregion
}

31 Jan 2008

DbEntry.Net 又一个国产开源ORM数据访问及 WEB 框架

今天去逛CodePlex发现这个东西

链接到JavaEyeblog,作者:梁利锋

中文描述:http://llf.hanzify.org/llf/show.asp?id=555

Features:

FluentInterface query syntax

  • RoR ActiveRecord style syntax
  • Ambient transaction
  • Dynamic object
  • Partial update
  • 1:1 1:Many Many:Many relations
  • Auto create table
  • Anti sql injection
  • Multiple data source
  • Object validation
  • Paged selector and collection
  • Nullable support
  • DbEntryDataSource
  • ASP.NET 2.0 Membership support
  • Built-in Cache Support
  • Ruby On Rails style MVC framework
  • High performance, almost same as using ADO.NET directly
  • Lightwight, the binary file only about 268KB
  • Rails Style MVC
    1. Getting Started
    2. Scaffolding
    3. Controller
    4. Viewer
    5. Configuration
    6. Deployment


另外还有一些不错的东西比如他的 Rails Style MVC 和TemplateBuilder  还有 HtmlBuilder

看了看并没有Nbear那样类似Linq的查询支持.

令人欣慰的是这个东西一直从.net1.1做到现在,而且就一个人开发,从codeplex上来看,最近还有代码签入.

http://www.codeplex.com/DbEntry/SourceControl/ListDownloadableCommits.aspx



最后贴一下地址:http://www.codeplex.com/DbEntry