isx*_*ker 6 c# static constructor
public class SharedCacheData : ISharedCacheData
{
private readonly ISharedPetsService mPetsService;
private static ISharedEmployeeService mSharedEmployeeService;
public SharedCacheData(ISharedPetsService petsService, ISharedEmployeeService employeeService)
{
mPetsService = petsService;
mSharedEmployeeService = employeeService;
}
public static string GetUserFullName(string key)
{
Hashtable value = HttpContext.Current.Application[USERFULLNAME_KEY] as Hashtable;
if (value == null)
{
value = new Hashtable();
HttpContext.Current.Application[USERFULLNAME_KEY] = value;
}
if (value.ContainsKey(key))
return value[key] as string;
string name = mSharedEmployeeService.FindOneUser(key);
value[key] = name;
return name;
}
Run Code Online (Sandbox Code Playgroud)
是否在构造函数中初始化静态字段mSharedEmployeeService(service)?
Jon*_*eet 11
不,这通常是一个坏主意.每次创建新实例时,它都将替换静态变量.这几乎肯定不是你想要做的.
我不知道是否确实这里应该有两个类-一个用于宠物的服务和一个为员工服务.从提供的代码中很难说出来,但我们在这里看到的并不是一个好主意.
在静态构造函数中初始化它会更有意义:
http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx
class SimpleClass
{
// Static constructor
static SimpleClass()
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7304 次 |
| 最近记录: |