我正在开发一个 Web 应用程序,它是一个 B2B 门户应用程序。我的应用程序遵循 2 层架构。 下面是一段代码,将公司注册到我的网站
/// <summary>
/// Register Company with the business bazaar
/// </summary>
/// <param name="registration"></param>
/// <returns></returns>
public static bool RegisterCompany(Registration registration)
{
bool result;
using (var helper = new DbHelper())
{
_commandText = "sp_RegisterCompany";
var success = new SqlParameter("@Success", SqlDbType.Bit, 1, ParameterDirection.Output, true, 0, 0,
"Result", DataRowVersion.Default, 0);
var parameters = new[]
{
new SqlParameter("@Name",registration.RegisteredUser.Name),
new SqlParameter("@Designation",registration.Designation ),
new SqlParameter("@Email",registration.RegisteredUser.Email ),
new SqlParameter("@AltEmail",registration.RegisteredUser.AlternateEmail ),
new SqlParameter("@City",registration.City ),
new SqlParameter("@State",registration.State ),
new SqlParameter("@Country",registration.Country ),
new SqlParameter("@Telephone",registration.Telephone ),
new SqlParameter("@Mobile",registration.Mobile ),
new SqlParameter("@CompanyName",registration.CompanyName ),
new SqlParameter("@Website",registration.Website ),
new SqlParameter("@LoginId",registration.RegisteredUser.UserName ),
new SqlParameter("@Password",registration.RegisteredUser.Password ),
success,
};
helper.ExecuteScalar(_commandText, CommandType.StoredProcedure, parameters);
result = (bool) success.Value;
}
return result;
}
我想说的是,我对所有数据层方法都使用静态方法。正如我浏览了网络上的各种文章,指出静态方法比非静态方法具有更多优势。所以我以这种方式设计了我的代码。但是几天前我看到一篇文章说当你为你的类设计一些实用程序时静态方法很有用,否则使用非静态,因为相同的静态对象对其他用户可用。所以我只想弄清楚要遵循哪种方法,静态的还是非静态的。
我正在使用这种格式的类:
public sealed class MyClass
{
private MyClass(){}
public static DataTable GetUserInfoByUserId(int userId)
{
// My datalayer code goes here
}
}
所以我很困惑,如果将上述方法设为静态,将不会使用户 1 的数据可供同时访问应用程序的用户 2 使用。基本上,我想知道这种设计的缺陷。
已更新 下面是我的类(class),展示了我的方法
#region
using System.Data;
using System.Data.SqlClient;
using System;
#endregion
namespace InnovativeTechnosoft.BusinessBazaar.Core
{
public sealed class UserData
{
private static string _commandText = string.Empty;
/// <summary>
/// Takes username and password as input and sets
/// the current user in sessionif the user authenticate
/// successfully
/// </summary>
/// <param name="userName">username as string</param>
/// <param name="password">password as string</param>
/// <returns>datatable</returns>
public static DataTable IsAuthenticated(string userName, string password)
{
DataTable dtResult;
using (var helper = new DbHelper())
{
_commandText = "sp_AuthenticateUsers";
var parameters = new[]
{
new SqlParameter("@username", userName),
new SqlParameter("@password", password),
};
dtResult = helper.ExecuteSelect(_commandText, CommandType.StoredProcedure, parameters);
}
return dtResult;
}
/// <summary>
/// Checks for username if it exists or not
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static bool IsExistingUser(string userName)
{
bool result;
using (var helper = new DbHelper())
{
_commandText = "sp_IsExistingUserName";
var success = new SqlParameter("@Success", SqlDbType.Bit, 1, ParameterDirection.Output, true, 0, 0,
"Result", DataRowVersion.Default, 0);
var parameters = new[]
{
new SqlParameter("@userName", userName),
success,
};
helper.ExecuteScalar(_commandText, CommandType.StoredProcedure, parameters);
result = (bool)success.Value;
}
return result;
}
/// <summary>
/// Register Company with the business bazaar
/// </summary>
/// <param name="registration"></param>
/// <returns></returns>
public static bool RegisterCompany(Registration registration)
{
bool result;
using (var helper = new DbHelper())
{
_commandText = "sp_RegisterCompany";
var success = new SqlParameter("@Success", SqlDbType.Bit, 1, ParameterDirection.Output, true, 0, 0,
"Result", DataRowVersion.Default, 0);
var parameters = new[]
{
new SqlParameter("@Name",registration.RegisteredUser.Name),
new SqlParameter("@Designation",registration.Designation ),
new SqlParameter("@Email",registration.RegisteredUser.Email ),
new SqlParameter("@AltEmail",registration.RegisteredUser.AlternateEmail ),
new SqlParameter("@City",registration.City ),
new SqlParameter("@State",registration.State ),
new SqlParameter("@Country",registration.Country ),
new SqlParameter("@Telephone",registration.Telephone ),
new SqlParameter("@Mobile",registration.Mobile ),
new SqlParameter("@CompanyName",registration.CompanyName ),
new SqlParameter("@Website",registration.Website ),
new SqlParameter("@LoginId",registration.RegisteredUser.UserName ),
new SqlParameter("@Password",registration.RegisteredUser.Password ),
success,
};
helper.ExecuteScalar(_commandText, CommandType.StoredProcedure, parameters);
result = (bool) success.Value;
}
return result;
}
/// <summary>
/// Recovers Password
/// </summary>
/// <param name="email"></param>
/// <param name="password"></param>
/// <returns></returns>
public static bool RecoverPassword(string email, out string password)
{
bool result;
password = string.Empty;
using (var helper = new DbHelper())
{
_commandText = "sp_RecoverPassword";
var success = new SqlParameter("@Success", SqlDbType.Bit, 1, ParameterDirection.Output, true, 0, 0,
"Result", DataRowVersion.Default, 0);
var pwd = new SqlParameter("@Password", SqlDbType.NVarChar, 50, ParameterDirection.Output, true, 0, 0, "Password", DataRowVersion.Default, string.Empty);
var parameters = new[]
{
new SqlParameter("@Email",email ),
success,
};
helper.ExecuteScalar(_commandText, CommandType.StoredProcedure, parameters);
result = (bool)success.Value;
password = Convert.ToString(pwd.Value);
}
return result;
}
/// <summary>
/// Update Password
/// </summary>
/// <param name="email"></param>
/// <param name="password"></param>
/// <returns></returns>
public static bool UpdatePassword(int userId,string password)
{
bool result;
using (var helper = new DbHelper())
{
_commandText = "sp_UpdatePassword";
var success = new SqlParameter("@Success", SqlDbType.Bit, 1, ParameterDirection.Output, true, 0, 0,
"Result", DataRowVersion.Default, 0);
var parameters = new[]
{
new SqlParameter ("@UserId",userId),
new SqlParameter("@Password",password ),
success,
};
helper.ExecuteScalar(_commandText, CommandType.StoredProcedure, parameters);
result = (bool)success.Value;
}
return result;
}
}
}
这将是一个很大的帮助。
问候 阿米特·兰詹
最佳答案
您可以使用静态函数,但您应该完全避免使用静态变量或成员。简而言之,不要在静态上下文中“保存”任何信息。这些在应用范围内可用,并且对于来自不同用户的不同请求可能是相同的。
在静态函数中运行数据访问操作应该没有问题。
关于c# - 在 ASP.Net Web 应用程序数据层中实现静态方法是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6503293/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack