Create your first Child
We will create in first the child feature 'Engines'.
Prerequisites
CRUD Parent creation
Follow steps from the section Create your first CRUD to create the parent's child.
We will assume that the parent is a Plane for this documentation.
Create the Model
- In '...\MyFirstProject\DotNet\MyCompany.MyFirstProject.Domain\PlaneModule\Aggregate'
- Create empty class 'Engine.cs' and add following:
Engine.cs
// <copyright file="Engine.cs" company="MyCompany">
// Copyright (c) MyCompany. All rights reserved.
// </copyright>
namespace MyCompany.MyFirstProject.Domain.Plane.Entities
{
using BIA.Net.Core.Domain;
/// <summary>
/// The engine entity.
/// </summary>
public class Engine : VersionedTable, IEntity<int>
{
/// <summary>
/// Gets or sets the engine Id.
/// </summary>
public int Id { get; set; }
/// <summary>
/// Gets or sets the engine serial number.
/// </summary>
public string SN { get; set; }
/// <summary>
/// Gets or sets the plane Id.
/// </summary>
public int PlaneId { get; set; }
/// <summary>
/// Gets or sets the plane.
/// </summary>
public virtual Plane Plane { get; set; }
}
}
- In Plane.cs, add a new
ICollection<Engine>
to the model:
Plane.cs
// <copyright file="Plane.cs" company="MyCompany">
// Copyright (c) MyCompany. All rights reserved.
// </copyright>
namespace MyCompany.MyFirstProject.Domain.PlaneModule.Aggregate
{
public class Plane : VersionedTable, IEntity<int>
{
[...]
/// <summary>
/// Gets or sets the list of engines.
/// </summary>
public ICollection<Engine> Engines { get; set; }
}
}
Complete DataContext
- Go in '...\MyFirstProject\DotNet\MyCompany.MyFirstProject.Infrastructure.Data' folder.
- Open DataContext.cs and add your new
DbSet<Engine>
:
DataContext.cs
public class DataContext : BiaDataContext
{
// Existing DbSet<T>
/// <summary>
/// Gets or sets the Engine DBSet.
/// </summary>
public DbSet<Engine> Engines { get; set; }
}
- In folder ModelBuilders, create class EngineModelBuilder.cs or use parent's model builder, and add :
EngineModelBuilder.cs
namespace MyCompany.MyFirstProject.Infrastructure.Data.ModelBuilders
{
using Microsoft.EntityFrameworkCore;
using MyCompany.MyFirstProject.Domain.CompanyModule.Aggregate;
/// <summary>
/// Class used to update the model builder for Engine domain.
/// </summary>
public static class EngineModelBuilder
{
/// <summary>
/// Create the model for projects.
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
public static void CreateModel(ModelBuilder modelBuilder)
{
CreateEngineModel(modelBuilder);
}
/// <summary>
/// Create the model for engines.
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
private static void CreateEngineModel(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Engine>().HasOne(x => x.Plane).WithMany(x => x.Engines).HasForeignKey(x => x.PlaneId);
}
}
}
- Back to DataContext.cs, ensure to have a call to your model builder's method
CreateModel
:
DataContext.cs
public class DataContext : BiaDataContext
{
/// <inheritdoc cref="DbContext.OnModelCreating"/>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Existing model builders
EngineModelBuilder.CreateModel(modelBuilder);
this.OnEndModelCreating(modelBuilder);
}
}
Update the database
- Open a new Package Manager Console.
- Set default project to MyCompany.MyFirstProject.Infrastructure.Data.
- Run command
add-migration -context "DataContext" AddEngines
. - Verify the generated migration.
- Run command
update-database -context "DataContext"
- Verify your database.
Generate DTO
Using BIAToolKit
- Launch the BIAToolKit, go to the tab "Modify existing project".
- Set your parent project path, then select your project folder.
- Go to "DTO Generator" tab.
- Fill the form as following :
- Then, click on Generate button !
- Complete MyCompany.MyFirstProject.Domain.Dto.Plane.EngineDto.cs by adding property's attribute
IsParent = true
for thePlaneId
property :
EngineDto.cs
/// <summary>
/// Gets or sets the PlaneId.
/// </summary>
[BiaDtoField(IsParent = true, Required = true)]
public int PlaneId { get; set; }
Generate CRUD
Using BIAToolKit
- Launch the BIAToolKit, go to the tab "Modify existing project".
- Set your parent project path, then select your project folder.
- Go to "CRUD Generator" tab.
- Fill the form as following : Based on this informations, the BIAToolKit will detect automatically the parent's folders to generate the new CRUD child. Make sure to fill the correct informations without misspelling.
- Then, click on Generate button !
Customize generated files
Back
AppService
- Open the generated application service MyCompany.MyFirstProject.Application\Plane\EngineAppService.cs
- Delete the field declaration and assignement of
IEngineRepository repository
- Replace
IEngineRepository
type of constructor parameterrepository
byITGenericRepository<Engine, int>
Front
model.ts
- Go in 'src\app\features\planes\children\engines\model' and open the engine.ts file.
- Adapt the field configuration if needed.
- Remove all unused imports from the generated file.
engine-item.component.ts
- Go in 'src\app\features\planes\children\engines\views' and open the engine-item.component.ts file.
- Adapt the field of the item to display in the breadcrump.
Complete traductions
- Go in '...\MyFirstProject\Angular\src\assets\i18n\app'
- Complete each available language traduction JSON file with the correct values :
en.json
"app": {
//...
"engines": "engines"
},
"engine": {
"add": "Add engine",
"edit": "Edit engine",
"listOf": "List of engines",
"plane": "Plane",
"sn": "Serial Number",
},
Testing your CRUD Child
- Run the DotNet solution.
- Launch
npm start
in Angular folder. - Go to http://localhost:4200/
- Navigate to the plane list.
- Select one plane (create one if needed) and click on the button "Engines":
- You should access to the engines list of the plane.