项目中公共信息的存储和调用,请高手帮忙(100分)

mywebcom 2004-08-30 11:13:07
在一个项目中,应该有多种方式共享一些公共信息(例如:一些提示信息,
一些错误信息,还有一些相似的帮助信息,等等),究竟是那几种呢?
目前我们的.net 可以提供application, cache, Web.config(用键值),
以及一些xml 文件也可以达到效果。

我发现在web项目中真正能够实现的是后面2个(Web.config(用键值),
一些xml 文件 ), 不知道前面2种的用法,尤其想请教 cache 地用法。
...全文
197 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
robin0925 2004-08-30
  • 打赏
  • 举报
回复
是阿!搂主思维要开阔点!你这样说,兄弟们都不好回答阿!
mywebcom 2004-08-30
  • 打赏
  • 举报
回复
高手来呀!!!
mywebcom 2004-08-30
  • 打赏
  • 举报
回复
/// <summary>
/// 通过strCode,获得所对应的信息。
/// </summary>
/// <param name="strCode"></param>
/// <returns></returns>
public string GetMessage(string strCode)
{
try
{
DsErrMsg.TErrMsgRow drwErrMsg; //声明一个错误的行
DsErrMsg dsErrMsg = new DsErrMsg(); //声明并实例化一个DataSet

//TODO:CACHE
if (Cache["dsErrMsg"] == null)
{
GetDatasetErrMsg();
}
dsErrMsg = (DsErrMsg)Cache["dsErrMsg"];
drwErrMsg = (DsErrMsg.TErrMsgRow)dsErrMsg.TErrMsg.Select("sCode='" + strCode + "'")[0];
if (drwErrMsg != null)
{
return drwErrMsg.sInformation.ToString();
}
else
{
return null;
}
}
catch (Exception ex)
{
ExceptionData.InsertException(ex.Message);
return null;
}

}
DsErrMsg GetDatasetErrMsg()
{
DsErrMsg dsErrMsg = new DsErrMsg();
//......
//从数据库得到数据填充 dsErrMsg
Cache["dsErrMsg"] = dsErrMsg;
}

我把代码贴出来主要部分,但是发现编译可以通过,运行时候抛出异常"Cache is not available"
mywebcom 2004-08-30
  • 打赏
  • 举报
回复
To Eddie005(暴走005) & tianjh(苍穹彗星) :

谢谢你们的思路,这种公共信息类我肯定有了,问题是这些公共信息在项目中的存储和调用就颇费心思。
进一步解释,我的所有的信息都是保存在数据里面,只要我的project 启动之后,一旦需要访问公共信息,第一次肯定要访问数据库,于是我就将它存储到一个application 或者
cache 中,这样,下一次在访问,我就直接读取我要的信息就可以了
tianjh 2004-08-30
  • 打赏
  • 举报
回复
Eddie005(暴走005)
不错,这是一个很好的建议。
不过为了方便一些,写XML文档似乎更好一些。这样在版本控制上比上一个方便些。
zahuifan 2004-08-30
  • 打赏
  • 举报
回复
用 Application 的话很方便,但是其中的键值要像全局变量一样管理,颇为费心。
如果要共享的变量很多的话,最好新建一个类库,在里面建一些单件类,用来保存需共享的信息。
Eddie005 2004-08-30
  • 打赏
  • 举报
回复
楼主这个问题局限了,其实方法多的是,举例一种,把它写成公共信息类也可以呀~
yingshis 2004-08-30
  • 打赏
  • 举报
回复
帮你up
mywebcom 2004-08-30
  • 打赏
  • 举报
回复
我已经揭帖了,怎么看不到效果?
mywebcom 2004-08-30
  • 打赏
  • 举报
回复
其实有些东西,不一定需要完成的答案,更不需要全面的代码,只需要别人的一点点启示或者点播,我们就可以解决很困惑的问题。
mywebcom 2004-08-30
  • 打赏
  • 举报
回复
上面的例子很有帮助,我知道我引用cache 得问题出在那里了:

cache 在好几个命名空间下面都有,在我的page 里面,默认成了 system.web.cache,
其实,这是不对的。
微启子 2004-08-30
  • 打赏
  • 举报
回复
学习!!帮你顶!
zjy118 2004-08-30
  • 打赏
  • 举报
回复
public static string GetFromWebConfig(string p_ValueName , string p_Default)
{
string szValue;
string msg;
object obj;

//可能这里因为没有缓存
try
{
if(HttpContext.Current != null)
{
obj = HttpContext.Current.Cache.Get(p_ValueName);
if( obj == null)
{
HttpContext.Current.Cache.Insert(p_ValueName,"");
szValue = "";
}
else
{
szValue = obj.ToString();
}

if(szValue != "" && szValue != null)
return szValue;
}
szValue = ConfigurationSettings.AppSettings[p_ValueName];
//如果没有得到数据的话,那么使用默认的值
if( szValue == "" || szValue == null )
{
szValue = p_Default;
}

if( HttpContext.Current != null)
{
//将数据进行保存到高速缓冲,以提高速度
HttpContext.Current.Cache[p_ValueName] = szValue;
}
}
catch(Exception ee)
{
msg = ee.Message;
szValue = p_Default;
}
return szValue;
}
}


看看这个怎么样,我是新手,是前辈的代码里有这个东西,不知道是不是你想要的东西

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧