有两个表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;
}
}看,逻辑基本一致,就是返回类型不一样。如何让这两个类的方法节省代码呢?
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;
}
}看,逻辑基本一致,就是返回类型不一样。如何让这两个类的方法节省代码呢?
解决方案 »
- iframe中的页面无法显示,网上找了很久都没用,大家帮帮忙
- 查询数据量过大,查询出结果后,再点击其它按钮,系统白页,提示500错误,求解,高分重谢
- response.write()弹出新窗口的问题(在线=)
- sql语句换行问题
- 菜鸟访问数据库的问题
- 如果获取父窗口的title和页面地址
- 我的页面经常提示<form>标签行出错,有时又是提示<title>标签行错误,但又不是每次都出错,大部分时间是正常的
- cookie中保存用户密码是否安全,如果不保存不验证密码是否安全?
- 右键菜单contextMenuStrip 关联多个文本的问题
- 如何在DataGrid控件的数据在编辑状态下使用正则表达式来验证输入的是不是数字?在线等待...
- 大虾们,请提供下下载地址
- 关于模板页面验证的问题.
Response.Write((int)Test(1));
Response.Write((string)Test("abc"));object Test(object data)
{
return data;
}
另外(object data)这个data,我转递给dal层使用的时候,dal层得负责把data转换成实体类,似乎代码又增多了。
另外(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层,扯远了...
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);
}
虽然我用的Castle的框架,然后用hql语句查询分别在两个实体中写的方法,但是感觉总是不太好
取得后再转化成你要的数据类型
返回我觉得还是应该在相应的数据适配器里面抽象返回类型..
------------
我尝试这样做过,就是让Model.HouseSell和Model.HouseRent都继承一个接口,但是这两个实体毕竟有不同的地上,会多一些或少了些字段。如果我让他们继承的话,那么Model.HouseSell和Model.HouseRent需要实现所有成员,而一些成员对于Model.HouseSell来说是有用的,而对于Model.HouseRent是没用,反之亦然。况且,如果我要适合所有这样的操作呢,但实体结构完全不一样,这种接口的定义就不科学了。
{
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;
}
}----
泛型真强大,刚刚学习了一下,这是我写的简单的例子!怪不得上面几位朋友说用泛型。