I am trying to extend the AbpUserNotifications
table to make it include some extra columns (including IsDeleted
, DeletionTime
).
So far, it worked and I now have the columns I need.
But, as you may know, a column called Discriminator
is auto-generated to register this record to which class it is related (parent or sub).
The issue is when a notification is published, it is published for the parent class. So when I retrieve the data using the subclass, it is not returned in the result because the Discriminator
column has the parent class name and not the subclass name.
Is there a way where I can retrieve the data using the subclass even though it was published for the parent class? (Meaning to ignore the value of the Discriminator
when retrieving the data).
The application is using the built-in publishing of ASP.NET Boilerplate and all data-level interactions are done using the IRepository
provided by the framework.
Here is the extended class code:
public class ExtendedUserNotification : UserNotificationInfo
{
public virtual bool IsDeleted { get; set; }
public virtual long? DeleterUserId { get; set; }
public virtual DateTime? DeletionTime { get; set; }
public virtual string ExtraInformation { get; set; }
[ForeignKey("TenantNotificationId")]
public virtual TenantNotificationInfo TenantNotificationInfo { get; set; }
}
As I mentioned above, the actual result is that when I retrieve data using the subclass, it does not return data of the parent class.
The expected and wanted result is to be able to use the subclass to retrieve data of the parent class, as I need to apply some filters using the new columns when retrieving the data.
Thank you.
This seems to be an XY Problem.
You should implement a custom Notification Store that overrides the relevant methods.
Override InsertUserNotificationAsync
to insert (publish) an ExtendedUserNotification
instead.
public class ExtendedUserNotificationStore : NotificationStore
{
private readonly IRepository<ExtendedUserNotification, Guid> _extendedUserNotificationRepository;
private readonly IObjectMapper _objectMapper;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public ExtendedUserNotificationStore(
IRepository<NotificationInfo, Guid> notificationRepository, IRepository<TenantNotificationInfo, Guid> tenantNotificationRepository, IRepository<UserNotificationInfo, Guid> userNotificationRepository, IRepository<NotificationSubscriptionInfo, Guid> notificationSubscriptionRepository,
IRepository<ExtendedUserNotification, Guid> extendedUserNotificationRepository,
IObjectMapper objectMapper,
IUnitOfWorkManager unitOfWorkManager)
: base(notificationRepository, tenantNotificationRepository, userNotificationRepository, notificationSubscriptionRepository, unitOfWorkManager)
{
_extendedUserNotificationRepository = extendedUserNotificationRepository;
_objectMapper = objectMapper;
_unitOfWorkManager = unitOfWorkManager;
}
[UnitOfWork]
public override async Task InsertUserNotificationAsync(UserNotificationInfo userNotification)
{
var extendedUserNotification = _objectMapper.Map<ExtendedUserNotification>(userNotification);
using (_unitOfWorkManager.Current.SetTenantId(userNotification.TenantId))
{
await _extendedUserNotificationRepository.InsertAsync(extendedUserNotification);
await _unitOfWorkManager.Current.SaveChangesAsync();
}
}
}
If you want to use IObjectMapper
, then configure the mapping easily with AutoMapFrom
attribute.
[AutoMapFrom(typeof(UserNotificationInfo))] // Add this
public class ExtendedUserNotification : UserNotificationInfo
{
// ...
}
No, there is no supported way.