Spring.Data.IBatis源码

Spring.Data.IBatis这个在网上找了很久都没有找到,于是把该项目反编译后获取了源码,并在目前最新的spring.net(1.3.0.20349)和iBatis.NET(1.6.2.0)下编译通过了。源码如下:

AbstractIBatisFactoryObject.cs源码:

using IBatisNet.Common.Utilities;
using Spring.Core.IO;
using Spring.Objects.Factory.Config;
using System;
using System.IO;

namespace Spring.Data.IBatis
{
    public abstract class AbstractIBatisFactoryObject : AbstractFactoryObject
    {
        private IResource configuration;
        private ConfigureHandler configWatcher;

        protected AbstractIBatisFactoryObject()
        {
        }

        protected override object CreateInstance()
        {
            if (this.Config == null)
            {
                return this.CreateUsingDefaultConfig();
            }
            return this.CreateUsingCustomConfig();
        }

        protected abstract object CreateUsingCustomConfig();
        protected abstract object CreateUsingDefaultConfig();
        protected virtual string GetConfigFileName()
        {
            FileInfo info = null;
            try
            {
                info = this.Config.File;
            }
            catch (IOException)
            {
                throw new ArgumentException("The 'Config' property cannot be resolved to an iBatis.NET config file that physically exists on the filesystem.");
            }
            return info.Name;
        }

        public IResource Config
        {
            get
            {
                return this.configuration;
            }
            set
            {
                this.configuration = value;
            }
        }

        public ConfigureHandler ConfigWatcher
        {
            get
            {
                return this.configWatcher;
            }
            set
            {
                this.configWatcher = value;
            }
        }
    }
}

DateTypeHandlerCallback.cs源码:

using IBatisNet.DataMapper.TypeHandlers;
using System;

namespace Spring.Data.IBatis
{
    public class DateTypeHandlerCallback : ITypeHandlerCallback
    {
        object ITypeHandlerCallback.GetResult(IResultGetter getter)
        {
            if ((getter.Value == null) || (getter.Value == DBNull.Value))
            {
                return DateTime.MinValue;
            }
            return getter.Value;
        }

        void ITypeHandlerCallback.SetParameter(IParameterSetter setter, object parameter)
        {
            if (parameter is DateTime)
            {
                DateTime time = (DateTime) parameter;
                if (time == DateTime.MinValue)
                {
                    setter.Value=null;
                    return;
                }
            }
            setter.Value=parameter;
        }

        object ITypeHandlerCallback.ValueOf(string s)
        {
            return DateTime.Parse(s);
        }

        object ITypeHandlerCallback.NullValue
        {
            get
            {
                return DBNull.Value;
            }
        }
    }
}
IbatisPlatformTransactionManager.cs源码:
using IBatisNet.DataMapper;
using Spring.Objects.Factory;
using Spring.Transaction;
using Spring.Transaction.Support;
using System;

namespace Spring.Data.IBatis
{
    public class IbatisPlatformTransactionManager : AbstractPlatformTransactionManager, IInitializingObject
    {
        private ISqlMapper sqlMap;

        public IbatisPlatformTransactionManager()
        {
        }

        public IbatisPlatformTransactionManager(ISqlMapper sqlMap)
        {
            this.sqlMap = sqlMap;
        }

        public void AfterPropertiesSet()
        {
        }

        protected override void DoBegin(object transaction, ITransactionDefinition definition)
        {
            if (this.sqlMap == null)
            {
                throw new ArgumentException("SqlMap is required to be set on IbatisPlatformTransactionManager");
            }
            base.log.Debug("开始事务");
            this.sqlMap.BeginTransaction();
        }

        protected override void DoCommit(DefaultTransactionStatus status)
        {
            base.log.Debug("提交事务");
            this.sqlMap.CommitTransaction();
        }

        protected override object DoGetTransaction()
        {
            IbatisTransactionObject obj2 = new IbatisTransactionObject();
            obj2.SavepointAllowed = base.NestedTransactionsAllowed;
            SqlMapHolder resource = (SqlMapHolder) TransactionSynchronizationManager.GetResource(this.sqlMap);
            obj2.SetSqlMapHolder(resource, false);
            return obj2;
        }

        protected override void DoRollback(DefaultTransactionStatus status)
        {
            base.log.Debug("回滚事务");
            this.sqlMap.RollBackTransaction();
        }

        protected override bool IsExistingTransaction(object transaction)
        {
            IbatisTransactionObject obj2 = (IbatisTransactionObject) transaction;
            return ((obj2.SqlMapHolder != null) && obj2.SqlMapHolder.TransactionActive);
        }

        public ISqlMapper SqlMap
        {
            get
            {
                return this.sqlMap;
            }
            set
            {
                this.sqlMap = value;
            }
        }

        private class IbatisTransactionObject : IbatisTransactionObjectSupport
        {
            private bool newSqlMapHolder;

            public void SetRollbackOnly()
            {
                base.SqlMapHolder.RollbackOnly = true;
            }

            public void SetSqlMapHolder(SqlMapHolder sqlMapHolder, bool newSqlMap)
            {
                base.SqlMapHolder = sqlMapHolder;
                this.newSqlMapHolder = newSqlMap;
            }

            public bool HasTransaction
            {
                get
                {
                    return ((base.SqlMapHolder != null) && base.SqlMapHolder.TransactionActive);
                }
            }

            public bool NewSqlMapHolder
            {
                get
                {
                    return this.newSqlMapHolder;
                }
            }

            public bool RollbackOnly
            {
                get
                {
                    return base.SqlMapHolder.RollbackOnly;
                }
            }
        }
    }
}
IbatisTransactionObjectSupport.cs源码:

using Common.Logging;
using IBatisNet.Common.Transaction;
using Spring.Transaction;
using System;

namespace Spring.Data.IBatis
{
    public abstract class IbatisTransactionObjectSupport : ISavepointManager
    {
        protected static readonly ILog log = LogManager.GetLogger(typeof(IbatisTransactionObjectSupport));
        private IsolationLevel previousIsolationLevel;
        private bool savepointAllowed;
        private Spring.Data.IBatis.SqlMapHolder sqlMapHolder;

        protected IbatisTransactionObjectSupport()
        {
        }

        public void CreateSavepoint(string savepointName)
        {
            throw new NotImplementedException();
        }

        public void ReleaseSavepoint(string savepoint)
        {
            throw new NotImplementedException();
        }

        public void RollbackToSavepoint(string savepoint)
        {
            throw new NotImplementedException();
        }

        public bool HasSqlMapHolder
        {
            get
            {
                return (this.sqlMapHolder != null);
            }
        }

        public IsolationLevel PreviousIsolationLevel
        {
            get
            {
                return this.previousIsolationLevel;
            }
            set
            {
                this.previousIsolationLevel = value;
            }
        }

        public bool SavepointAllowed
        {
            get
            {
                return this.savepointAllowed;
            }
            set
            {
                this.savepointAllowed = value;
            }
        }

        public Spring.Data.IBatis.SqlMapHolder SqlMapHolder
        {
            get
            {
                return this.sqlMapHolder;
            }
            set
            {
                this.sqlMapHolder = value;
            }
        }
    }
}
SqlMapHolder.cs源码:

using IBatisNet.DataMapper;
using Spring.Transaction.Support;

namespace Spring.Data.IBatis
{
    public class SqlMapHolder : ResourceHolderSupport
    {
        private ISqlMapper currentSqlMap;
        private ISqlMapSession currentSqlMapSession;
        private bool transactionActive = false;

        public SqlMapHolder(ISqlMapper sqlMap, ISqlMapSession transaction)
        {
            this.currentSqlMap = sqlMap;
            this.currentSqlMapSession = transaction;
        }

        public override void Clear()
        {
            base.Clear();
            this.transactionActive = false;
        }

        public bool HasSqlMap
        {
            get
            {
                return (this.currentSqlMap != null);
            }
        }

        public ISqlMapper SqlMap
        {
            get
            {
                return this.currentSqlMap;
            }
            set
            {
                this.currentSqlMap = value;
            }
        }

        public ISqlMapSession Transaction
        {
            get
            {
                return this.currentSqlMapSession;
            }
            set
            {
                this.currentSqlMapSession = value;
            }
        }

        public bool TransactionActive
        {
            get
            {
                return this.transactionActive;
            }
            set
            {
                this.transactionActive = value;
            }
        }
    }
}
SqlMapperFactoryObject.cs源码:

using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using Spring.Context;
using System;
using System.IO;

namespace Spring.Data.IBatis
{
    public class SqlMapperFactoryObject : AbstractIBatisFactoryObject, IApplicationContextAware
    {
        private IApplicationContext springContext;
        private DomSqlMapBuilder sqlMapBuilder;

        protected override object CreateUsingCustomConfig()
        {
            if (base.ConfigWatcher != null)
            {
                return this.SqlMapBuilder.ConfigureAndWatch(base.Config.File, base.ConfigWatcher);
            }
            using (Stream stream = base.Config.InputStream)
            {
                return this.SqlMapBuilder.Configure(stream);
            }
        }

        protected override object CreateUsingDefaultConfig()
        {
            if (base.ConfigWatcher != null)
            {
                return this.SqlMapBuilder.ConfigureAndWatch(base.ConfigWatcher);
            }
            return this.SqlMapBuilder.Configure();
        }

        public IApplicationContext ApplicationContext
        {
            get
            {
                return this.springContext;
            }
            set
            {
                this.springContext = value;
            }
        }

        public override Type ObjectType
        {
            get
            {
                return typeof(ISqlMapper);
            }
        }

        public DomSqlMapBuilder SqlMapBuilder
        {
            get
            {
                if (this.sqlMapBuilder == null)
                {
                    this.sqlMapBuilder = new DomSqlMapBuilder();
                }
                return this.sqlMapBuilder;
            }
            set
            {
                this.sqlMapBuilder = value;
            }
        }
    }
}

Spring.Data.IBatis 源码下载

原创作品出自努力偷懒,转载请说明文章出处http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值