当前位置:  编程技术>.net/c#/asp.net
本页文章导读:
    ▪Entity Framework 复杂类型      为了说明什么是复杂属性,先举一个例子。 public class CompanyAddress { public int ID { get; set; } public string CompanyName { get; set; } public string StreetAddress { get; set; } public string City { ge.........
    ▪巧用特性减少代码书写量。      您还在为每次添加数据或者修改数据的时候,书写大量的代码判断输入是否合法而发愁吗?如果是,我这里可以为您提供一个好的解决方案。先看我列出的一个实体类例子:using System;using Syste.........
    ▪DYRuntime基于iis express7.5的局域网asp.net4.0服务器      最后要发布基于silverlight的产品。其中一种解决方案是要让客户自己直接配置服务器。而且服务器还很可能是xp,由于是局域网应用性能的问题我们可以不作计较。目标:1.尽可能简单的让使用.........

[1]Entity Framework 复杂类型
    来源:    发布时间: 2013-10-28

为了说明什么是复杂属性,先举一个例子。

public class CompanyAddress
{
public int ID { get; set; }
public string CompanyName { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}

public class FamilyAddress
{
public int ID { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}

上面有两个类:公司地址和家庭地址,它们有四个相同的属性:StreetAddress、City、State、ZipCode。映射到数据库中的结构如图:

这里,我们可以将这四个属性集合成一个复杂属性Address,修改后的类为:

public class CompanyAddress
{
public int ID { get; set; }
public string CompanyName { get; set; }
public Address Address { get; set; }
}

public class FamilyAddress
{
public int ID { get; set; }
public Address Address { get; set; }
}

[ComplexType]
public class Address
{
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}

此时,所生成的数据库如图:

可以看到,两张表中仍然具有相应的地址属性信息。代码中的Address类就是复杂属性,它并不会在数据库中映射成相应的表,但我们的代码确简洁了许多。

所以如果有几个属性在几个类中都有用到,那么就可以将这几个属性集合成一个复杂类型,并在相应的类中增加这个复杂类型的属性。

本文链接


    
[2]巧用特性减少代码书写量。
    来源:    发布时间: 2013-10-28

您还在为每次添加数据或者修改数据的时候,书写大量的代码判断输入是否合法而发愁吗?

如果是,我这里可以为您提供一个好的解决方案。先看我列出的一个实体类例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Attributes;
using System.Data;

namespace Model
{
public class cms_article
{
[Model(Name = "ID", Empty = false, DataType = DbType.Int32, ErrorTip = "ID不能为空,且必须为整数", AutoIncrement = true, PrimaryKey = true)]
public int ArticleID { get; set; }
[Model(Name = "标题", Empty = false, DataType = DbType.String, MinLength = 2, MaxLength = 5, ErrorTip = "请为标题输入2-5个字符,谢谢!")]
public string ArticleTitle { get; set; }
[Model(Name = "内容", Empty = false, DataType = DbType.String, Rule = @"^[\s\S]+$", ErrorTip = "内容不能为空")]
public string ArticleContent { get; set; }
[Model(Name = "发布日期", Empty = true, DataType = DbType.DateTime)]
public DateTime ArticleDateTime { get; set; }
[Model(Name = "所属类别", Empty = false, DataType = DbType.Int32)]
public int ClassID { get; set; }
}
}

看到这,知道我在唱哪出戏了吧?您可能会想到:特性(attribute)。对,就是它。我们可以巧用特性来处理大量的输入判断。如果您感兴趣,继续往下看吧。

有人也许会说,没必要,我在客户端用JS判断就行了。大哥,省省吧。如果网站是面向互联网的,您仅仅用JS判断用户的输入,无疑是自掘坟墓。

******************************************************************************************

我们为什么要这么做?

试想,如果一个系统有几十个,甚至几百个表。您在进行添加或者修改的时候是不是要对用户的输入进行合法性的判断?那是不是意味着每个Add()或者Update()方法里面都要写很多判断代码??天呢,请原谅我,我没有这么多时间来浪费我的青春了。。。那我们应该怎么做呢?这就是我们要解决的问题,也是我写这篇文章的目的。

明白了我们要解决什么问题,下面的事情就好办了。

我们如何做:

需要说明的是:您必须具备C#反射的知识,不然您是看不下去的。

还有,假设数据库字段跟网页的form表单元素是对应的,如果您不喜欢这么做,建议您不要往下看下去了,因为我觉得这是在浪费您的时间。

好,开始。我们先在大脑里,模拟用户输入数据,然后提交,然后在这里我们就要绑定Model了。

 

我们可以这么做:

//绑定Model
Model.cms_article modelArticle = Lib.Model.Instance().BindModel<Model.cms_article>();

Model.cms_article是一个类型,比如:文章。BindModel是个静态方法。

然后看BindModel方法:

/// <summary>
/// 绑定Model
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T BindModel<T>() where T : new()
{
T entity = new T();
PropertyInfo[] Properties = typeof(T).GetProperties();//获取当前类型的所有属性
//遍历属性集合
foreach (PropertyInfo Property in Properties)
{
//获取属性的值
string value = System.Web.HttpContext.Current.Request[Property.Name];
//获取当前属性所指定的特性
object[] attributes = Property.GetCustomAttributes(typeof(Attributes.ModelAttribute), false);
if (attributes.Length > 0)
{
this.modelCheck.CheckInput(attributes[0] as Attributes.ModelAttribute, value);
}
//给实体赋值
if (value != null)
{
Property.SetValue(entity, Convert.ChangeType(value, (System.Nullable.GetUnderlyingType(Property.PropertyType) ?? Property.PropertyType)), null);
}
}
return entity;
}

先是根据反射获取Model的所有属性,遍历循环

然后根据属性获取其所拥有的特性,如果当前属性找到有特性存在,我们则遍历所有特性。

在这里我不需要查找所有的特性,因为我知道我需要什么特性,所以我就取第一个特性attributes[0] as ModelAttribute。

再然后根据当前属性所拥有的特性,对输入的数据进行判断。this.modelCheck.CheckInput(attributes[0] as Attributes.ModelAttribute, value);

/// <summary>
/// 检测输入数据的合法性,统一在这里处理
/// </summary>
/// <param name="modelAttribute"></param>
/// <param name="value"></param>
/// <returns></returns>
public string CheckInput(Attributes.ModelAttribute modelAttribute, string value)
{
string str = string.Empty;
//判断是否允许为空,如果不允许为空
if (!modelAttribute.Empty)
{
this.CheckEmpty(modelAttribute, value);
this.CheckType(modelAttribute, value);
this.CheckLength(modelAttribute, value);
this.CheckRule(modelAttribute, value);
}
else
{
if (!string.IsNullOrEmpty(value))
{
this.CheckType(modelAttribute, value);
this.CheckLength(modelAttribute, value);
this.CheckRule(modelAttribute, value);
}
}
return str;
}

最后是利用我们所约定的规则进行判断。

/// <summary>
/// 检测正则表达式
/// </summary>
/// <param name="modelAttribute"></param>
/// <param name="value"></param>
private void CheckRule(Attributes.ModelAttribute modelAttribute, string value)
{
if (!string.IsNullOrEmpty(modelAttribute.Rule))
{
if (!Common.RegexMatch(value, modelAttribute.Rule))
{
Javascript.Alert(modelAttribute.ErrorTip, true);
}
}
}
    
[3]DYRuntime基于iis express7.5的局域网asp.net4.0服务器
    来源:    发布时间: 2013-10-28

最后要发布基于silverlight的产品。其中一种解决方案是要让客户自己直接配置服务器。而且服务器还很可能是xp,由于是局域网应用性能的问题我们可以不作计较。

目标:

1.尽可能简单的让使用者直接运行到指定的基于.net4.0的web程序。

2.必须可以让局域网内任意端终正常访问。

为解决这两个问题我首先试过Visual studio development server。的确可以运行.net4.0的web程序。但很可惜它是绝对不支持局域网让运行的。其它还有试很多各种网络的所谓微型服务器。全部大多数是基于VSDS这样技术的。只能本机运行。不能让内网访问。最后使用iis express 7.5解决了这两个问题。

概述

IIS 7.5 Express 兼具 IIS 7.5 的强大功能与轻型 Web 服务器(例如 ASP.NET 开发服务器,也称为“Cassini”)的便利,可以增强在 Windows 上开发和测试 Web 应用程序的能力。 Microsoft WebMatrix 中包含 IIS 7.5 Express,这套集成工具可以让 Windows 上的 Web 应用程序开发工作变得简单、顺畅。 IIS 7.5 Express 也可与 Visual Studio 2010 一起使用,功能强大足以代替 Cassini。 使用 IIS 7.5 Express 的好处包括:

  • 在生产服务器上运行的同一 Web 服务器现在可以在开发计算机上使用。
  • 在无需管理员特权的情况下,可以完成大多数任务。
  • IIS 7.5 Express 在 Windows XP 和所有更高版本的 Windows 上运行。
  • 多位用户可在相同的计算机上独立工作。

    系统要求

    支持的操作系统: Windows 7, Windows Server 2003, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP

    • 运行依赖 .NET Framework 4.0

它是可以完成目标的两个要求。但整过配置和运行过程是很不友好的。

1.必须知道iis express的配置文件各个关键属性的设置。

2.启动iis express要通过命令行指令。

基本以上两点内Cool超人还是觉得如果让我们产品的客户这样操作简直就是世界未日。经几两天的研究终于写出一个程序让用户直接简单设置然后即可运行iis express到内网中。

以下让我们看看主界面。

1.运行依赖.net framework4.0 /注意并不是profile

2.支持xp,32bit/64bit系统。还支持win7。更多系统我并没有测试。欢迎各位使用后回复本博客告知内Cool超人。谢谢

以下是使用步骤:

1.运行前先确保您的系统已经安装.net framework4,下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=17718

2.如果第一次运行,请在启动服务前先安装启动服务按扭下方的“安装运行依赖组件”按扭。它会帮运行iis express安装程序。安装过程所有是“下一步”直至完成。

3.确保(内网IP),(端口)项要正确填写,如果你不懂这些程序会自动取得。

4.程序目录,此项是指定web程序目录。通过"…”按扭可能设置web程序目录。

5.确保开放防火墙开端相应端口的网络访问权。

6.启动服务

运行后的说明:

5.1 左下角出现此图标说明iis express已经正常运行.

5.2 右键5.1图标。我们可以看到iis express已经在行运的相应web服务。选中后会自动浏览器中打开。

最后本软件使用过程中的一些情况:

1.由于合用了很多cmd指令来操作iis express。所以有些杀毒软件会误以为是木马或病毒。

2.此软件授权为永久免费使用。你可以用于任何商业或非商业的解决方案中。

下载:

下载本软件>>>>>>>>>>> 下载DYRunTime

本文链接


    
最新技术文章:
▪C#通过IComparable实现ListT.sort()排序
▪C#实现对Json字符串处理实例
▪Winform实现抓取web页面内容的方法
▪Winform实现将网页生成图片的方法
▪C#控制台程序中处理2个关闭事件的代码实例
▪WinForm实现同时让两个窗体有激活效果的特效...
▪WinForm实现拦截窗体上各个部位的点击特效实...
▪用C#的params关键字实现方法形参个数可变示例
▪C#判断某程序是否运行的方法
▪C#验证码识别基础方法实例分析
▪C#控制台程序中处理2个关闭事件的代码实例 iis7站长之家
▪C#实现获取鼠标句柄的方法
▪C#事件处理和委托event delegate实例简述
▪C#获取程序文件相关信息的方法
▪C#中的除法运算符与VB.NET中的除法运算符
▪ASP.NET MVC 5使用X.PagedList.Mvc进行分页教程(PagedLi...
▪Base64编码解码原理及C#编程实例
▪C#实现的优酷真实视频地址解析功能(2014新算...
▪C#和SQL实现的字符串相似度计算代码分享
▪C#使用Word中的内置对话框实例
▪C#反射之基础应用实例总结
▪C#生成单页静态页简单实例
▪C#实现SMTP邮件发送程序实例
▪C#实现随鼠标移动窗体实例
▪C#使用GDI+创建缩略图实例
▪C#实现通过模板自动创建Word文档的方法
▪C#中Response.Write常见问题汇总
▪C#中多态、重载、重写区别分析
▪WinFrom中label背景透明的实现方法
▪C#中out保留字用法实例分析
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3