3 层架构 - 需要一个例子 [英] 3 Tier Architecture - In need of an example

查看:22
本文介绍了3 层架构 - 需要一个例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用单层架构.现在我想学习如何使用 3 层架构编写代码.请给我一个简单的例子好吗?

Presently I am working using single tier architecture. Now I am wanting to learn how to write code using 3 tier architecture. Please can you provide me with a simple example?

推荐答案

这就是我在我的项目中所拥有的.不仅仅是传统的三层架构.

This is what I have in my project. More than just a traditional 3-tier architecture.

1.) 应用程序.基础设施

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

2.) Application.DataModel

  • 数据库的类型化数据集.
  • TableAdapter 扩展为包含事务和我可能需要的其他功能.

3.) Application.DataAccess

  • 数据访问类.
  • 使用底层类型化数据集查询数据库操作的实际位置.

4.) Application.DomainObjects

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

5.) Application.BusinessLayer

  • 提供可从表示层访问的管理器类.
  • HttpHandlers.
  • 我自己的页面基类.
  • 这里有更多内容..

6.) Application.WebClientApplication.WindowsClient

  • 我的表示层
  • 从 Application.BusinessLayer 和 Application.BusinessObjects 获取引用.

Application.BusinessObjects 在整个应用程序中使用,并在需要时跨越所有层 [Application.DataModel 和 Application.Infrastructure 除外]

Application.BusinessObjects are used across the application and they travel across all layers whenever neeeded [except Application.DataModel and Application.Infrastructure]

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

All my queries are defined only Application.DataModel.

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

Application.DataAccess returns or takes Business objects as part of any data-access operation. Business objects are created with the help of reflection attributes. Each business object is marked with an attribute mapping to target table in database and properties within the business object are marked with attributes mapping to target coloumn in respective data-base table.

我的验证框架让我可以在指定的 ValidationAttribute 的帮助下验证每个字段.

My validation framework lets me validate each field with the help of designated ValidationAttribute.

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

My framrwork heavily uses Attributes to automate most of the tedious tasks like mapping and validation. I can also new feature as new aspect in the framework.

示例业务对象在我的应用程序中看起来像这样.

A sample business object would look like this in my application.

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)
    {
    }
}

这篇关于3 层架构 - 需要一个例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆