本篇文章介绍怎么创建自己的模块,并且使用依赖注入方法进行模块间的无缝结合。

我们创建一下自己的一个会员模块,针对不同的系统都可以用。你们可以看看我是怎么做的,或者从中得到启发。

目录

1.开始创建项目

2.新建自己的模块

1)引入类库

2)创建模块类

3)创建实体类与仓储

4)创建service类

5)创建对外用的类(接口)

3.其他模块调用会员模块

1.开始创建项目

首先,我们到ABP官网上下载一个MVC NLayered的解决方案。项目名字叫TestMember

具体怎么下载网上很多资料,这里不一一介绍。

2.新建自己的模块

下载完项目之后,我们新建一个类库,名字叫ClassLibraryMember。用户我们的会员模块。

创建完成之后,如下:

1)引入类库

Install-Package Abp -Version 2.0.2
Install-Package Abp.Zero -Version 2.0.1

当然,你也可以引入自己想要的版本。我们这里用到Zero模块,你也可以不用。

2)创建模块类

首先我们要创建一个模块类,因为ABP是用模块方式的架构来解耦的。模块之间有依赖性,A模块可以依赖于B模块,B模块依赖于A模块。

模块是怎么工作的呢?

我们可以看到,在Web模块里面,有个模块类,叫XXXWebModule,在App_Start文件夹下。在类的上面,有模块的所有依赖:

   [DependsOn(
typeof(AbpWebMvcModule),
typeof(TestMemberDataModule),
typeof(TestMemberApplicationModule),
typeof(TestMemberWebApiModule),
typeof(ClassLibraryMember.ClassLibraryMemberModule))]
public class TestMemberWebModule : AbpModule

ClassLibraryMemberModule模块是作者自己添加进去的,后面再做阐述。

我们看到,首先依赖于AbpWebMvcModule模块,此模块是ABP写的。

然后依赖于TestMemberDataModule模块,此模块是EntityFramework模块的模块类。

然后依赖于TestMemberApplicationModule模块,此模块是Application模块的模块类。

再依赖于TestMemberWebApiModule模块,此模块是WebApi模块的模块类。

最后是自己的模块类(会员模块)ClassLibraryMemberModule。

本次关于模块的调用关系先介绍到这里,有深入兴趣的话,可以看此文章 http://www.cnblogs.com/huaizuo/p/4836853.html

现在,我们创建自己的模块类,叫ClassLibraryMemberModule,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Abp.Dependency;
using Abp.Modules; namespace ClassLibraryMember
{
public class ClassLibraryMemberModule:AbpModule
{ //
// 摘要:
// This method is used to register dependencies for this module.
public override void Initialize()
{
//这行代码的写法基本上是不变的。它的作用是把当前程序集的特定类或接口注册到依赖注入容器中。
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
//
// 摘要:
// This method is called lastly on application startup.
public override void PostInitialize()
{ }
//
// 摘要:
// This is the first event called on application startup. Codes can be placed here
// to run before dependency injection registrations.
public override void PreInitialize()
{ }
//
// 摘要:
// This method is called when the application is being shutdown.
public override void Shutdown()
{ } }
}

可以看到,我们的会员模块,暂时没依赖到其他模块。

这行代码的写法基本上是不变的。它的作用是把当前程序集的特定类或接口注册到依赖注入容器中:

IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

到此我们的模块以及创建成功了。

我们在梳理一下模块的内部调用顺序:

假如模块A依赖于模块B,在项目启动时则运行方法顺序如下:

模块B的PreInitialize –> 模块A的PreInitialize

模块B的Initialize —> 模块A的Initialize

模块B的PostInitialize—> 模块A的PostInitialize

如果项目停止,则:

模块B的Shutdown—> 模块A的Shutdown

3)创建实体类与仓储

我们创建一个实体Member,继承AbpUser,如下:

using System.ComponentModel.DataAnnotations.Schema;
using Abp.Authorization.Users;
using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing; namespace ClassLibraryMember.Entity
{
[Table("Member")]
public class Member: AbpUser<Member>
{ public string Token { get; set; } public string IdentityType { get; set; } public string Identifier { get; set; } }
}

即使我们创建了实体类,并且写了Table注解,但是怎么才能在新建数据库的时候才能把此表加入到数据库中呢?

所以我们找到EntityFramework中的类XXXDbContext。在此类中,建一个会员类的属性:

public virtual IDbSet<ClassLibraryMember.Entity.Member> Members { get; set; }

最后,我们运行数据库的创建,就能创建Member表了。如下:

4)创建service类

上面我们创建了实体类与数据库,但是怎么实现数据库的CRUD操作呢?

我们写一个会员的service,分别为接口IMemberService,实现类MemberService

在MemberService类中,我们要继承DomainService,此类是用来所有自己创建的domain services用到的。

当我们继承了此类时,有几个好处:

1)自动实例化(依赖注入)

2)继承了AbpServiceBase,此类实现了很多通用的方法,我们都可以用到

为什么继承DomainService会自动实例化呢?因为DomainService继承了ITransientDependency。ABP会扫所有类,如果继承了ITransientDependency,会自动实例化。

接口代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Dependency;
using Abp.Domain.Services; namespace ClassLibraryMember.Core
{
public interface IMemberService
{
void MemberToDo(); }
}

类代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.Domain.Uow;
using ClassLibraryMember.Entity; namespace ClassLibraryMember.Core
{
public class MemberService:DomainService,IMemberService
{ private readonly IRepository<Member, long> _memberRepository; public MemberService()
{
} public MemberService(IRepository<Member, long> memberRepository)
{
_memberRepository = memberRepository;
} public void MemberToDo()
{
try
{
Console.WriteLine("MemberService MemberToDo");
_memberRepository.Insert(new Member()
{
Identifier = Guid.NewGuid().ToString(),
IdentityType = "Token",
Token = Guid.NewGuid().ToString().Substring(10, 5),
EmailAddress = "11",
Name = "22",
Surname = "33",
UserName = "44",
Password = "55fddfgfdgd544",
AccessFailedCount = 1,
IsLockoutEnabled = false,
IsPhoneNumberConfirmed = true,
IsTwoFactorEnabled = true,
IsEmailConfirmed = true,
IsActive = true,
IsDeleted = false,
CreationTime = DateTime.Now
});
CurrentUnitOfWork.SaveChanges();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
} } }
}

5)创建对外用的类(接口)

上面我们创建了实体,并且实现了逻辑。我们这个会员模块要其他人也可以用,所以我们要创建IMemberClient接口,并且继承ITransientDependency,因为要用到依赖注入:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Dependency; namespace ClassLibraryMember.ServicesClient
{
public interface IMemberClient: ITransientDependency
{ void ToDo(); }
}

在创建它的实现类,MemberClient。此类有一个IMemberService的属性,也是注入的方式。

using ClassLibraryMember.Core;

namespace ClassLibraryMember.ServicesClient
{
public class MemberClient:IMemberClient
{
private IMemberService _memberService; public MemberClient()
{
} public MemberClient(IMemberService memberService)
{
_memberService = memberService;
} public void ToDo()
{
_memberService.MemberToDo();
} }
}

到此,创建自己的类库已经完毕。

那外面怎么用呢,下面开始介绍。

3.其他模块调用会员模块

1)首先,在Web模块里面,Web的模块类里面,添加对会员模块的依赖。

[DependsOn(
typeof(AbpWebMvcModule),
typeof(TestMemberDataModule),
typeof(TestMemberApplicationModule),
typeof(TestMemberWebApiModule),
typeof(ClassLibraryMember.ClassLibraryMemberModule))]
public class TestMemberWebModule : AbpModule

2)然后在EntityFramework仓储里面,添加对Member的实体的创建

public virtual IDbSet<ClassLibraryMember.Entity.Member> Members { get; set; }

3)最后,开始调用。

我们这里从Application模块层里面调用。

首先要实现对IMemberClient的构造器注入,然后调用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Dependency;
using Abp.Domain.Repositories;
using ClassLibraryMember.ServicesClient;
using TestMember.Entity; namespace TestMember.Services
{
public class TestAppService: ITestAppService
{ private readonly IMemberClient _iMemberClient; public TestAppService() { } public TestAppService(IMemberClient iMemberClient)
{
_iMemberClient = iMemberClient;
} public void Test1()
{
_iMemberClient.ToDo();
} }
}

到此,调用与测试都完毕,你们可以从GitHub中下载此demo:

https://github.com/cjt321/TestMember2

其他文章:模块化的深入探讨

C# ABP - 创建自己的模块的更多相关文章

  1. 【ABP框架系列学习】模块系统(4)

    0.引言 ABP提供了构建模块和通过组合模块以创建应用程序的基础设施.一个模块可以依赖于另外一个模块.通常,程序集可以认为是模块.如果创建多个程序集的应用程序,建议为每个程序集创建模块定义. 当前,模 ...

  2. ABP创建数据库操作步骤

    1 ABP创建数据库操作步骤 1.1 SimpleTaskSystem.Web项目中的Web.config文件修改数据库配置. <add name="Default" pro ...

  3. OFBiz进阶之HelloWorld(一)创建热部署模块

    创建热部署模块 参考文档 https://cwiki.apache.org/confluence/display/OFBIZ/OFBiz+Tutorial+-+A+Beginners+Developm ...

  4. 从头开始编写一个Orchard网上商店模块(3) - 创建Orchard.Webshop模块项目

    原文地址:http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-par ...

  5. 创建自定义 HTTP 模块

    本主题中描述的自定义 HTTP 模块阐释了 HTTP 模块的基本功能.在响应下面两个事件时调用该模块:BeginRequest 事件和 EndRequest 事件.这使该模块可以在处理页请求之前和之后 ...

  6. npm创建和发布模块

    今天项目需要使用npm去创建一个模块,然后我查询了了npm的使用文档(Working with private modules),然后对其进行了整理. 一.在操作之前,我们首先要将npm装好,并且登录 ...

  7. 创建maven多模块项目(idea工具)

    1.创建父项目模块(new 一个maven空项目模块)不勾选 create from archetype  删除src目录 2.创建子模块 webapp (该模块为web入口模块) 3.创建其他子模块 ...

  8. idea创建Maven多模块项目

    最近几天学习到了创建多模块项目,应为自己使用的是Idea,所以想用idea创建多模块,查阅了相关资料后,自己做一个记录. 一.首先创建一个maven项目 Parent Project,创建xxx-ro ...

  9. IDEA创建多个模块MavenSpringBoot项目

    最近在学习springboot,先从创建项目开始,一般项目都是一个项目下会有多个模块,这里先创建一个最简单的实例,一个项目下有一个springboot模块项目提供web服务,引用另一个java项目(相 ...

随机推荐

  1. OS基础:动态链接库(一)

    动态链接库(一) 1.新建文件夹,命名lpt 2.用vc6.0建立一个空工程(Win 32 Dynamic-Link Library),名称:lptDll1 3.新建C++文件,命名:lptDll1: ...

  2. bash编程-Shell基础

    1. Shell脚本执行方式 直接运行,需要在脚本文件头部指定解释器,如#!/bin/bash ./myshell.sh 运行时指定shell解释器 bash myshell.sh 2. Shell命 ...

  3. [Proposal]Transform ur shapes!

    [Name] Transform ur shapes [Motivation]市场上有很多涂鸦游戏,例如火柴人涂鸦,非常有趣 我们可以结合所学,将一些图形变形的操作融入进去,做一个我们自己的有趣的游戏 ...

  4. caffe 教程

    Caffe是一个清晰而高效的深度学习框架,本文详细介绍了caffe的优势.架构,网络定义.各层定义,Caffe的安装与配置,解读了Caffe实现的图像分类模型AlexNet,并演示了CIFAR-10在 ...

  5. 在线团队协作工具+在线UML工具

    话不多说直接上https://worktile.com去看,顺便附上小众软件上面的介绍 默默增加worktile的外国原版https://trello.com/,worktile照着trello做的, ...

  6. Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作

    Decimal类型截取保留N位小数向上取Decimal类型截取保留N位小数并且不进行四舍五入操作 封装静态方法 public class DecimalHelper { /// <summary ...

  7. updateByPrimaryKey 和 updateByPrimaryKeySelective

    1. 数据库记录 2. updateByPrimaryKey Preparing: UPDATE t_token_info SET entity_id = ?,entity_type = ?,time ...

  8. http://www.vaikan.com/docs/jquery.form.plugin/jquery.form.plugin.html#getting-started

    http://www.vaikan.com/docs/jquery.form.plugin/jquery.form.plugin.html#getting-started Jquery.Form 异步 ...

  9. jQuery Mobile Api

        jQuery Mobile提供了使用Javascript与框架(html5)通信以及进行内容管理的API.下面介绍具体事件. 文档事件     mobileinit事件会在jQuery Mob ...

  10. 07-01 Java 封装

    1:成员变量和局部变量的区别 /* 成员变量和局部变量的区别? A:在类中的位置不同 成员变量:在类中方法外 局部变量:在方法定义中或者方法声明上 B:在内存中的位置不同 成员变量:在堆内存 局部变量 ...