ABP - 简单的实现DDD

Acme.BookStore.Application

Application\Publishers\PublisherAppService.cs

image

Application\BookStoreApplicationAutoMapperProfile.cs

image

Acme.BookStore.Application.Contracts

Application.Contracts\Publishers\CreateUpdatePublisherDto.cs

image

Application.Contracts\Publishers\IPublisherAppService.cs

image

Application.Contracts\Publishers\PublisherDto.cs

image

Acme.BookStore.Domain

Domain\Publishers\Publisher.cs

image

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.Domain.Values;

namespace Acme.BookStore.Publishers
{
    public class Publisher : FullAuditedAggregateRoot<Guid>
    {
        [Required]
        [StringLength(PublisherConsts.MaxNameLength)]
        public string Name { get; private set; }

        [StringLength(PublisherConsts.MaxDescriptionLength)]
        public string Description { get; private set; }

        public DateTime EstablishmentTime { get; private set; }
        public Address Address { get; private set; } // 改为只读

        [Url]
        [StringLength(PublisherConsts.MaxUrlLength)]
        public string Url { get; private set; }
        //public string FormattedAddress => Address != null
        //? $"{Address.Street}, {Address.City}, {Address.StateOrProvince}, {Address.PostalCode}, {Address.Country}"
        //: string.Empty;

        private Publisher() { } // 为ORM保留构造

        public Publisher(
            Guid id,
            string name,
            DateTime establishmentTime,
            Address address,
            string description = null,
            string url = null)
        {
            Id = id;
            SetName(name);
            EstablishmentTime = establishmentTime;
            UpdateAddress(address);
            Description = description;
            Url = url;
        }

        public void SetName(string name)
        {
            Name = Check.NotNullOrWhiteSpace(name, nameof(name));
        }

        public void UpdateAddress(Address address)
        {
            Address = Check.NotNull(address, nameof(address));
        }
    }

    public class Address : ValueObject
    {
        /// <summary>
        /// 街道地址,例如:人民路123号。
        /// </summary>
        public string Street { get; set; }

        /// <summary>
        /// 城市名称,例如:北京市。
        /// </summary>
        public string City { get; set; }

        /// <summary>
        /// 州或省份的名称,例如:加利福尼亚州 或 浙江省。
        /// </summary>
        public string StateOrProvince { get; set; }

        /// <summary>
        /// 邮政编码或邮递区号,例如:100000 或 90210。
        /// </summary>
        public string PostalCode { get; set; }

        /// <summary>
        /// 国家名称,例如:中国 或 美国。
        /// </summary>
        public string Country { get; set; }


        private Address() { }

        public Address(string street, string city, string stateOrProvince, string postalCode, string country)
        {
            Street = street;
            City = city;
            StateOrProvince = stateOrProvince;
            PostalCode = postalCode;
            Country = country;
        }

        // 实现ValueObject的抽象方法,以便正确地比较两个Address对象。
        protected override IEnumerable<object> GetAtomicValues()
        {
            yield return Street;
            yield return City;
            yield return StateOrProvince;
            yield return Country;
            yield return PostalCode;
        }

        // 重写ToString方法,以便在调试时更容易地查看Address对象。
        public override string ToString()
        {
            return $"{Street}, {City}, {StateOrProvince}, {PostalCode}, {Country}";
        }
    }
}

Domain\Publishers\IPublisherRepository.cs

image

Acme.BookStore.EntityFrameworkCore

EntityFrameworkCore\EntityFrameworkCore\BookStoreDbContext.cs

image

EntityFrameworkCore\Publishers\EfCorePublisherRepository.cs

image

posted @ 2026-01-09 10:01  【唐】三三  阅读(41)  评论(0)    收藏  举报