加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 3层架构 – 需要一个例子

发布时间:2020-12-15 23:01:56 所属栏目:asp.Net 来源:网络整理
导读:目前我正在使用单层架构.现在我想要学习如何使用3层架构编写代码.请给我一个简单的例子? 解决方法 这是我在项目中所拥有的.不仅仅是一个传统的3层架构. 1)应用基础设施 所有业务对象的基类,业务对象集合,数据访问类和我的自定义属性和实用程序作为扩展方法,
目前我正在使用单层架构.现在我想要学习如何使用3层架构编写代码.请给我一个简单的例子?

解决方法

这是我在项目中所拥有的.不仅仅是一个传统的3层架构.

1)应用基础设施

>所有业务对象的基类,业务对象集合,数据访问类和我的自定义属性和实用程序作为扩展方法,通用验证框架.这决定了我的最终.net应用程序的整体行为组织.

2.)Application.DataModel

>数据库的类型化数据集.
> TableAdapters扩展到可以包含交易和其他功能.

3.)Application.DataAccess

>数据访问类.
>使用基础类型化数据集查询数据库操作的实际位置.

4.)Application.DomainObjects

>业务对象和业务对象集合.
枚举.

5.)Application.BusinessLayer

>提供从Presentation层访问的管理员类.
> HttpHandlers.
>我自己的页面基类.
>更多的东西去这里..

6.)Application.WebClient或Application.WindowsClient

>我的演示层
>从Application.BusinessLayer和Application.BusinessObjects引用.

Application.BusinessObjects跨越应用程序使用,并且每当需要时跨越所有层次[除了Application.DataModel和Application.Infrastructure]

我的所有查询都仅定义Application.DataModel.

Application.DataAccess返回或将业务对象作为任何数据访问操作的一部分.在反射属性的帮助下创建业务对象.每个业务对象都标有与数据库中的目标表的属性映射,业务对象中的属性将标记为映射到相应数据库表中的目标列的属性.

我的验证框架允许我在指定的ValidationAttribute的帮助下验证每个字段.

我的框架大量使用属性来自动执行大多数繁琐的任务,如映射和验证.我也可以将新功能作为框架的新方面.

在我的应用程序中,示例业务对象将如下所示.

User.cs

[TableMapping("Users")]
public class User : EntityBase
{
    #region Constructor(s)
    public AppUser()
    {
        BookCollection = new BookCollection();
    }
    #endregion

    #region Properties

    #region Default Properties - Direct Field Mapping using DataFieldMappingAttribute

    private System.Int32 _UserId;

    private System.String _FirstName;
    private System.String _LastName;
    private System.String _UserName;
    private System.Boolean _IsActive;

    [DataFieldMapping("UserID")]
    [DataObjectFieldAttribute(true,true,false)]
    [NotNullOrEmpty(Message = "UserID From Users Table Is Required.")]
    public override int Id
    {
        get
        {
            return _UserId;
        }
        set
        {
            _UserId = value;
        }
    }

    [DataFieldMapping("UserName")]
    [Searchable]
    [NotNullOrEmpty(Message = "Username Is Required.")]
    public string UserName
    {
        get
        {
            return _UserName;
        }
        set
        {
            _UserName = value;
        }
    }

    [DataFieldMapping("FirstName")]
    [Searchable]
    public string FirstName
    {
        get
        {
            return _FirstName;
        }
        set
        {
            _FirstName = value;
        }
    }

    [DataFieldMapping("LastName")]
    [Searchable]
    public string LastName
    {
        get
        {
            return _LastName;
        }
        set
        {
            _LastName = value;
        }
    }

    [DataFieldMapping("IsActive")]
    public bool IsActive
    {
        get
        {
            return _IsActive;
        }
        set
        {
            _IsActive = value;
        }
    }

    #region One-To-Many Mappings
    public BookCollection Books { get; set; }

    #endregion

    #region Derived Properties
    public string FullName { get { return this.FirstName + " " + this.LastName; } }

    #endregion

    #endregion

    public override bool Validate()
    {
        bool baseValid = base.Validate();
        bool localValid = Books.Validate();
        return baseValid && localValid;
    }
}

BookCollection.cs

/// <summary>
/// The BookCollection class is designed to work with lists of instances of Book.
/// </summary>
public class BookCollection : EntityCollectionBase<Book>
{
    /// <summary>
    /// Initializes a new instance of the BookCollection class.
    /// </summary>
    public BookCollection()
    {
    }

    /// <summary>
    /// Initializes a new instance of the BookCollection class.
    /// </summary>
    public BookCollection (IList<Book> initialList)
        : base(initialList)
    {
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读