Visual Studio 2008+NClay小试牛刀

  经过一段时间的努力,NClay框架终于到了可以见人的阶段了,为了让大家了解NClay,于是基于NClay编写了一个SmallBlog程序作为介绍案例。在开发SmallBlog过程中,刚好VS 2008 beta 2中文版发布,看到她的特性后发现能进一步提高NClay的开发效率,于是就用VS 2008结合NClay进行SmallBlog的编写。

 NClay的结构

 

Visual Studio 2008+NClay小试牛刀

  

  图1

 SmallBlog

 SmallBlog是一个简单的个人博客程序,从SmallBlog中大家可以了解到NClay的ORM,MVC和AOP等功能的应用,下面通过SmallBlog的代码来了解NClay的处理结果。

 程序结构

 

Visual Studio 2008+NClay小试牛刀

 图2

 程序主要分为三大部分:

 实体层:SmallBlog.Entities用于各层进行数据交换的基础介体。

 逻辑层:SmallBlog.Logic用于处理各种输入和输出逻辑

 表现层:SmallBlog用于信息的输出和信息输入。

 实体的定义

 实体的是程序和数据结构的映射描述,也是NClay.Data进行数据操作的依赖结构。

/// <summary>
/// DBModel.Table
/// </summary>
[Serializable]
[NClay.Data.Mappings.TableMapper("Category")]
public partial class Category
{
 public Category ()
 {
//
// TODO: 在此处添加构造函数逻辑
//
 }
 private string mCategoryID;
/// <summary>
/// varchar
/// </summary>
[NClay.Data.Mappings.PrimaryKey("CategoryID")]
public string CategoryID
{
 get
 {
return mCategoryID;
 }
 set
 {
mCategoryID= value;
 }
}

 业务逻辑

 NClay的原则是所有逻辑处理必须以接口的方式体现,所以在设计阶段必须构造业务输出和输入的逻辑接口。

public interface ICategoryDelete
{
 string CategoryID
 {
get;
set;
 }
}
public void Delete(ICategoryDelete logic)
{
 if (!NClay.Common.IsEmpty(logic.CategoryID))
 {
if ((DB.Post.CategoryID == logic.CategoryID).CountOf<Entities.Post>() > 0)
{
 throw new LogicException("有文章在此类别中不能删除!");
}
(DB.Category.CategoryID == logic.CategoryID).Delete<Entities.Category>();
 }
}

  VS 2008对代码的感知支持有所提高,编辑器能直接感知到表达式所返回的类型:

Visual Studio 2008+NClay小试牛刀

 

  图3

 视图

 由于VS 2008对默认属性的支持,这样大大减少了NClay下的视图代码;在VS 2005里,不得不对编辑器生成的接口代码进行调整。而VS 2008下直接编辑生成接口代码就可以,省了不少修改的工作。

[NClay.MVC.Action(ActionType = NClay.MVC.ActionType.All,
Tag = "~/Default.aspx", Services = new Type[] {
typeof(Logic.Post.IPostView) })]
public class Default :BaseView ,Logic.Post.IPostView
{
 #region IPostView 成员
public string CategoryID
 { get; set; }
public System.Collections.Generic.IList<SmallBlog.Entities.PostView> Posts
 { get; set; }
#endregion
 #region IDataPageProperty 成员
 [NClay.MVC.Bind(typeof(NClay.DataPage))]
 public NClay.IDataPage DataPage
 { get; set; }
 #endregion
}

  AOP扩展

 

 很多时候面对多个视图处理同样的逻辑,这时候可以借助于框架的AOP功能完成。以下是统一处理页面左侧的功能。

[NClay.MVC.ViewAspect(NClay.MVC.AspectLevel.High)]
public class AspectBaseView:NClay.MVC.IAspect
{
 #region IAspect 成员
 public void Aspect(object source, NClay.MVC.AspectHandler e)
 {
if (source is BaseView)
{
 BaseView bv = (BaseView)source;
 NClay.MVC.Container.Execute<Logic.SysUser.IBlogConfig>(bv, true);
 NClay.MVC.Container.Execute<Logic.Category.IStatCategories>(bv, true);
 NClay.MVC.Container.Execute<Logic.Post.IHotPost>(bv, true);
}
e.Execute(source);
 }
 #endregion
}

  为了避免加载重复的数据,可以通过AOP来实现逻辑数据的缓存处理。

[NClay.MVC.LogicAspect(typeof(Logic.SysUser.IBlogConfig),
typeof(Logic.SysUser.IEditUserInfo))]
public class BlogConfigCache : NClay.MVC.IAspect
{
 #region IAspect 成员
public void Aspect(object source, NClay.MVC.AspectHandler e)
 {
if (source is Logic.SysUser.IBlogConfig)
{
 Logic.SysUser.IBlogConfig config = (Logic.SysUser.IBlogConfig)source;
 config.OwnerConfig = CacheUnit.GetBlogConfig();
 if (config.OwnerConfig == null)
 {
e.Execute(source);
CacheUnit.SetBlogConfig(config.OwnerConfig);
 }
}
if (source is Logic.SysUser.IEditUserInfo)
{
 e.Execute(source);
 CacheUnit.ClearBlogConfig();
}
 }
 #endregion
}

 程序运行效果图

Visual Studio 2008+NClay小试牛刀

  

  图4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值