有两个表HouseRent和HouseSell,结构基本一致,生成了对应的数据实体HouseRent和HouseSell在这两个表对应的类中,一些方法和逻辑基本是一样的,但是返回类型不一样,分别返回的类型是HouseRent和HouseSell,请看下面这两个方法:
        Model.HouseRent 类:
        public Model.HouseRent Select(int id)
        {
            if (!Caches.EnableCache) return dal.Select(id);            string key = basicKey + "Model_" + id.ToString();
            if (HttpRuntime.Cache[key] != null)
                return (Model.HouseRent)HttpRuntime.Cache[key];
            else
            {
                Model.HouseRent data = dal.Select(id);
                HttpRuntime.Cache.Add(key, data, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                return data;
            }
        }        Model.HouseSell 
        public Model.HouseSell Select(int id)
        {
            if (!Caches.EnableCache) return dal.Select(id);            string key = basicKey + "Model_" + id.ToString();
            if (HttpRuntime.Cache[key] != null)
                return (Model.HouseSell)HttpRuntime.Cache[key];
            else
            {
                Model.HouseSell data = dal.Select(id);
                HttpRuntime.Cache.Add(key, data, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                return data;
            }
        }看,逻辑基本一致,就是返回类型不一样。如何让这两个类的方法节省代码呢?

解决方案 »

  1.   

    楼上所言极是,这也是使用实体的弊端,如果返回dataset就好办多了!
      

  2.   


    Response.Write((int)Test(1));
    Response.Write((string)Test("abc"));object Test(object data)
    {
       return data;
    }
      

  3.   

    首先感谢楼上的兄弟,但是如果采用object的话,就会多一个步骤:强制转换。显然这样不划算。
    另外(object data)这个data,我转递给dal层使用的时候,dal层得负责把data转换成实体类,似乎代码又增多了。
      

  4.   

    如果采用object的话,就会多一个步骤:强制转换。显然这样不划算。 
    另外(object data)这个data,我转递给dal层使用的时候,dal层得负责把data转换成实体类,似乎代码又增多了。
    ----------------------------------------------------------------------------------------------------InterFace:
    public interface IAction
    {
        object Execute(object data);
    }
    ActionFactory:
    public static IAction CreateAddAction()
    {
        return new AddHouseRentAction();
    }
    实现IAction的AddHouseRentAction:
    public object Execute(object data)
    {
        HouseRent newHouseRent = (HouseRent)data;
        return namespace.Data.HouseRentDataHelper.Insert(newHouseRent);
        //这里才是dal
    }
    照我的习惯,我觉得(object data)的转换不应该在dal层,扯远了...
      

  5.   

    老实说,我不怎么理解Nils兄弟的代码。在“public object Execute(object data)”这个方法中,返回的类型还是object, 宋的话,我在使用的地方就把这个object转换成实体类。
      

  6.   

    lovehongyun,zhq3k这两位兄弟都认为可以用泛型解决这个问题,可以贴一段简单的代码示例吗?谢谢了!
      

  7.   

    实现接口IAction
    public class AddDHouseRentAction : IAction
    {
        #region IAction 成员    public object Execute(object data)
        {
            HouseRent newHouseRent = (HouseRent)data;
            return namespace.Data.HouseRentDataHelper.Insert(newHouseRent);
        }    #endregion
    }
    使用倒可以多种方式,接口的:
    private void Button_Click(object sender, EventArgs e)
    {
        HouseRent hr = new HouseRent();
        hr.ID = 1;
        hr.Name = "出租房屋";    IAction action = ActionFactory.CreateAddAction();
        action.Execute(hr);
    }
      

  8.   

    同样困惑
    虽然我用的Castle的框架,然后用hql语句查询分别在两个实体中写的方法,但是感觉总是不太好
      

  9.   

    dal.Select(id); 你这个返回的是什么类型  就用这个类型 做为一个返回类型
    取得后再转化成你要的数据类型
      

  10.   

    在不同的方法中,dal.Select(id)返回的类型不一致,分别是Model.HouseRent和Model.HouseSell
      

  11.   

    哦我理解错了.. 
    返回我觉得还是应该在相应的数据适配器里面抽象返回类型..
    ------------
    我尝试这样做过,就是让Model.HouseSell和Model.HouseRent都继承一个接口,但是这两个实体毕竟有不同的地上,会多一些或少了些字段。如果我让他们继承的话,那么Model.HouseSell和Model.HouseRent需要实现所有成员,而一些成员对于Model.HouseSell来说是有用的,而对于Model.HouseRent是没用,反之亦然。况且,如果我要适合所有这样的操作呢,但实体结构完全不一样,这种接口的定义就不科学了。
      

  12.   

    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Test<string> str = new Test<string>();
            Response.Write(str.GetData("ok"));
            Test<int> ints = new Test<int>();
            Response.Write(ints.GetData(3).ToString());
        }
    }public class Test<T>
    {
        public T GetData(T t)
        {
            return t;
        }
    }----
    泛型真强大,刚刚学习了一下,这是我写的简单的例子!怪不得上面几位朋友说用泛型。