动软.Net代码自动生成器.生成工厂模式三层,怎么接口老是找不到“未将对象引用设置到对象的实例”
附全部代码,如个兄弟有空帮看下,数据库为一个表
Sysuser
id,int,自增
name,varchar(50)附各层代码model:using System;
namespace Maticsoft.Model.NewsManage
{
    /// <summary>
    /// 实体类Sysuser 。(属性说明自动提取数据库字段的描述信息)
    /// </summary>
    public class Sysuser
    {
        public Sysuser()
        { }
        #region Model
        private int _id;
        private string _name;
        /// <summary>
        /// 
        /// </summary>
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string name
        {
            set { _name = value; }
            get { return _name; }
        }
        #endregion Model    }
}
bll:
using System;
using System.Data;
using Maticsoft.DALFactory;
using Maticsoft.Model.NewsManage;using Maticsoft.IDAL;
namespace Maticsoft.BLL.NewsManage
{
    /// <summary>
    /// 业务逻辑类Sysuser 的摘要说明。
    /// </summary>
    public class Sysuser
    {
        private readonly ISysuser dal = DataAccess.CreateSysuser();
        public Sysuser()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            return dal.Exists(ID);
        }        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Maticsoft.Model.NewsManage.Sysuser model)
        {
            return dal.Add(model);
        }        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Maticsoft.Model.NewsManage.Sysuser model)
        {
            dal.Update(model);
        }        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {
            dal.Delete(ID);
        }        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.NewsManage.Sysuser GetModel(int ID)
        {
            return dal.GetModel(ID);
        }        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return dal.GetList("");
        }        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}        #endregion  成员方法
    }
}

解决方案 »

  1.   

    DALFactory
    using System;
    using System.Reflection;
    using System.Configuration;
    namespace Maticsoft.DALFactory
    {
        /// <summary>
        /// Abstract Factory pattern to create the DAL。
        /// </summary>
        public sealed class DataAccess
        {
            private static readonly string path = ConfigurationManager.AppSettings["DAL"];
            public DataAccess()
            { }        #region CreateObject        //不使用缓存
            private static object CreateObjectNoCache(string path, string CacheKey)
            {
                try
                {
                    object objType = Assembly.Load(path).CreateInstance(CacheKey);
                    return objType;
                }
                catch//(System.Exception ex)
                {
                    //string str=ex.Message;// 记录错误日志
                    return null;
                }        }
            //使用缓存
            private static object CreateObject(string path, string CacheKey)
            {
                object objType = DataCache.GetCache(CacheKey);
                if (objType == null)
                {
                    try
                    {
                        objType = Assembly.Load(path).CreateInstance(CacheKey);
                        DataCache.SetCache(CacheKey, objType);// 写入缓存
                    }
                    catch//(System.Exception ex)
                    {
                        //string str=ex.Message;// 记录错误日志
                    }
                }
                return objType;
            }
            #endregion
            /// <summary>
            /// 创建Sysuser数据层接口
            /// </summary>
            public static Maticsoft.IDAL.ISysuser CreateSysuser()
            {
                string CacheKey = path + ".NewsManage.Sysuser";
                object objType = CreateObject(path, CacheKey);
                return (Maticsoft.IDAL.ISysuser)objType;
            }
        }
    }
    using System;
    using System.Web;
    namespace Maticsoft.DALFactory
    {
        /// <summary>
        /// 缓存操作类
        /// </summary>
        public class DataCache
        {
            /// <summary>
            /// 获取当前应用程序指定CacheKey的Cache值
            /// </summary>
            /// <param name="CacheKey"></param>
            /// <returns></returns>
            public static object GetCache(string CacheKey)
            {
                System.Web.Caching.Cache objCache = HttpRuntime.Cache;
                return objCache[CacheKey];
            }        /// <summary>
            /// 设置当前应用程序指定CacheKey的Cache值
            /// </summary>
            /// <param name="CacheKey"></param>
            /// <param name="objObject"></param>
            public static void SetCache(string CacheKey, object objObject)
            {
                System.Web.Caching.Cache objCache = HttpRuntime.Cache;
                objCache.Insert(CacheKey, objObject);
            }
        }
    }IDAL:
    using System;
    using System.Data;
    namespace Maticsoft.IDAL
    {
        /// <summary>
        /// 接口层ISysuser 的摘要说明。
        /// </summary>
        public interface ISysuser
        {
            #region  成员方法
            /// <summary>
            /// 是否存在该记录
            /// </summary>
            bool Exists(int ID);
            /// <summary>
            /// 增加一条数据
            /// </summary>
            int Add(Maticsoft.Model.NewsManage.Sysuser model);
            /// <summary>
            /// 更新一条数据
            /// </summary>
            void Update(Maticsoft.Model.NewsManage.Sysuser model);
            /// <summary>
            /// 删除一条数据
            /// </summary>
            void Delete(int ID);
            /// <summary>
            /// 得到一个对象实体
            /// </summary>
            Maticsoft.Model.NewsManage.Sysuser GetModel(int ID);
            /// <summary>
            /// 获得数据列表
            /// </summary>
            DataSet GetList(string strWhere);
            /// <summary>
            /// 获得数据列表
            /// </summary>
            DataSet GetList();
            /// <summary>
            /// 根据分页获得数据列表
            /// </summary>
            // DataSet GetList(int PageSize,int PageIndex,string strWhere);
            #endregion  成员方法
        }
    }
      

  2.   

    SQLServerDAL:
    using System;
    using System.Data;
    using System.Text;
    using System.Data.SqlClient;
    using Maticsoft.IDAL;
    using Maticsoft.DBUtility;//请先添加引用;
    namespace Maticsoft.SQLServerDAL.NewsManage
    {
        /// <summary>
        /// 数据访问类Sysuser 。
        /// </summary>
        public class Sysuser : ISysuser
        {
            public Sysuser()
            { }
            #region  成员方法
            /// <summary>
            /// 是否存在该记录
            /// </summary>
            public bool Exists(int ID)
            {
                int rowsAffected;
                SqlParameter[] parameters = {
    new SqlParameter("@ID", SqlDbType.Int,4)
    };
                parameters[0].Value = ID;
                int result = DbHelperSQL.RunProcedure("UP_Sysuser_Exists", parameters, out rowsAffected);
                if (result == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }        /// <summary>
            ///  增加一条数据
            /// </summary>
            public int Add(Maticsoft.Model.NewsManage.Sysuser model)
            {
                int rowsAffected;
                SqlParameter[] parameters = {
    new SqlParameter("@ID", SqlDbType.Int,4),
    new SqlParameter("@name", SqlDbType.VarChar,50)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.name;            DbHelperSQL.RunProcedure("UP_Sysuser_ADD", parameters, out rowsAffected);
                return (int)parameters[0].Value;
            }        /// <summary>
            ///  更新一条数据
            /// </summary>
            public void Update(Maticsoft.Model.NewsManage.Sysuser model)
            {
                int rowsAffected;
                SqlParameter[] parameters = {
    new SqlParameter("@ID", SqlDbType.Int,4),
    new SqlParameter("@name", SqlDbType.VarChar,50)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.name;            DbHelperSQL.RunProcedure("UP_Sysuser_Update", parameters, out rowsAffected);
            }        /// <summary>
            /// 删除一条数据
            /// </summary>
            public void Delete(int ID)
            {            int rowsAffected;
                SqlParameter[] parameters = {
    new SqlParameter("@ID", SqlDbType.Int,4)
    };
                parameters[0].Value = ID;
                DbHelperSQL.RunProcedure("UP_Sysuser_Delete", parameters, out rowsAffected);
            }        /// <summary>
            /// 得到一个对象实体
            /// </summary>
            public Maticsoft.Model.NewsManage.Sysuser GetModel(int ID)
            {
                SqlParameter[] parameters = {
    new SqlParameter("@ID", SqlDbType.Int,4)
    };
                parameters[0].Value = ID;
                Maticsoft.Model.NewsManage.Sysuser model = new Maticsoft.Model.NewsManage.Sysuser();            DataSet ds = DbHelperSQL.RunProcedure("UP_Sysuser_GetModel", parameters, "ds");
                model.ID = ID;
                if (ds.Tables[0].Rows.Count > 0)
                {
                    model.name = ds.Tables[0].Rows[0]["name"].ToString();
                    return model;
                }
                else
                {
                    return null;
                }
            }        /// <summary>
            /// 获取数据列表
            /// </summary>
            public DataSet GetList()
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select [ID],[name] ");
                strSql.Append(" FROM Sysuser ");
                return DbHelperSQL.Query(strSql.ToString());
            }        /// <summary>
            /// 获取数据列表
            /// </summary>
            public DataSet GetList(string strWhere)
            {            StringBuilder strSql = new StringBuilder();
                strSql.Append("select [ID],[name] ");
                strSql.Append(" FROM Sysuser ");
                if (strWhere.Trim() != "")
                {                strSql.Append(" where " + strWhere);
                }
                return DbHelperSQL.Query(strSql.ToString());        }        /*
            /// <summary>
            /// 分页获取数据列表
            /// </summary>
            public DataSet GetList(int PageSize,int PageIndex,string strWhere)
            {
                SqlParameter[] parameters = {
                        new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                        new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                        new SqlParameter("@PageSize", SqlDbType.Int),
                        new SqlParameter("@PageIndex", SqlDbType.Int),
                        new SqlParameter("@IsReCount", SqlDbType.Bit),
                        new SqlParameter("@OrderType", SqlDbType.Bit),
                        new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                        };
                parameters[0].Value = "Sysuser";
                parameters[1].Value = "ID";
                parameters[2].Value = PageSize;
                parameters[3].Value = PageIndex;
                parameters[4].Value = 0;
                parameters[5].Value = 0;
                parameters[6].Value = strWhere;
                return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
            }*/        #endregion  成员方法
        }
    }
      

  3.   

    估计你反射没有创建成功 ~~!
    原因可能 这句 DataAccess.CreateSysuser() 没有成功创建dal
      

  4.   

    请检查程序集名称和命名空间不一致。
      因为Assembly.Load(path)这里的path必须是一个程序集的名称
      CreateInstance(CacheKey)这里的CacheKey其实是需要反射的类型全名(包括命名空间的全路径)。
    所以,尽量让程序集名称和命名空间一致,这样的得到的类型全名=程序集名称+类名。
    否则,你需要把CacheKey换成实际的类型全名。
      

  5.   

    你看你的Web引用SQLSQLServerDAL没,没有引用就引用它,在看Web.config中
    <!--数据访问层程序集名称 -->是不是你的项目命名空间下的SQLSQLServerDAL,不是的话改为你的项目命名空间下的SQLSQLServerDAL,在重新生成试试