Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mark 122 posts 255 karma points
    May 12, 2020 @ 12:23
    Mark
    0

    Subscribing to Npoco database events

    Hey guys,

    Sorry if this is in the docs, I have done a bit of searching and have not found an answer as yet...

    I am inserting/updating (or at least will be when this works!) data to custom database tables as per the documentation, but I want to subscribe to Npoco database events (OnInserting, OnUpdating, etc.) so that I can auto-populate things like timestamp fields.

    The example from Npoco shows (https://github.com/schotime/NPoco/wiki/Performing-Operations-during-Poco-events-(Insert,-Update,-etc)):

    public class DatabaseEvents : Database
    {
        public DatabaseEvents(DbConnection connection) : base(connection)
        {
        }
    
        protected override bool OnInserting(InsertContext insertContext)
        {
            if (insertContext.Poco is CommonEntity entity)
            {
                entity.DateInserted = DateTime.UtcNow;
            }
    
            return base.OnInserting(insertContext);
        }
    
        protected override bool OnUpdating(UpdateContext updateContext)
        {
            var entity = updateContext.Poco as CommonEntity;
            if(entity != null)
            {
                entity.DateModified = DateTime.UtcNow;
            }
    
            return base.OnUpdating(updateContext);
        }
    }
    

    But I am pretty sure I need to register this with Umbraco somehow as it doesn't work out of the box...

    Has anyone done this yet with V8, if so, please help! :-)

    Many thanks,

    Mark

  • bradLucifer 1 post 21 karma points
    Oct 18, 2022 @ 13:08
    bradLucifer
    0

    According to my knowledge you have to create a class first and in that class override these events and to trigger these events you have to create that class object and return to your repo layer For example:

         public class AppNameDatabase : Database
            {
    
                public AppNameDatabase (DbConnection connection) : base(connection)
                {
                }
    protected override bool OnInserting(InsertContext insertContext)
        {
            if (insertContext.Poco is CommonEntity entity)
            {
                entity.DateInserted = DateTime.UtcNow;
            }
    
            return base.OnInserting(insertContext);
        }
    
        protected override bool OnUpdating(UpdateContext updateContext)
        {
            var entity = updateContext.Poco as CommonEntity;
            if(entity != null)
            {
                entity.DateModified = DateTime.UtcNow;
            }
    
            return base.OnUpdating(updateContext);
        }
    
        }
    

    At last create a factory class to encapsulate it further

        public class DatabaseFactory : IDatabaseFactory
            {
          readonly DbConnection _connection;
    
                public DatabaseFactory(DbConnection connectionl)
                {
                     _connection=connection;
                }
     public IDatabase GetDbConnection()
            {
                using (var dbContext = new AppNameDatabase (_connection)
                {
                    return dbContext;
                }
            }
        }
    

    You can ask if you have any further questions

Please Sign in or register to post replies

Write your reply to:

Draft