您的位置:程序门 -> .net技术 ->



实现 o/r 映射关系:如何写dal层...


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


实现 o/r 映射关系:如何写dal层...[已结贴,结贴人:yanjinbin01]
发表于:2007-12-25 15:05:44 楼主
如有一表t:
t1   int   not   null   主键   递增(1,1)
t2   int   null
t3   int   null    
------------
model   实体类设计如:
public   class   t
{
        protected   int   _t1;
        protected   int   _t2:
        protected   int   _t3;
        public   t(){}
        public   t(int   myid)
        {
            _t1   =   myid   ;
        }
        //主键
        public   int   id
        {
              get{return   _t1;}
        }    
        public   int   t2
      {
        get{return   _t2;}
        set{_t2=values}
      }

        public   int   t3
      {
        get{return   _t3}
        set{_t3=value}
      }
}

如何设计dal类   来操作上面的model实体类??
(假设是sql   server数据库:假设connection   =conn   command=cmd   对象创建好了的,可直接用的。)
public   class   dal_t
{
    //如何创建
    //+1   重载   创建新类   可调用t_insert   保存为一条新记录,但是如何取id值(因为id是递增的,如取数据库最后一条记录+1值,但入同时有人创建新类怎么办?)
    public   static   t   create_t()
    {
          return   new   t();     //这里id值如何设置
    }

    //+2   重载   从现有边创建类  
    public   static   t   create_t(int   id)
    {
          cmd.commandtext   =   "select*form   t   where   t1   =   "   +   id;
          datareader   dr   =   cmd.EXECutereader();
          dr.reader();
          t   tc   =   new   tc   (convert.toint32(dr[0]))   ;  
          tc.t2   =   convert.toint32(dr[1]);
          tc.t3   =   convert.toint32(dr[2]);

          return   t3;
    }
    //如何添加
    public   static   bool   t_insert(t   myt)
    {
          //因该如何写代码     关键是保存id值是一样的   如何做
    }

    //如何编辑
    public   static   bool   t_update(t   myt)
    {
          cmd.commandtext="update   t   set   t2   ="+   myt.t2   +   "   set   t3   =   "+myt.t3   +"   where   t   =   "+myt.id;
          int   b   =   cmd.EXECutenonquery();
          if(b   ==0)
          {
            return   false;
          }else{
            return   true;
          }  
    }

    //如何删除
    public   static   bool   t_update(int   id)
    {
          cmd.commandtext="delete   from   t   where   t1   =   "   +   id   ;
          int   b   =   cmd.EXECutenonquery();
          if(b   ==0)
        {
            return   false;
        }else{
            return   true;
        }  
    }
}

对于我的dal类谁帮我实现下创建新类   保存新类功能,还有谁对我这样的设计有更好建议告诉我啊.万分感激.


另:谁有用codesmith   实现这两层的摸版代码cst文件,谁有能共享下,万分感激.请发到邮箱:yanjinbin0@163.com   ..
发表于:2007-12-25 15:49:311楼 得分:0
不知道你打算干什么;你不是已经写好了吗?自动代码生成?
发表于:2007-12-25 16:41:422楼 得分:0
  老大   我还没完全实现啊,自动创建一个model类   那个   id因该怎么算啊.头晕.
发表于:2007-12-25 16:43:363楼 得分:0
我现在就在写codesmith摸版,谁要给我一份参考下也好啊.
现在碰到数据类型处理   要自己写   好难,谁要现成的   我是c#来写template  
摸版的.
发表于:2007-12-25 17:16:374楼 得分:50
        //项目一,实体类
        public   class   model_t
        {
                protected   int   _t1;
                protected   int   _t2;
                protected   int   _t3;
                public   model_t()   {   }
                //主键  
                public   int   id
                {
                        get   {   return   _t1;   }
                }
                public   int   t2
                {
                        get   {   return   _t2;   }
                        set   {   _t2   =   value;   }
                }

                public   int   t3
                {
                        get   {   return   _t3;   }
                        set   {   _t3   =   value;   }
                }
        }

        //项目二,有必要写接口
        public   interface   i_t
        {
                //比如这里有个添加数据的方法
                bool   t_add(model_t   model);
        }

        //项目三,数据层
        public   class   sql_t   :i_t
        {
                bool   i_t.t_add(model_t   model)
                {
                        sqlparameter[]   para   =   {
                                new   sqlparameter("@t2",   sqldbtype.varchar,5),
                                new   sqlparameter("@t3",   sqldbtype.varchar,5)};
                        para[0].value   =   model.t2;
                        para[1].value   =   model.t3;
                        sqlcommand   cmd   =   new   sqlcommand("insert   t(t2,t3)   values(@t2,t3)",   "fdsafa");
                        foreach   (sqlparameter   parm   in   para)
                        {
                                cmd.parameters.add(parm);
                        }
                        return   cmd.EXECutenonquery()   >   0;
                }


        }
        //dal,其实应该写反射到相关的数据处理类的.这里我省略了.
        public   class   dal
        {
                i_t   dal   =   new   sql_t();
                //简单的处理方法,我直接return出来,具体你要怎么样处理,自己决定
                public   bool   t_add(model_t   model)
                {
                        return   dal.t_add(model);
                }
        }

        //例子

        public   class   test
        {
                //添加信息
                public   void   test()
                {
                        model_t   mod   =   new   model_t();
                        mod.t2   =   "aa";
                        mod.t3   =   "bb";
                        new   dal().t_add(mod);
                }
               
发表于:2007-12-25 17:29:455楼 得分:0
to:cnaspnet   万分感激...   很完全了.

发表于:2007-12-26 09:30:316楼 得分:0
              //dal,其实应该写反射到相关的数据处理类的.这里我省略了.  
                public       class       dal  
                {  
                                i_t       dal       =       new       sql_t();  
                                //简单的处理方法,我直接return出来,具体你要怎么样处理,自己决定  
                                public       bool       t_add(model_t       model)  
                                {  
                                                return       dal.t_add(model);  
                                }  
                }  

我这里把他反射实体类model_t  
如:
                public       class       model_t  
                {  
                                protected       int       _t1;  
                                protected       int       _t2;  
                                protected       int       _t3;  
                                protected     i_t     it   =   new   sql_t();  
                                  public       model_t()       {       }  
                                /*....代码*/
                                 
                                //把方法反射到这里  
                                public   bool   t_add()  
                                  {
                                        it.t_add(this);
                                  }
                }  

我这样做到底行不行啊.符合不符合分层结构啊.
发表于:2007-12-26 10:22:557楼 得分:0
    ...up.
发表于:2007-12-26 10:30:228楼 得分:0
看看...
发表于:2007-12-26 10:44:069楼 得分:0
贴个完整的结构吧:
大家给点意见:
表   t:
结构:   t1   int   not   null   主键   递增(1,1)
            t2   int   null  
            t3   varchar(50)   null

//实体类
public   class   model  
{
                protected       int       _t1;  
                protected       int       _t2:  
                protected       int       _t3;  
                public       t(){}  
                public       t(int       myid)  
                {  
                        _t1       =       myid       ;  
                }  
                //主键  
                public       int       id  
                {  
                            get{return       _t1;}  
                }          
                public       int       t2  
                {  
                    get{return       _t2;}  
                    set{_t2=values}  
                }  

                public       int       t3  
                {  
                    get{return       _t3}  
                    set{_t3=value}  
                }  
}

//实体类接口   imodel  
public   inertface   imodel  
{
        //添加
        bool   add_model(model   md);
        //删除
        bool   updat_model(model   md);
        //删除
        bool   delete_model(model   md);
}

//方法类接口   idal
public   inertface   idal
{
      model   create_model(int   id);   //根据id创建对象
      dataset   writedata();     //返回多行数据
}

//方法类   dal   实现接口   imodel   idal   ,这里限制了dal对直接使用imodel接口方法
public   class   dal:imodel,idal
{
      public   bool   imodel.add_model(model   md)
      {
        //对应sql代码
      }
 
    public   bool   imodel.updat_model(model   md)
      {
        //对应sql代码
      }

    public   bool   imodel.delete_model(model   md)
      {
        //对应sql代码
      }
      public   model   create_model(int   id)
      {
          //对应sql代码
      }
      public   dataset   writedata(int   id)
      {
          //对应sql代码
      }
}

//下面我们imodel   方法在反射到model类中使用

//重写我们的model类
public   class   model  
{
                protected       int       _t1;  
                protected       int       _t2:  
                protected       int       _t3;    
                protected   imodel   imd   =   new   dal();
                public       t(){}  
                public       t(int       myid)  
                {  
                        _t1       =       myid       ;  
                }  
                //主键  
                public       int       id  
                {  
                            get{return       _t1;}  
                }          
                public       int       t2  
                {  
                    get{return       _t2;}  
                    set{_t2=values}  
                }  

                public       int       t3  
                {  
                    get{return       _t3}  
                    set{_t3=value}  
                }  
                  //添加   删除   编辑   方法
                  public   bool   add_model()
                  {
                            imd.add_model(this);
                  }

                  public   bool   update_model()
                  {
                            imd.update_model(this);
                  }

                  public   bool   delete_model()
                  {
                            imd.delete_model(this);
                  }
}
发表于:2007-12-26 11:11:0110楼 得分:0
都可以,我们用的是petshop4的架构,楼主你可以参考看看
发表于:2007-12-26 16:10:1711楼 得分:0
    说实话   那东西我没看明白.
发表于:2007-12-26 17:16:0212楼 得分:0
看看来
发表于:2007-12-26 17:19:3913楼 得分:0
study,mark,up.


快速检索

最新资讯
热门点击