我使用.Net开发了多年的企业应用程序我的应用程序通常有一个包含映射到SQL DB表的实体的域模型.我使用Repository模式,依赖注入和服务层.
最近我们开始研究MVC 3项目,我们讨论了在哪里放置哪些逻辑.我来自瘦控制器/ FAT模型架构,并想知道服务层如何适应
控制器很薄,调用模型上的方法.模型"知道"如何从数据库加载自己并与存储库或服务进行通信.例如,customerModel有一个Load(id)方法,并加载客户和一些子对象,如GetContracts().
Controller要求服务检索模型对象.加载/存储等逻辑在服务层中.该模型是仅具有数据的纯实体模型.
为什么选项1会成为更好的选择,特别是当我们谈论企业应用时,我的经验告诉我要分离问题,让模型和控制器尽可能地薄,并且有专门的服务来做业务逻辑(imcl.数据库交互)
感谢所有建议和对优质资源的参考.
architecture model-view-controller asp.net-mvc service-layer asp.net-mvc-3
在.Net 4.0上的C#的上下文中,是否有任何内置对象可以实现IQueryable<T>
?
DB有一张桌子PackagingInfo
.我有一Package
节课,还有一节课ShopEntities : DbContext
.
// Entity (ex. Package.cs)
[Table("PackagingInfo")]
public class Package
{
public decimal PackageID { get; set; }
public decimal Title { get; set; }
public decimal Cost { get; set; }
public bool isFree { get; set; }
}
// Entity Context (ex. ShopEntities.cs)
public class ShopEntities : DbContext
{
public DbSet<Package> Packages { get; set; }
}
// Controller Action (ex. HomeController.cs)
public ActionResult Index()
{
ShopEntities _db = new ShopEntities(); …
Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,但我试图ViewBag
像这样填充一个匿名对象:
ViewBag.Stuff = new { Name = "Test", Email = "user@domain.com" };
Run Code Online (Sandbox Code Playgroud)
并从这样的视图访问它:
@ ViewBag.Stuff.Name
我理解ViewBag是动态的,"Stuff"是一个匿名对象...但是当我从上面的View行看调试器时,我可以看到具有正确值的所有属性.为什么模型活页夹有这么难?
有没有一个很好的方法来实现这个而不创建一个模型类?我想继续使用new {}
这让我很困惑.它一定是我看不到的小东西.我正试图observableArray
用ajax调用加载一个非常简单的淘汰赛.
JavaScript的
// we bind the array to the view model property with an empty array.
var data = [];
var viewModel = {
vendors: ko.observableArray(data)
};
ko.applyBindings(viewModel);
$(function () {
// on this click event, we popular the observable array
$('#load').click(function () {
// WORKS. Html is updated appropriately.
viewModel.vendors([{ "Id": "01" },{ "Id": "02" },{ "Id": "03" }]);
// DOES NOT WORK. Fiddler2 shows the same exact json string come back
// as in the example …
Run Code Online (Sandbox Code Playgroud) 在Visual Studio 2013的"One ASP.NET"集合中集成项目模板.
"待分发"项目始于一个新的ASP.NET Web应用程序项目,使用Empty模板和MVC和Web API"文件夹和引用"进行检查.从那里我添加了我的爱和感情,并使用了导出模板.拉链落地My Documents > Visual Studio 2013 > My Exported Templates
.如果您有Visual Studio为您安装模板,则会在其中放置一个重复的zip My Documents > Visual Studio 2013 > Templates > ProjectTemplates
.这是问题的开始......
在Visual Studio中的模板是内发现的Visual C#树枝,而不是一个自定义模板中的ASP.NET Web应用程序模板.游民.更奇怪的是,模板仅安装到C#而不安装到其Web子文件夹.
MSDN在线文档提到<ProjectSubType>
的内.vstemplate
文件,当我调查了这是空的,但即使明确添加Web并未改变UI位置.这与我的目标无关,但似乎暗示VS2013不尊重其中的一些XML .vstemplate
?
我开始挖掘Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ProjectTemplates\
其中一个奇怪的东西,我发现它是两个独立的"文件夹树":{root}\Web\CSharp\1033
和{root}\CSharp\Web\1033
.
第一棵树持有WebTemplate45,其项目WebApplication45Dev12.vstemplate
说明了上面提到的值ProjectType
和ProjectSubtype
值.模仿这并没有改变任何东西. …
c# asp.net visual-studio visual-studio-templates visual-studio-2013
我尽可能多地阅读了这些答案,但它们似乎没有达到一个细节.
问题是当动作过滤器(由控制器注入的服务)绑定到相应的属性时,我一直无法弄清楚如何将参数/属性值从属性传递到其绑定过滤器.下面是代码,下面是我想要的假代码:
过滤器和属性
public class AuthorizationFilter : IAuthorizationFilter
{
private readonly IAuthorizationService _authorizationService;
private readonly UserRoles _requiredRoles; // Enum
public AuthorizationFilter(IAuthorizationService authorizationService, UserRoles requiredRoles)
{
_authorizationService = authorizationService;
_requiredRoles = requiredRoles;
}
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Session == null)
HandleUnauthorizedRequest(filterContext);
else {
var authorized = _authorizationService.IsUserInRole((UserSessionInfoViewModel) filterContext.HttpContext.Session["user"], _requiredRoles);
if (!authorized)
HandleUnauthorizedRequest(filterContext);
// else TODO: deal with cache...
}
}
}
public class RequireRolesAttribute : FilterAttribute
{
public readonly UserRoles RequiredRoles;
public RequireRolesAttribute(UserRoles requiredRoles)
{
RequiredRoles = requiredRoles;
} …
Run Code Online (Sandbox Code Playgroud) attributes dependency-injection ninject filter asp.net-mvc-3
我准备在ASP.Net MVC 3+ Web应用程序(服务器端MVC)中实现Backbone.js UI(客户端MVC).
我已经阅读了大量的Backbone教程,但没有一篇说明该技术在ASP.Net MVC场景中如何有用或可能没用.
结合这两个部分时我应该遵循什么策略?
ASP.Net MVC4 + Bootstrap(LESS)+ dotLess.
目标是将Bootstrap .less
文件转换为单个捆绑包.css
,并且我遇到了showstopper问题.
var bootstrapStyles = new Bundle("~/bundle/style/bootstrap").Include("~/Content/less/*.less");
bootstrapStyles.Transforms.Add(new LessTransform());
bootstrapStyles.Transforms.Add(new CssMinify());
bundles.Add(bootstrapStyles);
Run Code Online (Sandbox Code Playgroud)
只有bootstrap的文件较少,不应该有任何红旗语法问题.
下一步是构建该变换器类LessTransform
以生成css.
变换器类实现了Process()
内部存在的问题代码......这里是两个场景及其问题:
Less.Parse()
var parsedLess = Less.Parse(bundle.Content);
bundle.Content = parsedLess;
// Throws a FileNotFoundException
// "You are importing a file ending in .less that cannot be found."
// reset.less and it definitely exists in that folder.
Run Code Online (Sandbox Code Playgroud)
var content = new StringBuilder();
var engine = new LessEngine();
foreach (var file in bundle.Files)
{ …
Run Code Online (Sandbox Code Playgroud) c# ×3
asp.net ×2
asp.net-mvc ×2
javascript ×2
.net ×1
ajax ×1
architecture ×1
attributes ×1
backbone.js ×1
bdd ×1
c#-4.0 ×1
css ×1
filter ×1
frameworks ×1
interface ×1
iqueryable ×1
jquery ×1
json ×1
knockout.js ×1
less ×1
linq ×1
ninject ×1
parsing ×1
reflection ×1
rest ×1
types ×1
viewbag ×1