5.9 KiB
5.9 KiB
Conventions
Naming
Mgprefix for all custom types:MgEntityBase,MgOrderDto,MgDbTableBase,MgDalBase, etc.I+ name for interfaces:IMgDalBase,IMgDbTableBase,IMgOrderDto,IMgLockService.Dtosuffix for DTOs wrapping nopCommerce entities:MgOrderDto,MgProductDto,MgOrderItemDto.DbTablesuffix for repository classes:MgDbTableBase,MgDtoDbTableBase.Basesuffix for abstract base classes:MgEntityBase,ModelDtoBase,MgBackgroundServiceBase.Nopprefix for nopCommerce bridge types:NopLogWriter,NopLoggerMsSqlNopDataProvider,NopCommonConst.
XML Documentation
<summary> — brief, developer-facing, readable in VS IntelliSense tooltip. NO implementation details, NO wire-format / byte-level / perf specifics — those live in docs/TOPIC/*.md. Add <example> only when usage is non-obvious; otherwise omit.
Patterns
DTO Bidirectional Mapping
ModelDtoBase<TMainEntity>providesCopyEntityValuesToDto(entity),CopyDtoValuesToEntity(entity), andCreateMainEntity(). Override all three in concrete DTOs.- Simple DTOs (e.g.
CustomerDto) — manual property-by-property copy in overrides. - Complex DTOs (e.g.
MgOrderDto) — usePropertyHelper.CopyPublicValueTypeProperties(source, target)for automatic value-type property copy, then manually set reference/navigation properties.
LinqToDB Associations
- DTOs with navigation properties use
[Association(ThisKey, OtherKey, CanBeNull)]from LinqToDB. ThisKey= local FK property name,OtherKey= target entity property name (typicallyIdor via interface member likenameof(IMgProductDto.Id)).
NopDependencies Mirror
- Entity classes in
NopDependencies/use the same namespace as the original nopCommerce types. Do not change namespaces. - All mirror classes are
partial— they can be extended but should not be modified directly.
GenericAttribute Typed Access
- Use
GenericAttributeExtensions.GetValueOrDefault<T>()/GetValueOrNull<T>()/TryGetValue<T>()instead of raw string parsing. - Use
AddNewGenericAttribute()to create new attributes with automatic UTC timestamp.
Repository Base Chain
MgDbTableBase<TEntity>→EntityRepository<TEntity>(nopCommerce). Override virtualOnInsert/OnUpdate/OnDeletehooks, don't replace the chain.MgDtoDbTableBase<TDtoEntity, TMainEntity>→MgDbTableBase<TDtoEntity>. Never call Delete directly — always useDeleteMainEntityById(int id).
Timestamp Interfaces
- Entities implementing
ITimeStampCreatedgetCreated = DateTime.UtcNowon insert. - Entities implementing
ITimeStampModifiedgetModified = DateTime.UtcNowon insert and update. ITimeStampInfocombines both (Creator,Created,Modified).
Transaction Pattern
- Use
MgDbContextBase.TransactionSafeAsync()for operations needing global lock (order creation, stock adjustment). - Callback returns
bool—truecommits,falserolls back. - Exceptions are caught and logged by default (return
false). PassthrowException: trueonly for fail-fast scenarios.
Event Consumer Pattern
- Inherit
MgEventConsumerBaseand override relevantHandleEventAsyncmethods. - Base class handles DI of
IMgDbContextBase,IHttpContextAccessor, andIAcLogWriterBase[]. - Base provides
CheckAndUpdateProductManageInventoryMethodToManageStock(Product)helper.
Session Pattern
- Inherit
MgSessionServiceBase<TSessionItem>andMgSessionItemBase. - Sessions stored in
ConcurrentDictionary<string, TSessionItem>— thread-safe, in-memory only. - Session items track
SessionId,SignaRConnectionId,RequestCount.
Logging
- Base logging infrastructure (
IAcLoggerBase,IAcLogWriterBase,AcLoggerBase,AcLogItemWriterBase) — seeAyCode.Core/AyCode.Core/docs/LOGGING/README.md. - Services create loggers via
new Logger<TCategory>(logWriters.ToArray())in constructor. logWritersinjected asIEnumerable<IAcLogWriterBase>— typically containsNopLogWriter+ other writers.- Exception:
MgBackgroundServiceBaseusesNop.Services.Logging.ILoggerdirectly (nopCommerce logger), not the Mango wrapper.
Serialization Attributes
[ToonDescription(...)]— AyCode metadata for AI/doc tooling.Purpose,BusinessRule,TypeRelation,RelatedTypesproperties.[AcBinarySerializable(false, true, false, true, false)]— AcBinarySerializer config (seeAyCode.Core/AyCode.Core/docs/BINARY/BINARY_FORMAT.md). Parameters control serialization behavior for AcSignalR transport (seeAyCode.Core/AyCode.Services/docs/SIGNALR/README.md).- LinqToDB
[Table(Name = "...")]and[Association(...)]— DB mapping.
Project Boundaries
Mango.Nop.Core— NO nopCommerce runtime dependency. Only NopDependencies mirrors. NoNop.Data,Nop.Servicesreferences.Mango.Nop.Data— depends on nopCommerce data layer (Nop.Core,Nop.Data). NoNop.Servicesreference.Mango.Nop.Services— depends on full nopCommerce stack (includesNop.Services,Nop.Web.Framework).
AyCode Integration Points
Key AyCode types used across these libraries:
IEntityInt(AyCode.Interfaces.Entities) —int Identity contractIBaseEntity— defined locally in NopDependencies, mirrors AyCode's conceptIAcLoggerBase,IAcLogWriterBase,AcLoggerBase,AcLogItemWriterBase— logging infrastructure (seeAyCode.Core/AyCode.Core/docs/LOGGING/README.md)IAcModelDtoBaseEmpty— DTO marker interfaceIAcSoftRemoveEntity,IAcSoftRemoveEntityInt— soft-delete contractsIForeignKey,IForeignCollection<T>— FK marker interfacesITimeStampCreated,ITimeStampModified,ITimeStampInfo— timestamp contractsPropertyHelper.CopyPublicValueTypeProperties()— reflection-based property copyTaskHelper.ToThreadPoolTask()— wraps async work in thread poolAcConst— abstract constants baseToonDescription— metadata attribute for AI tooling