Adhesive框架系列文章--ADO.NET EF 数据访问模块使用

Adhesive框架的数据访问层是基于ADO.NET Entity Framework,所以用法和实体框架基本相同。为了满足实际项目的需要,我们在实体框架的基础上做了一些扩展,主要有以下几点:

1、对数据库连接字符串中比较敏感的密码字段进行加密;

2、数据库连接字符串配置脱离本地配置文件(web.config或app.config),集中在配置服务后台进行配置和管理;

3、数据自动审核,只要实体对象实现了IAuditable接口,当添加或修改实体对象时,CreatedOn和ModifiedOn字段会被自动设置为当前日期;

4、逻辑删除,像一些比较重要的业务数据,一般是不允许物理删除的,只要实体对象实现了ISoftDeletable接口,当执行删除操作时,数据不会被删除,只是IsDeleted字段会被值为true;

5、Id自动生成,每个实体都应该具有一个能唯一标识自身的Id字段,如果该Id字段没有实际意义,可以考虑自动生成。当实体实现了IIdentifiable接口,并且没有为Id字段赋值时,系统自动生成一个32位的连续的guid作为Id;

6、乐观并发控制,实体框架已经具备乐观并发控制的功能,最高效的方式是使用时间戳的方式,但是这种方式对MS SQL server支持比较好,不太适合其他类型的数据库。框架实现了基于行版本的乐观并发控制,只要实体对象实现了IVersionable接口,就自动启用该功能。

下面来看下具体如何使用:

(1)定义实体

public class Customer
        : Entity,  IAuditable, IVersionable, ISoftDeletable
    {
        [StringLength(256)]
        public string FirstName { get; set; }
        [StringLength(256)]
        public string LastName { get; set; }
        [StringLength(256)]
        public string FullName
        {
            get
            {
                return string.Format("{0}, {1}", this.LastName, this.FirstName);
            }
            set { }
        }
        [StringLength(256)]
        public string Telephone { get; set; }
        [StringLength(256)]
        public string Company { get; set; }
        public decimal CreditLimit { get; set; }
        public string CreatedBy { get; set; }
        public DateTime CreatedOn { get; set; }
        public string ModifiedBy { get; set; }
        public DateTime? ModifiedOn { get; set; }
        [ConcurrencyCheck]
        public long RowVersion { get; set; }
        public bool IsDeleted { get; set; }

    }

自定义实体必须继承抽象类Entity,Entity中定义了标识字段Id,实现了对象比较的一些基本方法。

 

(2)定义DbContext

[ContextAttribute("CustomerDbContext")]
    class CustomerDbContext : StorageContext
    {
        public CustomerDbContext(string contextName)
            : base(contextName)
        {
        }
        public DbSet<Customer> Customers { get; set; }
    }

自定义DbContext必须继承抽象类StorageContext,StorageContext是对DbContext的封装。StorageContext的构造函数有一个参数contextName,我们称之为数据库上下文名称,每个数据库上下文都对应了一个实际的数据库。数据库上下文名称有两种方式设置,第一种方式是通过ContextAttribute的ContextName属性指定,第二种方式是自定义DbContext类型的名称,比如自定义DbContext类型的名称是CustomerDbContext,那么它所对应的数据库上下文名称就是CustomerDbContext。在这两种方式中,前一种方式的优先级最高。如果指定的数据库上下文没有配置,那么会使用默认数据库上下文DefaultContext。

(3)数据库上下文配置

数据库上下文由三个部分组成:数据库上下文名称、数据提供者名称、数据库连接字符串名称。数据库上下文是在配置服务后台

全局配置->StorageConfig –>StorageContexts下配置的,如下图:

image

 

系统有一个默认的存储上下文DefaultContext,如下图:

image

 

数据库上下文的三个组成部分见下图:

image

以上三个部分可以根据具体的要求自己进行修改。

 

如果要将Customer信息存在另外一个数据库,这就需要再添加相关的数据库上下文配置。下面我们为上面自定义的DbContext新建一个名为CustomerDbContext的数据库上下文配置(导航到StorageContexts节点下点击增加按钮)如下:

image

 

点击确定,如下图:

image

 

由于添加CustomerDbContext节点时会将DefaultContext节点下三个子节点全部进行复制,这里要进行适当修改。存储上下文的名称也就是Name节点的值要和刚才新建数据库上下文时输入的名称保持一致,都是CustomerDbContext。由于我们使用的是Sql Server数据库,所以数据提供程序名称为System.Data.SqlClient。由于要将Customer存在CustomerDb数据库中,和其他数据分开存储,这里将数据库连接字符串里面的数据库名称改为CustomerDb。最终的CustomerDbContext配置见下图:

image

 

(4)数据操作

        AdhesiveFramework.Start();

            IDbContextFactory dbContextFactory = LocalServiceLocator.GetService<IDbContextFactory>();
            using (CustomerDbContext context = dbContextFactory.CreateContext<CustomerDbContext>())
            {
                Customer c = new Customer();
                c.FirstName = "James";
                c.LastName = "Chan";
                context.Customers.Add(c);
                context.SaveChanges();
            }

            AdhesiveFramework.End();

以上代码执行成功后,会自动创建数据库CustomerDb和Customer表。

image

添加的数据:

image

posted @ 2011-10-27 10:57  陈 锋  阅读(2066)  评论(6编辑  收藏  举报