为了账号安全,请及时绑定邮箱和手机立即绑定

快速上手 ABP 框架学习笔记记录(30天学习记录)第四天

标签:
C#

ABP 在网上火了很久了,之前自己的有学习过一小段时间,但之后懒癌发作,就停止学习了。
人生啊,还真的要不断学习~由其是当编程人员!!!!
不说了,为了备忘,所以记录下一些比较重要的东西。

一个不错的ABP博客文:http://www.cnblogs.com/sheng-jie/p/6260035.html

写得很好的~而且有代码

代码完成后再上传

1.数据迁移

命令: update-database
       Add-Migration Add_yourEntityName_Entity

如果你在ABP 官网下载了DEMO(带ZERO),使用VS2017(以后可能20XX)打开,重新生成后,设置web项目下的WEB.CONFIG 数据库连接字符串后,可以使用以上两个代码生成数据库。

创建新的表(EF Code First)

在XXX.Core 领域项目下,新建类. (例如 School)

继承 ABP 中的 ENTITY 类, 该类有一个属性 Id , 默认为 INT 类型的,

但也可以修改为其他类型。

   demo : public class School :Entity<string>{   .....    }

创建完一个类,那么如何通过命令新增数据库呢?
Add-Migration Add_yourEntityName_Entity 命令就可以出场了。
(PS:
1.必须设置WEB 、WEB API 项目为启用项
2.在工具-》NuGet包管理器-》程序包管理控制台

如下图图片描述

PM> Add-Migration Add_yourEntityName_Entity

回车后,正常情况会在 Demo.EntityFramework 项目下生成一个以时间格式+类名 的文件

然后再使用

PM>Update DataBase 

这样数据库就会新建一张表


Application

Application 是 WEB 和 CODE 项目间的桥梁,

DTO

个人理解是:同一个表,在不同的页面,需要展示的属性是不同的。不多不少只展示需求暴露的属性就好了,DTO 就是不同的 视图类。
建不同的DTO 类有利于扩展和不会造成混乱。(这个简单说法不太能说明DTO的好处,不想在这里说太多了,在写比较复杂的页面时,就能体会到)

IXXXXService
代码如下: 创建一个接口并现实,然后在WEB 中就可以调用了。
那么数据库中的交互是怎样的呢?
(从官网下载的项目中: XXXX.EntityFramework中的文件夹EntityFramework 有一个文件:XXXXDbContext

新建一个类后在此页面中添加: DEMO如下

  public class DemoDbContext : AbpZeroDbContext<Tenant, Role, User>
    {
        //TODO: Define an IDbSet for your Entities...

            public IDbSet<Schools> School { get; set; }

            ......其他代码......
   }
 public interface ISchoolService : IApplicationService
    {
        void GetAll();
    }

public class SchoolService:ISchoolService
    {
        private readonly IRepository<Schools, string> _SchoolsRepository;

        public SchoolService(IRepository<Schools, string> schoolsRepository)
        {
            _SchoolsRepository = schoolsRepository;
        }

        public void GetAll()
        {
            var list = _SchoolsRepository.GetAll().ToList();
        }
    }

上述代码: 应该不难理解,要注意一点是: 如果在ABP中没有创建类的Repository (School 类就没有),则可以使用ABP框架中的Repository 的方法。如下图:
图片描述

写好这里,那么在WEB项目中 注入构造函数,即可以与数据库交互了。


Repositories

上面说到,如果ABP 提供的方法不能满足你的需求,那么这要自定义一个Repositories

新建Repositories 和实现 Repositories 在 CORE 和 EntityFramework 项目中

1.CORE 项目下的 IRepositories 文件夹 新建一个 IStudentRepository


   // Student 学习实体(不清楚,看上面)
   public interface IStudentRepository: IRepository<Student,string>
    {
        void GetStudentTest();
    }

2.EntityFramework 项目中实现新建的 IRepositories

EntityFramework -》EntityFramework-》Repositories文件夹

 public class StudentRepository:DemoRepositoryBase<Student,string>, IStudentRepository
    {
        public StudentRepository(IDbContextProvider<DemoDbContext> dbContextProvider):base(dbContextProvider)
        {

        }
        //获取学生信息
        public void GetStudentTest()
        {
            var query = GetAllList();
        }
    }

继承DemoRepositoryBase 并实现接口

编写完以上代码,那么要在 Application 项目中调用。

public class StudentService : IStudentService
    {

        //因为自定义了Repository,所以不用  IRepository<Schools, string> 
        private readonly IStudentRepository _StudentRepository;

        public StudentService(IStudentRepository studentRepository)
        {
            _StudentRepository = studentRepository;
        }

        //这个方法是 IStudentService 中定义的
        public void GetStudentTest()
        {
           //这个方法是在 IStudentRepository 中定义的
            _StudentRepository.GetStudentTest();
        }
    }

然后在WEB层就能调用,测试过,能获取到数据

坚持的第四天。

在剩余2017年里,每晚也要学习!!!

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消