过去几天我一直在学习C#,用于ASP.NET创建网站.
我对C#很新,但我一直在考虑如何编写代码以使其尽可能可重用.
作为一个简单的例子,假设我想创建一段代码来检查用户登录详细信息,我可以随时将其放入另一个站点,并使其与所获得的数据一起使用.
记住我不知道我应该如何布置我的代码来实现这一点,这是我提出的想法(我会用某种伪代码保持简短):
首先我创建一个类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Module {
public class Login {
public static bool check_login(string usernameCheck = "", string passwordCheck = "") {
if(usernameCheck == "user" && passwordCheck == "password") {
return true;
}
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我会有一个登录表单的aspx页面,例如:
<asp:Content ContentPlaceHolderID="column1" runat="server">
<asp:TextBox ID="usernameInput" runat="server"></asp:TextBox>
<asp:TextBox ID="passwordInput" runat="server"></asp:TextBox>
<asp:Button OnClick="check_login" Text="Login" runat="server" />
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
并且文件背后的代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Module { …Run Code Online (Sandbox Code Playgroud) 请...任何帮助将不胜感激!
这是我的代码:
static void Main(string[] args)
{
IPEndPoint localpt = new IPEndPoint(IPAddress.Any, 6000);
UdpClient udpServer = new UdpClient(localpt);
udpServer.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
UdpClient udpServer2 = new UdpClient();
udpServer2.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer2.Client.Bind(localpt); // <<---------- Exception here
}
Run Code Online (Sandbox Code Playgroud) 虽然我的问题可能看似抽象,但我希望不是.假设我开发了一个应用程序,一个ASP.NET MVC站点,后来我的任务是为这个应用程序构建一个Winforms客户端,从现有应用程序中重用多少以及如何重用?
我定义了模型,我定义了控制器和视图.他们都运作良好.
现在老板要求Winforms客户端,我希望我可以重用模型和控制器(假设我把它们放在不同的程序集中)而不是只重用视图(ASPX视图).
可以这样做吗?怎么样?
ViewHolder模式改进了ListView滚动帧率,如下例所示:https: //developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
是否可以在为不同行使用不同类型的视图时保留此模式?
换句话说,是否可以执行以下操作:
public View getView(int position, View view, ViewGroup parent) {
// calculate viewID here
if (view == null || *view is not null but was created from different XML than viewID* ) {
view = mInflater.inflate(viewId, null);
Run Code Online (Sandbox Code Playgroud) 问这么简单的问题我觉得有些愚蠢,但我似乎无法找到答案.我是ASP.NET(C#)的新手,但我正在学习构建一组显示报告的简单网页.我有一个代表公司名称的变量.我需要在网页上的多个位置输出此变量.我发现输出变量的唯一方法是:
company_name.Text = "Acme Windows";
Run Code Online (Sandbox Code Playgroud)
然后
<asp:literal id="company_name" runat="server" />
Run Code Online (Sandbox Code Playgroud)
我的问题是我想在页面的多个地方使用company_name.我是否真的必须创建一个单独的变量,每次在页面上放置相同的值?如果我只是将上面的XML代码复制到我想要显示变量的所有位置,那么它显然会产生编译错误,因为已经定义了该ID.
我觉得我错过了一些非常明显的东西.
我想将一个对象集合添加到arrayList,只有当特定属性不为null时.
我正在考虑扩展ArrayList并在子类中实现检查.
另一种方法是在将它放入Arraylist之前检查属性,但这意味着,如果我需要根据逻辑将对象添加到arraylist,我必须分散if检查每个位置.
我想知道你对它的看法......再想一想,这是一种矫枉过正吗?
鉴于这一块HTML:
<div id="email_field" class="control-group">
<label class="control-label" for="account.email">Email</label>
<div id="email_input" class="controls">
<input id="account.email" name="account.email" type="text" placeholder="jpublic@example.com">
<span class="help-block">We just need a valid email address.</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
对于我想要的任何属性,如何将其转换为可重用的部分?IE:电子邮件,密码,密码确认等
我会假设某种视图层次结构,但我不太确定.
编辑:经过进一步的探索,我已经淘汰了{{view}},{{render}}并弄清楚我需要什么:
我想:1.使用特定的视图(InputView)2.使用特定的控制器(最好类似命名:InputController)({{view}}我认为不这样做)3.能够多次使用({{render}}不能这样做) )4.能够传入值({{render}}不能这样做)
例:
<!-- templates/application.hbs -->
{{foo "input" name="Email" id="account.email" placeholder="jpublic@email.com"}}
Run Code Online (Sandbox Code Playgroud)
// controllers/input.js
Application.InputController = Ember.ObjectController.extend({
type: "text"
});
Run Code Online (Sandbox Code Playgroud)
// views/input.js
Application.InputView = Ember.View.extend({
templateName: "form/input"
});
Run Code Online (Sandbox Code Playgroud)
<!-- templates/form/input.hbs -->
<input {{bindAttr id="id" name="name" type="type" placeholder="placeholder"}}>
Run Code Online (Sandbox Code Playgroud) 看到这个:http://www.suckless.org/wiki.html.基于Mercurial的维基.是否还有其他与代码无关的版本控制用途?是否还有其他项目使用版本控制工具而不是编写自己的特定解决方案?
这段代码:
Then %{I should see the following data in the "Feeds" data grid:
| Name |
| #{name} |}
Run Code Online (Sandbox Code Playgroud)
还有这个:
Then "I should see the following data in the \"Feeds\" data grid:
| Name |
| #{name} |"
Run Code Online (Sandbox Code Playgroud)
还有这个:
Then "I should see the following data in the \"Feeds\" data grid:\n| Name |\n| #{name} |"
Run Code Online (Sandbox Code Playgroud)
甚至这个:
Then <<EOS
I should see the following data in the "Feeds" data grid:
| Name |
| #{name} |
EOS
Run Code Online (Sandbox Code Playgroud)
给我:
Your block takes …Run Code Online (Sandbox Code Playgroud) 我正在iPhone上使用NSURLConnection实例来请求服务器上的数据,由代理人照常管理.请求非常频繁(可能每2分钟说一次)并且有一个共同的固定URL.而不是在每次下载后发现NSURLConnection的良好实例,然后创建一个新的实例:
保留第一个连接并重新使用它有什么价值吗?(我希望如此,一个好的身份验证应该值一千.)
如果是这样,我该如何重复使用它?文档中的突出方法是,-start但是当在已经使用过的(并且非零)NSURLConnection实例上调用时,这似乎会使应用程序崩溃.[文档确实说-start"导致接收者开始加载数据,如果还没有."]
如果它对上述问题有帮助,我(是!)提议:
if (connection_ == nil)
{
connection_ = [NSURLConnection connectionWithRequest:request
delegate:self];
}
else
{
[connection_ start];
}
Run Code Online (Sandbox Code Playgroud) reusability ×10
c# ×2
.net ×1
abstraction ×1
android ×1
arraylist ×1
asp.net ×1
asp.net-mvc ×1
cucumber ×1
ember.js ×1
iphone ×1
java ×1
listadapter ×1
listview ×1
performance ×1
port ×1
refactoring ×1
templates ×1
udpclient ×1
winforms ×1