我从互联网上读到了我得到了这一点,其中说接口用于此
但是我无法理解接口在这一点上的用途Replace persistance engine.让我们考虑我正在创建一个基本的(没有泛型)存储库EmployeeRepository
public class EmployeeRepository
{
public employee[] GetAll()
{
//here I'll return from dbContext or ObjectContex class
}
}
Run Code Online (Sandbox Code Playgroud)
界面界面如何形成?
如果假设我创建了一个接口,为什么要使用向上转换?例如
IEmployee emp = new EmployeeRepository() ;
vs
EmployeeRepository emp = new EmployeeRepository();
Run Code Online (Sandbox Code Playgroud)
请详细解释我,以及关于存储库模式的接口的其他有用性.
我写了我自己的动作过滤器并在global.asax文件中注册,现在我的问题是我如何跳过这个过滤器的特定动作,我想通过创建一个自定义属性为例如DontValidate
并将其放在我的动作上想要跳过验证,并在我的动作过滤器代码中,我将提出一个条件,如果操作包含
DontValidate属性,则跳过验证.所以目前我还没有得到如何实现它:
下面的代码是我的验证操作过滤器
public class ValidationActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext context)
{
if (context.Request.Method.ToString() == "OPTIONS") return;
//bool dontValidate = context.ActionDescriptor. // here im stuck how to do
var modelState = context.ModelState;
if (!modelState.IsValid)
{
JsonValue errors = new JsonObject();
foreach (var key in modelState.Keys)
{
// some stuff
}
context.Response = context.Request.CreateResponse<JsonValue>(HttpStatusCode.BadRequest, errors);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在左边加入linq到sql,所以我的问题是在选择正确的表字段时,我正在检查每个字段,而连接对象是否为空,这是正确的方法吗?或者还有其他方法吗?我的查询就像
from u in user
join x in employeee on u.id equals x.userId
into ux from ujoinx in ux.DefaultIfEmpty()
join y in department on x.id equals y.employeeId
into xy from xjoiny in xy.DefaultIfEmpty()
select new {
EmployeeSal = ujoinx!=null?ujoinx.employeeSal:0, // see checkig for null
EmployeeTax = ujoinx!=null?ujoinx.employeeTax:0, // in this 3 lines
UserName = u.username,
DeptName = xjoiny!=null?xjoiny.name:"" //is this a correct way ?
}
Run Code Online (Sandbox Code Playgroud)
查询得到了正确的答案,但如果我不检查那些几个字段为null抛出它object reference not set.....error.这到底是DefaultIfEmpty()什么?