abp-entity-patterns

Domain layer patterns for ABP Framework following DDD principles.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "abp-entity-patterns" with this command: npx skills add thapaliyabikendra/ai-artifacts/thapaliyabikendra-ai-artifacts-abp-entity-patterns

ABP Entity Patterns

Domain layer patterns for ABP Framework following DDD principles.

Architecture Layers

Domain.Shared → Constants, enums, shared types Domain → Entities, repositories, domain services, domain events Application.Contracts → DTOs, application service interfaces Application → Application services, mapper profiles EntityFrameworkCore → DbContext, repository implementations HttpApi → Controllers HttpApi.Host → Startup, configuration

Key principle: Dependencies flow downward. Application depends on Domain, but Domain never depends on Application.

Entity Base Classes

Choosing the Right Base Class

Base Class Use When

Entity<TKey>

Simple entity, no auditing

AuditedEntity<TKey>

Need creation/modification tracking

FullAuditedEntity<TKey>

Need soft delete + full audit

AggregateRoot<TKey>

Root entity of an aggregate

FullAuditedAggregateRoot<TKey>

Most common - full features

Standard Entity Pattern

public class Patient : FullAuditedAggregateRoot<Guid> { public string FirstName { get; private set; } public string LastName { get; private set; } public string Email { get; private set; } public DateTime DateOfBirth { get; private set; } public bool IsActive { get; private set; }

// Required for EF Core
protected Patient() { }

// Constructor with validation
public Patient(
    Guid id,
    string firstName,
    string lastName,
    string email,
    DateTime dateOfBirth)
    : base(id)
{
    SetName(firstName, lastName);
    SetEmail(email);
    DateOfBirth = dateOfBirth;
    IsActive = true;
}

// Domain methods with validation
public void SetName(string firstName, string lastName)
{
    FirstName = Check.NotNullOrWhiteSpace(firstName, nameof(firstName), maxLength: 100);
    LastName = Check.NotNullOrWhiteSpace(lastName, nameof(lastName), maxLength: 100);
}

public void SetEmail(string email)
{
    Email = Check.NotNullOrWhiteSpace(email, nameof(email), maxLength: 256);
}

public void Activate() => IsActive = true;
public void Deactivate() => IsActive = false;

}

Soft Delete

public class Patient : FullAuditedAggregateRoot<Guid>, ISoftDelete { public bool IsDeleted { get; set; } // ABP automatically filters out soft-deleted entities }

Multi-Tenancy

public class Patient : FullAuditedAggregateRoot<Guid>, IMultiTenant { public Guid? TenantId { get; set; } // ABP automatically filters by current tenant }

Audit Fields

FullAuditedAggregateRoot<Guid> provides:

  • CreationTime , CreatorId

  • LastModificationTime , LastModifierId

  • IsDeleted , DeletionTime , DeleterId

Repository Pattern

Generic Repository Usage

public class PatientAppService : ApplicationService { private readonly IRepository<Patient, Guid> _patientRepository;

public PatientAppService(IRepository&#x3C;Patient, Guid> patientRepository)
{
    _patientRepository = patientRepository;
}

public async Task&#x3C;PatientDto> GetAsync(Guid id)
{
    var patient = await _patientRepository.GetAsync(id);
    return ObjectMapper.Map&#x3C;Patient, PatientDto>(patient);
}

public async Task&#x3C;PagedResultDto&#x3C;PatientDto>> GetListAsync(PagedAndSortedResultRequestDto input)
{
    var totalCount = await _patientRepository.GetCountAsync();
    var queryable = await _patientRepository.GetQueryableAsync();

    var patients = await AsyncExecuter.ToListAsync(
        queryable
            .OrderBy(input.Sorting ?? nameof(Patient.FirstName))
            .PageBy(input.SkipCount, input.MaxResultCount));

    return new PagedResultDto&#x3C;PatientDto>(
        totalCount,
        ObjectMapper.Map&#x3C;List&#x3C;Patient>, List&#x3C;PatientDto>>(patients));
}

}

Custom Repository

Define interface in Domain layer:

public interface IPatientRepository : IRepository<Patient, Guid> { Task<List<Patient>> GetActivePatientsByDoctorAsync(Guid doctorId); Task<Patient?> FindByEmailAsync(string email); }

Implement in EntityFrameworkCore layer:

public class PatientRepository : EfCoreRepository<ClinicDbContext, Patient, Guid>, IPatientRepository { public PatientRepository(IDbContextProvider<ClinicDbContext> dbContextProvider) : base(dbContextProvider) { }

public async Task&#x3C;List&#x3C;Patient>> GetActivePatientsByDoctorAsync(Guid doctorId)
{
    var dbSet = await GetDbSetAsync();
    return await dbSet
        .Where(p => p.PrimaryDoctorId == doctorId &#x26;&#x26; p.IsActive)
        .Include(p => p.Appointments)
        .ToListAsync();
}

public async Task&#x3C;Patient?> FindByEmailAsync(string email)
{
    var dbSet = await GetDbSetAsync();
    return await dbSet.FirstOrDefaultAsync(p => p.Email == email);
}

}

Domain Services

Use domain services when business logic involves multiple entities or external domain concepts.

public class AppointmentManager : DomainService { private readonly IRepository<Appointment, Guid> _appointmentRepository; private readonly IRepository<DoctorSchedule, Guid> _scheduleRepository;

public AppointmentManager(
    IRepository&#x3C;Appointment, Guid> appointmentRepository,
    IRepository&#x3C;DoctorSchedule, Guid> scheduleRepository)
{
    _appointmentRepository = appointmentRepository;
    _scheduleRepository = scheduleRepository;
}

public async Task&#x3C;Appointment> CreateAsync(
    Guid patientId,
    Guid doctorId,
    DateTime appointmentDate,
    string description)
{
    // Business rule: Check if doctor is available
    await CheckDoctorAvailabilityAsync(doctorId, appointmentDate);

    // Business rule: Check for conflicts
    await CheckAppointmentConflictsAsync(doctorId, appointmentDate);

    var appointment = new Appointment(
        GuidGenerator.Create(),
        patientId,
        doctorId,
        appointmentDate,
        description);

    return await _appointmentRepository.InsertAsync(appointment);
}

private async Task CheckDoctorAvailabilityAsync(Guid doctorId, DateTime appointmentDate)
{
    var schedule = await _scheduleRepository.FirstOrDefaultAsync(
        s => s.DoctorId == doctorId &#x26;&#x26; s.DayOfWeek == appointmentDate.DayOfWeek);

    if (schedule == null)
        throw new BusinessException("Doctor not available on this day");

    var timeOfDay = appointmentDate.TimeOfDay;
    if (timeOfDay &#x3C; schedule.StartTime || timeOfDay > schedule.EndTime)
        throw new BusinessException("Doctor not available at this time");
}

private async Task CheckAppointmentConflictsAsync(Guid doctorId, DateTime appointmentDate)
{
    var hasConflict = await _appointmentRepository.AnyAsync(a =>
        a.DoctorId == doctorId &#x26;&#x26;
        a.AppointmentDate == appointmentDate &#x26;&#x26;
        a.Status != AppointmentStatus.Cancelled);

    if (hasConflict)
        throw new BusinessException("Doctor already has an appointment at this time");
}

}

Data Seeding

IDataSeedContributor Pattern

public class ClinicDataSeedContributor : IDataSeedContributor, ITransientDependency { private readonly IRepository<Doctor, Guid> _doctorRepository; private readonly IGuidGenerator _guidGenerator;

public ClinicDataSeedContributor(
    IRepository&#x3C;Doctor, Guid> doctorRepository,
    IGuidGenerator guidGenerator)
{
    _doctorRepository = doctorRepository;
    _guidGenerator = guidGenerator;
}

public async Task SeedAsync(DataSeedContext context)
{
    // Idempotent check
    if (await _doctorRepository.GetCountAsync() > 0)
        return;

    var doctors = new List&#x3C;Doctor>
    {
        new Doctor(_guidGenerator.Create(), "Dr. Smith", "Cardiology", "smith@clinic.com"),
        new Doctor(_guidGenerator.Create(), "Dr. Jones", "Pediatrics", "jones@clinic.com"),
    };

    foreach (var doctor in doctors)
    {
        await _doctorRepository.InsertAsync(doctor);
    }
}

}

Test Data Seeding

public class ClinicTestDataSeedContributor : IDataSeedContributor, ITransientDependency { public static readonly Guid TestPatientId = Guid.Parse("2e701e62-0953-4dd3-910b-dc6cc93ccb0d"); public static readonly Guid TestDoctorId = Guid.Parse("3a801f73-1064-5ee4-a21c-ed7dd4ddc1e");

public async Task SeedAsync(DataSeedContext context)
{
    await _patientRepository.InsertAsync(new Patient(
        TestPatientId, "Test", "Patient", "test@example.com", DateTime.Now.AddYears(-30)));

    await _doctorRepository.InsertAsync(new Doctor(
        TestDoctorId, "Test Doctor", "General", "doctor@example.com"));
}

}

Best Practices

  • Encapsulate state - Use private setters and domain methods

  • Validate in constructor - Ensure entity is always valid

  • Use value objects - For complex properties (Address, Money)

  • Domain logic in entity - Simple rules belong in the entity

  • Domain service - For cross-entity logic

  • Custom repository - Only when you need custom queries

  • Idempotent seeding - Always check before inserting

Related Skills

  • abp-service-patterns

  • Application layer patterns

  • abp-infrastructure-patterns

  • Cross-cutting concerns

  • efcore-patterns

  • Database configuration

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

abp-infrastructure-patterns

No summary provided by upstream source.

Repository SourceNeeds Review
General

abp-api-implementation

No summary provided by upstream source.

Repository SourceNeeds Review
General

abp-service-patterns

No summary provided by upstream source.

Repository SourceNeeds Review
General

abp-contract-scaffolding

No summary provided by upstream source.

Repository SourceNeeds Review