您的位置:首页 > Web前端 > JQuery

基于JQuery、WebApi的ASP.NET MVC插件的代码生成项目主要技术解析

2013-03-02 21:08 1061 查看

基于JQuery、WebApi的ASP.NET MVC插件的代码生成项目主要技术解析

YbSoftwareFactory目前已可快速生成ASP.NET WebForm、MVC、WinForm和WPF的解决方案源代码,所生成的源代码可直接在VS中打开并运行。终端用户还可自行二次开发自己所需的插件。本章将对ASP.NET MVC代码生成插件所生成项目的主要特点和技术进行解析,其Demo地址:http://mvcdemo.yellbuy.com/

主要技术特点:

一、数据字典的排序

有童鞋问到数据字典的排序是如何实现的,其实数据字典的排序比较简单。首先,需要定义一个排序字段,并根据这个字段升序排列。如果要优先显示某个字典项,则需减小该排序字段的值。如果该排序字段再首位,则无需操作,第二位,则为第一位的排序值小1000,其他设置为前面两项的值的平均数。反之亦然~

在所生成的解决方案中,使用了通用的Api组件,你只需配置好主代码即可。

YbApiAuthorizeAttribute

1 public class YbApiAuthorizeAttribute : System.Web.Http.AuthorizeAttribute
2     {
3         private const string BasicAuthResponseHeader = "WWW-Authenticate";
4         private const string BasicAuthResponseHeaderValue = "Basic";
5         public string PermissionKey { get; set; }
6         public override void OnAuthorization(HttpActionContext actionContext)
7         {
8             if (actionContext == null)
9                 throw new ArgumentNullException("actionContext");
10             if (AuthorizationDisabled(actionContext) || AuthorizeRequest(actionContext.ControllerContext.Request))
11                 return;
12             this.HandleUnauthorizedRequest(actionContext);
13         }
14
15         protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
16         {
17             if (actionContext == null)
18                 throw new ArgumentNullException("actionContext");
19             actionContext.Response = CreateUnauthorizedResponse(actionContext.ControllerContext.Request);
20         }
21
22         private HttpResponseMessage CreateUnauthorizedResponse(HttpRequestMessage request)
23         {
24             var result = new HttpResponseMessage()
25                 {
26                     StatusCode = HttpStatusCode.Unauthorized,
27                     RequestMessage = request
28                 };
29             //we need to include WWW-Authenticate header in our response,
30             //so our client knows we are using HTTP authentication
31             result.Headers.Add(BasicAuthResponseHeader, BasicAuthResponseHeaderValue);
32             return result;
33         }
34
35         private static bool AuthorizationDisabled(HttpActionContext actionContext)
36         {
37             //support new AllowAnonymousAttribute
38             if (!actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any())
39                 return actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any();
40             return true;
41         }
42
43         private bool AuthorizeRequest(HttpRequestMessage request)
44         {
45             //匿名用户的权限验证
46             if (!HttpContext.Current.User.Identity.IsAuthenticated)
47             {
48                 var permissionsOfEveryOne = Permissions.GetPermissionsInRole("EveryOne");
49                 return CheckPermission(request, permissionsOfEveryOne);
50             }
51             //未设置权限Key,则任何用户均可访问
52             if (string.IsNullOrWhiteSpace(PermissionKey)) return true;
53
54             //登录用户的权限验证
55             var allowPermissions = Permissions.GetPermissionsForUser();
56             return CheckPermission(request, allowPermissions);
57         }
58
59         private bool CheckPermission(HttpRequestMessage request, Permission[] permissionsOfEveryOne)
60         {
61             if (permissionsOfEveryOne == null || permissionsOfEveryOne.Length == 0)
62                 return false;
63             var permissionKeys = permissionsOfEveryOne.Select(c => c.PermissionKey);
64             if (request.Method.Method.Equals("GET", StringComparison.OrdinalIgnoreCase))
65                 return permissionKeys.Contains(PermissionKey.ToLower());
66             if (request.Method.Method.Equals("PUT", StringComparison.OrdinalIgnoreCase))
67                 return permissionKeys.Contains(string.Format("{0}.add", PermissionKey.ToLower()));
68             if (request.Method.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
69                 return permissionKeys.Contains(string.Format("{0}.edit", PermissionKey.ToLower()));
70             if (request.Method.Method.Equals("DELETE", StringComparison.OrdinalIgnoreCase))
71                 return permissionKeys.Contains(string.Format("{0}.delete", PermissionKey.ToLower()));
72             return false;
73         }
74
75         protected string[] RolesSplit
76         {
77             get { return SplitStrings(Roles); }
78         }
79
80         protected string[] UsersSplit
81         {
82             get { return SplitStrings(Users); }
83         }
84
85         protected static string[] SplitStrings(string input)
86         {
87             if (string.IsNullOrWhiteSpace(input)) return new string[0];
88             var result = input.Split(',').Where(s => !String.IsNullOrWhiteSpace(s.Trim()));
89             return result.Select(s => s.Trim()).ToArray();
90         }
91
92        /// <summary>
93         /// Implement to include authentication logic and create IPrincipal
94        /// </summary>
95         protected bool TryCreatePrincipal(string user, string password, out IPrincipal principal)
96         {
97             principal = null;
98             if (!Membership.ValidateUser(user, password))
99                 return false;
100             string[] roles = System.Web.Security.Roles.Provider.GetRolesForUser(user);
101             principal = new GenericPrincipal(new GenericIdentity(user), roles);
102             return true;
103         }
104     }


当然,你也需要在具体的Controller上添加相应的Attribute标记,例如:[YbApiAuthorize(PermissionKey = "Categories")]

四、实体对象的验证

实体对象的验证使用Fluent Validator组件进行验证,YbSoftWareFactory自动生成基本的客户端和服务器端的验证代码(如非空检查,字符串长度检查等),所生成的服务器端的验证代码示例如下:




public class CustomersValidator : AbstractValidator<Customers>
{
public CustomersValidator()
{
//此处添加验证逻辑
RuleFor(x => x.CustomerID).NotEmpty().WithMessage("必须输入标识");
RuleFor(x => x.CompanyName).NotEmpty().WithMessage("必须输入公司名");
RuleFor(x => x.ContactName).Length(30).WithMessage("联系人最多只能输入30个字符");
RuleFor(x => x.ContactTitle).Length(30).WithMessage("职务最多只能输入30个字符");
RuleFor(x => x.Address).Length(60).WithMessage("地址最多只能输入60个字符");
RuleFor(x => x.City).Length(15).WithMessage("市最多只能输入15个字符");
RuleFor(x => x.Region).Length(15).WithMessage("省/自治区最多只能输入15个字符");
RuleFor(x => x.PostalCode).Length(10).WithMessage("邮编最多只能输入10个字符");
RuleFor(x => x.Country).Length(15).WithMessage("国家最多只能输入15个字符");
RuleFor(x => x.Phone).Length(24).WithMessage("电话最多只能输入24个字符");
RuleFor(x => x.Fax).Length(24).WithMessage("传真最多只能输入24个字符");
}
}





五、日志

为方便调试和错误诊断,系统集成long4net日志组件,所有异常信息均输出至相应的日志文件中。

有童鞋问YbSoftwareFactory怎么使用,好用不?(见过很多人是一根筋,自己把自己搞得太累~)其实使用YbSoftwareFactory生成ASP.NET MVC项目的源代码超级简单,仅需三步:

第一步:运行YbSoftwareFactory并连接数据库





第二步:编辑元数据信息,以便生成的界面更加友好





第三步:点击“解决方案”按钮生成源代码





短暂的等待后,在VS中打开所生成的源码解决方案,然后就是运行它....



注:本插件已正式发布,需要插件和代码的请和我联系。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐