学习abp-2-ContosoUniversity Abp版
这里略过理论篇,但需要了解abp分层,对于小项目来说abp分层有点复杂,这里只是演示,个别地方没有完全按照ddd理论去写,后期我将会完善。
1. 创建ContosoUniversity相关功能的菜单
在vs2019中,按快捷键“ctrl+,”输入navigationprovider,会出现ContosoAbpNavigationProvider类,这个类中存放了整个系统的菜单:
它位于ContosoAbp.Web.MVC项目的Startup文件夹中:
菜单的定义为MenuItemDefinition,第一个参数为菜单项,这个在cshtml中使用到,以便切换到该页面时,该菜单项处于选中状态,第二个参数为菜单名称,为多语言配置,这里只配置中英文,需要找到ContosoAbp.xml和ContosoAbp-zh-Hans.xml分别配置菜单名称,第三个参数为url,可以为相对地址,也可以为外部地址,第四个参数为图标,第五个参数为页面权限;这里暂时没有配置icon和权限; SetNavigation方法中添加菜单,修改如下:
public override void SetNavigation(INavigationProviderContext context)
{
context.Manager.MainMenu
.AddItem(
new MenuItemDefinition(
PageNames.Home,
L("HomePage"),
url: "",
icon: "home",
requiresAuthentication: true
)
).AddItem(
new MenuItemDefinition(
PageNames.Contoso,
L("ContosoUniversity"),
url: "",
icon: "business"
).AddItem(
new MenuItemDefinition(
PageNames.CUStudent,
L("CUStudent"),
url: "Student"
)
).AddItem(
new MenuItemDefinition(
PageNames.CUCourse,
L("CUCourse"),
url: "Course"
)
).AddItem(
new MenuItemDefinition(
PageNames.CUInstructor,
L("CUInstructor"),
url: "Instructor"
)
).AddItem(
new MenuItemDefinition(
PageNames.CUDepartment,
L("CUDepartment"),
url: "Department"
)
)
).AddItem(
new MenuItemDefinition(
PageNames.Tenants,
L("Tenants"),
url: "Tenants",
icon: "business",
requiredPermissionName: PermissionNames.Pages_Tenants
)
).AddItem(
new MenuItemDefinition(
PageNames.Users,
L("Users"),
url: "Users",
icon: "people",
requiredPermissionName: PermissionNames.Pages_Users
)
).AddItem(
new MenuItemDefinition(
PageNames.Roles,
L("Roles"),
url: "Roles",
icon: "local_offer",
requiredPermissionName: PermissionNames.Pages_Roles
)
)
.AddItem(
new MenuItemDefinition(
PageNames.About,
L("About"),
url: "About",
icon: "info"
)
).AddItem( // Menu items below is just for demonstration!
new MenuItemDefinition(
"MultiLevelMenu",
L("MultiLevelMenu"),
icon: "menu"
).AddItem(
new MenuItemDefinition(
"AspNetBoilerplate",
new FixedLocalizableString("ASP.NET Boilerplate")
).AddItem(
new MenuItemDefinition(
"AspNetBoilerplateHome",
new FixedLocalizableString("Home"),
url: "https://aspnetboilerplate.com?ref=abptmpl"
)
).AddItem(
new MenuItemDefinition(
"AspNetBoilerplateTemplates",
new FixedLocalizableString("Templates"),
url: "https://aspnetboilerplate.com/Templates?ref=abptmpl"
)
).AddItem(
new MenuItemDefinition(
"AspNetBoilerplateSamples",
new FixedLocalizableString("Samples"),
url: "https://aspnetboilerplate.com/Samples?ref=abptmpl"
)
).AddItem(
new MenuItemDefinition(
"AspNetBoilerplateDocuments",
new FixedLocalizableString("Documents"),
url: "https://aspnetboilerplate.com/Pages/Documents?ref=abptmpl"
)
)
).AddItem(
new MenuItemDefinition(
"AspNetZero",
new FixedLocalizableString("ASP.NET Zero")
).AddItem(
new MenuItemDefinition(
"AspNetZeroHome",
new FixedLocalizableString("Home"),
url: "https://aspnetzero.com?ref=abptmpl"
)
).AddItem(
new MenuItemDefinition(
"AspNetZeroDescription",
new FixedLocalizableString("Description"),
url: "https://aspnetzero.com/?ref=abptmpl#description"
)
).AddItem(
new MenuItemDefinition(
"AspNetZeroFeatures",
new FixedLocalizableString("Features"),
url: "https://aspnetzero.com/?ref=abptmpl#features"
)
).AddItem(
new MenuItemDefinition(
"AspNetZeroPricing",
new FixedLocalizableString("Pricing"),
url: "https://aspnetzero.com/?ref=abptmpl#pricing"
)
).AddItem(
new MenuItemDefinition(
"AspNetZeroFaq",
new FixedLocalizableString("Faq"),
url: "https://aspnetzero.com/Faq?ref=abptmpl"
)
).AddItem(
new MenuItemDefinition(
"AspNetZeroDocuments",
new FixedLocalizableString("Documents"),
url: "https://aspnetzero.com/Documents?ref=abptmpl"
)
)
)
);
}
private static ILocalizableString L(string name)
{
return new LocalizableString(name, ContosoAbpConsts.LocalizationSourceName);
}
}
菜单文字配置:
2. 创建实体
创建实体是在ContosoAbp.Core项目里,这里先创建Student,Enrollment和Course实体;
3. 在数据库上下文中添加实体对应的数据集
abp强大的地方在于,它虽然提供了仓储模式和工作单元模式,但你依然可以选择最原始的Context用最原始的方法。
数据库上下文定义在ContosoAbp.EntityFrameworkCore项目的EntityFrameworkCore文件夹下,修改如下:
4.添加种子数据
添加初始化数据在ContosoAbp.EntityFrameworkCore项目下的 Seed的文件夹下,这里在Host(租主)文件夹下新建类:DefaultContosoCreator.cs,在这里模仿其他类,添加初始化数据,这里要去除context.SaveChanges();因为后面会统一保存数据,之后在InitialHostDbBuilder.cs添加 new DefaultSettingsCreator(_context).Create();
5. 数据迁移
在程序包管理控制台中执行命令:
add-migration InitialContosoEntities
再执行:update-database;
运行项目,当成功显示界面后,初始化数据会自动写入数据库。
下一节:创建学生列表,并分页