最佳实践使用C#XSD类型化数据集,在企业应用中 [英] Best Practice Using C# XSD typed-datasets In Enterprise Applications

查看:95
本文介绍了最佳实践使用C#XSD类型化数据集,在企业应用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解什么是使用从我目前使用的数据库方案生成的XSD表的最佳实践。

I'm trying to understand what is the best practice using XSD tables generated from the Database scheme that I currently use.

1)你认为XSD信息应放置在模型的一部分?

1) Do you think the XSD information should be located as part of the Model?

2),这是否意味着数据访问层返回数据集和其他生成的对象?

2) Does it mean that Data Access Layer returns Datasets and other generated objects?

3)是否经过所有的系统层一直到用户界面?

3) Does it goes through all the system layers all the way to the UI?

4)如果XSD是数据访问层的一部分,我要的结果转化为物体模型?什么是最好的转换方法?

4) If the XSD is part of the Data Access Layer, should I convert the results to objects from the Model? What is best convert methodology?

谢谢,
罗尼

推荐答案

您已经限制在你的问题作出具体到数据集的XSD的目的和XSD的应用程序。

XSD是可扩展Scehma认定中的首字母缩写。 XSD标准是由W3C标准化
XML文件的缘故,你可能在你的
的应用程序使用定义的

作为一个例子可以说,你是大量使用在应用程序的XML文件,你可以与不同类型的远程数据源交换。这些来源可以以各种格式发送给您的XML文件。在您的应用程序,你需要确保接收XML文件中正确的格式,这样就可以进一步对XML文件执行业务操作。因此,你需要对这些XML文件实施标准化。您将需要验证对可接受的标准XML文件在你结束。您将需要XML的架构与标准进行比较。这些标准被写入XSD形式。你将验证作为XSD文件中定义针对该架构标准的XML文件的架构。这是XSD文件的实际目的。

As an example lets say, you are heavily using XML files in your application which you may exchange with different types of remote sources. These sources may send you XML files in various formats. In your application you need to be sure to receive the XML file in proper format so that you can further perform your business operations on the XML file. So you need enforce standardization on to those XML file. You will need to validate the XML file against the acceptable standards at your end. You will need to compare the schema of XML with the standards. These standards are written in XSD form. And you will validate the schema of your XML file against the schema standards as defined in the XSD file. This is the actual purpose of the XSD files.

现在回答你的问题。

1。)< STRONG>你认为XSD信息应放置在模型的一部分?

正如我刚才最高审计机关XSD文件存储架构而不是数据。在当你使用它实际上在内存中运行时保存数据的数据集的应用程序相同的方式 - 也将有自己的模式,将在其中保存数据的形式。这些变化的基础上的基本数据表和它们之间的关系。苏女士介绍球员的TypedDataSets的概念。 TypedDataSets - 顾名思义是你会在运行时使用的数据发挥DataSet的资格架构。所以TypedDataSets在定义数据表和关系的模式其间XSD文件的形式实际上限定。所以,当你创建在Visual Studio中TypedDataSet文件,它基本上创建一个XSD文件,您从数据库源添加到TypedDataSet表面的所有表格,进行分析和每个表的元数据方案将在XSD文件被创建。在当你选择记录到的数据集运行时,你已经知道什么样的数据正在进入他们,如果数据不是形式作为XSD你会得到一个运行时异常定义。

As I just sais XSD file stores the schema not the data. Same way in any application when you use Datasets which actually hold data in memory at runtime - will also have its own schema, the form in which it will hold the data. These varies based on the underlying Datatables and their relations. So MS guys introduced the concept of TypedDataSets. TypedDataSets - as the name suggests are qualified schema of the Dataset which you are going to use at run-time to play with the data. So TypedDataSets are actually defined in form of XSD file which defines the schema of DataTables and the relations inbetween. So when you create a TypedDataSet file in Visual studio, it basically creates an XSD file, All tables that you add from the database source to TypedDataSet surface, will be analyzed and metadata schema of each table will be created in the XSD file. At runtime when you select records into your dataset, you already know what kind of data is coming into them and if the data is not in the form as defined in the XSD you will get a runtime exception.

不过的XSD是不是在运行时监守的Visual Studio工具生成的XSD文件tpyed数据集的代码库使用的 XSD.EXE工具

Still XSDs are not instrumental at runtime becuase Visual studio generates tpyed-dataset codebase from the XSD file by using XSD.exe tool.

2)的这是否意味着数据访问层返回数据集和其他生成的对象?

如果您的数据层是使用TypedDataset,它会返回的DataTable或DataRow的[],或DataRow的,因为你需要。

If Your data layer is using TypedDataset, It will return DataTables or DataRow[], or DataRow as you need.

3)难道它去在所有的系统层一直到用户界面?

您可以生成它的顶部自定义业务对象是一个推荐的做法,而不是抛出数据集对象在这里和那里在你的应用程序。

You can generate custom business objects on top of it which is a recommended practice rather than throwing Dataset objects here and there in your application.

4)如果该XSD是数据访问层,我应该将结果转换为对象,从模型的一部分?什么是最好的方法转换?

使用反射写映射机制。我们映射我们的DataRow业务对象实例和数据表到业务对象的集合。

Write a mapping mechanism using Reflection. We map our DataRow to Business object instances and DataTables to Business object Collections.

您可以开始重新设计,以高档项目与更易于维护的架构。当然,这需要时间和精力,但最终你会有很大的效果。

You can start re-designing to upscale your project with more maintainable architecture. And of course this will take time and effort but eventually you'll have great results.

这是我在我的项目。

1) Application.Infrastructure


  • 基类所有的BusinessObjects,业务内容对象收集,数据访问类和我的自定义属性和公用事业扩展方法,通用验证框架。这就决定了我最后的.NET应用程序的整体行为的组织。

2)的 Application.DataModel


  • XSD类型化数据集的数据库。

  • TableAdapter的扩展合并交易和其他功能我可能需要。

3。) Application.DataAccess


  • 数据访问类。

  • 实际地方的数据库操作使用类型化数据集的基本查询。

4。) Application.DomainObjects


  • 业务对象和业务对象的集合。

  • 枚举。

5)的应用.BusinessLayer


  • 提供访问管理器类从表现层。

  • HttpHandlers的。

  • 自己的网页基类。

  • 更事情在这里..

  • Provides manager classes accessible from Presentation layer.
  • HttpHandlers.
  • My own Page base class.
  • More things go here..

6。) Application.WebClient Application.WindowsClient


  • 我的表现层

  • 从Application.BusinessLayer和Application.BusinessObjects注意到引用。

Application.BusinessObjects跨应用程序使用,他们只要跨neeeded [除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收益或需要的业务对象的任何数据访问操作的一部分。业务对象与反射属性的帮助下产生。每个业务对象标有一个属性映射到目标表中的数据库和业务对象内的属性标记具有属性映射到目标在各个数据基表coloumn

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.

我framrwork大量使用属性来最繁琐的任务,像自动映射和验证。我还可以将新的功能作为框架新的方面。

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

这篇关于最佳实践使用C#XSD类型化数据集,在企业应用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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