我有一个类在其构造函数中采用IRepository,就像这样......
public class UserService
{
public IRepository<User> _repo { get; set; }
public UserService(IRepository<User> repo = null)
{
_repo = repo ?? new UserRepository();
}
Run Code Online (Sandbox Code Playgroud)
并有一个看起来像这样的方法......
public bool IsUserActive(email string)
{
//The method actually does more but to keep it simple lets imagine
// it does this
User user = _repo.Find(u => u.Email == email).First();
return user.IsActive;
}
Run Code Online (Sandbox Code Playgroud)
IRepository看起来像这样.
public interface IRepository<T> : IDisposable where T : IEntity
{
void InsertOrUpdate(T entity);
void Delete(T entity);
IQueryable<T> Find(Func<T, bool> query);
T …Run Code Online (Sandbox Code Playgroud) 在下面我得到一个编译时错误,如果我输入字符串匹配,则说"使用未分配的局部变量'匹配'"; 但是当我使用string match = null时它可以工作; 那么有什么区别,一般来说,如果没有立即为字符串分配值,我应该像这样分配给null吗?
string question = "Why do I need to assign to null";
char[] delim = { ' ' };
string[] strArr = question.Split(delim);
//Throws Error
string match;
//No Error
//string match = null;
foreach (string s in strArr)
{
if (s == "Why")
{
match = "Why";
}
}
Console.WriteLine(match);
Run Code Online (Sandbox Code Playgroud) 我正在使用Entity Framework Code First,并且有一些像这样的System.Net.Mail.MailAddress类的类.
public class Person
{
public MailAddress Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当EF尝试创建数据库时,我收到此错误
"Problem in mapping fragments starting at line 6 No mapping for properties Person.Email"
Run Code Online (Sandbox Code Playgroud)
如何告诉EF只将Email.ToString()的结果保存到数据库,然后我可以创建一个新的MailAddress(stringOfEmail); 在对象上设置值时?
或者我最好只将电子邮件的数据类型更改为字符串并在其他地方处理验证?
这么简单的问题但是如果我想在OnPreRenderComplete事件中添加一些代码来运行使用Visual Studio Web Developer Express 2010从母版页继承的Asp.Net页面,我该怎么办呢?右键单击aspx页面以获取属性允许我选择下拉列表中的所有Web控件.一旦我选择了一个,我只需单击事件选项卡,然后单击我希望将空方法添加到.cs并为事件注册它的事件.但我没有看到Page的选项.我错过了什么?
使用Asp.Net 4.0 Web窗体和Jquery 1.6.2.我想在页面上对WebMethod进行Ajax调用,并让它返回html.在服务器端,WebMethod看起来像这样.
[WebMethod]
public static string GetOrders()
{
return theMethodThatGeneratesHtml();
}
Run Code Online (Sandbox Code Playgroud)
这是调用Ajax函数.
function GetOrders()
{
$.ajax({
type: 'POST',
contentType: "application/json",
url: "Orders.aspx/GetOrders",
data: "{}",
success: function (data) {
$('#content').html(data);
},
dataType: "text"
});
}
Run Code Online (Sandbox Code Playgroud)
从WebMethod返回的数据总是被包装为以这样开始的json对象.
{"d":"\u003ctable\u003e\r\n ........
Run Code Online (Sandbox Code Playgroud)
如何让WebMethod只返回HTML?