在EF 4.1 RC1中,我有一个简单的实体,比如说Category,属性ID为int.我可以将它作为只读属性并仍然有效吗?
如果没有,你如何保护PK/FK?
我想知道是否可以反序列化可能是对象或数组的JSON对象.
与此问题类似:杰克逊反序列化对象或数组
但是使用JSON.Net.
例
{
"response": {
"status":"success",
// Could be either a single object or an array of objects.
"data": {
"prop":"value"
}
// OR
"data": [
{"prop":"value"},
{"prop":"value"}
]
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的MVC3 HtmlHelper扩展:
public static MvcHtmlString ShowInfoBar(this HtmlHelper helper, string message, InfoMessageType messageType)
{
return MvcHtmlString.Create(String.Format(@"<script type=""text/javascript"">$(document).ready(function () { gtg.core.showInfoBar('{0}', '{1}'}; );</script>", message, messageType.ToString().ToLower()));
}
Run Code Online (Sandbox Code Playgroud)
价值message是"The product "Product Name" was saved successfully."
价值messageType是info.
它一直在说 Input string was not in the correct format.
我卡住了?
我仍然很难绕过这个.我想像我这样分开我的图层(dll):
1)MyProject.Web.dll - MVC Web App(控制器,模型(编辑/查看),视图)
2)MyProject.Services.dll - 服务层(业务逻辑)
3)MyProject.Repositories.dll - 存储库
4)MyProject. Domain.dll - POCO类
5)MyProject.Data.dll - EF4
工作流程:
1)控制器调用服务以获取对象以填充视图/编辑模型.
2)服务调用存储库来获取/持久化对象.
3)存储库调用EF以从SQL Server获取/持久化对象.
我的存储库返回IQueryable(Of T),在其中它们使用ObjectSet(Of T).
所以我看到这一点,图层完全依赖于下一层和包含POCO类的lib?
一些问题:
1)现在我的存储库与EF一起正常工作,它们将依赖于System.Data.Objects,现在我在我的存储库层与EF紧密耦合,那是不是很糟糕?
2)我正在使用UnitOfWork模式.那住哪儿?它有一个Property Context As ObjectContext,因此它也与EF紧密耦合.坏?
3)我如何使用DI使这更容易?
我希望这是一个松散耦合的测试.有什么建议?
----------编辑----------
如果我在这里正确的轨道,请告诉我.另外,因此服务器注入一个IRepository(Of Category)权限,它如何知道它与EFRepository(Of T)的具体类之间的区别?与UnitOfWork和服务相同?
一旦有人帮助我把它弄清楚到我所理解的地方,我知道它看起来似乎微不足道,但是男人,我有一个时间包围我的头!
调节器
Public Class CategoryController
Private _Service As Domain.Interfaces.IService
Public Sub New(ByVal Service As Domain.Interfaces.IService)
_Service = Service
End Sub
Function ListCategories() As ActionResult
Dim Model As New CategoryViewModel
Using UOW As New Repositories.EFUnitOfWork
Mapper.Map(Of Category, CategoryViewModel)(_Service.GetCategories)
End Using
Return …Run Code Online (Sandbox Code Playgroud) asp.net-mvc unit-of-work repository-pattern service-layer entity-framework-4
我在互联网上搜索过,所以我在这个主题上找到了一些好东西,但是我还有一些问题,我仍然不确定:
1)我正在使用自定义身份验证提供程序的表单身份验证.所以我Authorize仍然使用web.config中的属性和部分,但基本上当FormsAuthenticationTicket不存在时,我重定向到登录页面(在web.config中指定),然后利用自定义身份验证提供程序对用户进行身份验证. db然后发出FormsAuthenticationTicket.它是否正确?
2)我应该使用自定义Authorize属性,或者我应该只是注入GenericPrincipal到HttpContext从Application_AuthenticateRequest在Global.asax页事件处理程序?或者我应该使用User.IsInRole控制器操作的insode?
我只需要基于角色的授权,我认为我的身份验证方案非常好.
任何指针/建议?
谢谢,山姆
编辑
所以根据我的阅读,最好的选择是创建一个自定义AuthorizeAttribute并覆盖AuthorizeCore.
所以我做的是这样的:
public class CustomAuthorize : System.Web.Mvc.AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext.User.Identity.IsAuthenticated)
{
var model = AdminUserViewModel.FromJsonString(((FormsIdentity)httpContext.User.Identity).Ticket.UserData);
httpContext.User = new GenericPrincipal(HttpContext.Current.User.Identity, model.SecurityGroups.Select(x => x.Name).ToArray());
}
return base.AuthorizeCore(httpContext);
}
protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
{
//base.HandleUnauthorizedRequest(filterContext);
filterContext.Result = new System.Web.Mvc.RedirectResult("/Authentication/NotAuthorized", false);
}
}
Run Code Online (Sandbox Code Playgroud)
只需使用存储在FormsAuthenticationTicket UserData属性中的角色注入新的主体/标识.然后让基地完成剩下的工作.
这看起来好吗?
编辑#2 …
我需要能够提供IComponentContext给我ValidatorFactory解决FluentValidation Validators.我有点卡住了.
ValidatorFactory
public class ValidatorFactory : ValidatorFactoryBase
{
private readonly IComponentContext context;
public ValidatorFactory(IComponentContext context)
{
this.context = context;
}
public override IValidator CreateInstance(Type validatorType)
{
return context.Resolve(validatorType) as IValidator;
}
}
Run Code Online (Sandbox Code Playgroud)
我如何提供上下文并注册 ValidatorFactory
FluentValidation.Mvc.FluentValidationModelValidatorProvider.Configure(x => x.ValidatorFactory = new ValidatorFactory());
Run Code Online (Sandbox Code Playgroud) 在IIS 7中托管的ASP.NET WebApi中,它是否可以访问会话?它似乎Session是空的HttpContext.Current.
这两个存储全局变量有什么区别?
private static Dictionary<string, string> ConnectionStrings
{
get
{
if (HttpContext.Current.Session["ConnectionStrings"] == null)
HttpContext.Current.Session["ConnectionStrings"] = new Dictionary<string, string>();
return HttpContext.Current.Session["ConnectionStrings"] as Dictionary<string, string>;
}
}
Run Code Online (Sandbox Code Playgroud)
和
private static Dictionary<string, string> connectionStrings = new Dictionary<string, string>();
Run Code Online (Sandbox Code Playgroud)
我应该使用会话或静态变量来存储动态生成的连接字符串(长篇故事)吗?
所以我正在玩我的单元测试的模拟框架(Moq),并且想知道何时应该使用模拟框架?
以下两个测试之间的好处/缺点是什么:
public class Tests
{
[Fact]
public void TestWithMock()
{
// Arrange
var repo = new Mock<IRepository>();
var p = new Mock<Person>();
p.Setup(x => x.Id).Returns(1);
p.Setup(x => x.Name).Returns("Joe Blow");
p.Setup(x => x.AkaNames).Returns(new List<string> { "Joey", "Mugs" });
p.Setup(x => x.AkaNames.Remove(It.IsAny<string>()));
// Act
var service = new Service(repo.Object);
service.RemoveAkaName(p.Object, "Mugs");
// Assert
p.Verify(x => x.AkaNames.Remove("Mugs"), Times.Once());
}
[Fact]
public void TestWithoutMock()
{
// Arrange
var repo = new Mock<IRepository>();
var p = new Person { Id = 1, Name = …Run Code Online (Sandbox Code Playgroud) 尝试将复杂的JSON对象传递给MVC 3中的操作时,我得到了一些奇怪的结果.
将Locations被填充的动作参数模型,但名称和位置都没有.
如果我这样做ko.toJS(testViewModel),那么名称和位置都在那里,但位置是空白的???
我正在使用knockout.js:
var testViewModel = {
Name: ko.observable("Joe Bob"),
Locations: ko.observableArray([
{ ID: 1, Name: "Salem, OR" },
{ ID: 2, Name: "Big Bear Lake, CA" },
{ ID: 3, Name: "Big Bear City, CA" }
]),
Position: ko.observable("Manager")
}
Run Code Online (Sandbox Code Playgroud)
通过jQuery ajax发送它:
$.ajax({
url: "/ClaimsAuthority/Home/TestIt",
type: "POST",
data: ko.toJSON(testViewModel),
success: function (data, status, xhr) {
//ko.applyBindings(data);
}
});
Run Code Online (Sandbox Code Playgroud)
MVC行动:
<HttpPost()>
Public Function TestIt(model As TestModel) As ActionResult
Return Json(model)
End Function
Run Code Online (Sandbox Code Playgroud)
楷模:
Public …Run Code Online (Sandbox Code Playgroud) 所以说我有一个$scope如此定义的变量:
$scope.data = {
filter: {
state: 'WA',
country: 'US'
}
};
Run Code Online (Sandbox Code Playgroud)
如何通过字符串在单独的服务中访问它?例如data.filter,在$scope.
所以说我有这样的服务方法:
function doSomething($scope, variableName) {
// I want to access $scope[variableName] here??
}
Run Code Online (Sandbox Code Playgroud)
我会从控制器中调用它,如下所示:
service.doSomething($scope, 'data.filter');
Run Code Online (Sandbox Code Playgroud) c# ×3
asp.net ×2
asp.net-mvc ×2
json ×2
angularjs ×1
autofac ×1
json.net ×1
mocking ×1
moq ×1
unit-of-work ×1
unit-testing ×1