<Unknow>
nnhy authored at 2016-04-13 20:55:14
1.93 KiB
X
using System;
using NewLife.Model;
using XCode.DataAccessLayer;

namespace XCode.Model
{
    /// <summary>XCode服务对象提供者</summary>
    class XCodeService //: ServiceContainer<XCodeService>
    {
        #region 当前静态服务容器
        /// <summary>当前对象容器</summary>
        public static IObjectContainer Container { get { return ObjectContainer.Current; } }
        #endregion

        static XCodeService()
        {
            var container = Container;
            container.Register<IDataTable, XTable>()
                .AutoRegister<IDataRowEntityAccessorProvider, DataRowEntityAccessorProvider>()
                .AutoRegister<IEntityPersistence, EntityPersistence>()
                .AutoRegister<IModelResolver, ModelResolver>()
                .AutoRegister<IEntityAddition, EntityAddition>();

            DbFactory.Reg(container);

            //EntityAccessorFactory.Reg(container);
        }

        #region 方法
        //public static Type ResolveType<TInterface>(Func<IObjectMap, Boolean> func)
        //{
        //    foreach (var item in Container.ResolveAll(typeof(TInterface)))
        //    {
        //        if (func(item)) return item.ImplementType;
        //    }

        //    return null;
        //}
        #endregion

        #region 使用
        /// <summary>创建模型数据表</summary>
        /// <returns></returns>
        public static IDataTable CreateTable()
        {
            return Container.Resolve<IDataTable>();
        }

        /// <summary>创建实体类的数据行访问器</summary>
        /// <param name="entityType"></param>
        /// <returns></returns>
        public static IDataRowEntityAccessor CreateDataRowEntityAccessor(Type entityType)
        {
            return Container.ResolveInstance<IDataRowEntityAccessorProvider>().CreateDataRowEntityAccessor(entityType);
        }
        #endregion
    }
}