mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-04 00:34:26 -05:00 
			
		
		
		
	UnitOfWork compltely removed. GetDbContext now returns a NadekoContext. Changed every access to contect via uow._context to uow
This commit is contained in:
		@@ -108,7 +108,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var gc = uow._context.GuildConfigsForId(guild.Id, set => set);
 | 
			
		||||
                var gc = uow.GuildConfigsForId(guild.Id, set => set);
 | 
			
		||||
                gc.Prefix = prefix;
 | 
			
		||||
                uow.SaveChanges();
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,14 +0,0 @@
 | 
			
		||||
using NadekoBot.Core.Services.Database.Repositories;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Core.Services.Database
 | 
			
		||||
{
 | 
			
		||||
    public interface IUnitOfWork : IDisposable
 | 
			
		||||
    {
 | 
			
		||||
        NadekoContext _context { get; } 
 | 
			
		||||
 | 
			
		||||
        int SaveChanges();
 | 
			
		||||
        Task<int> SaveChangesAsync();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,11 +0,0 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using NadekoBot.Core.Services.Database.Models;
 | 
			
		||||
using NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Services.Database.Repositories
 | 
			
		||||
{
 | 
			
		||||
    public interface IRepository<T> where T : DbEntity
 | 
			
		||||
    {
 | 
			
		||||
        void Add(T obj);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,23 +0,0 @@
 | 
			
		||||
using Microsoft.EntityFrameworkCore;
 | 
			
		||||
using NadekoBot.Services.Database.Models;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using NadekoBot.Core.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Services.Database.Repositories.Impl
 | 
			
		||||
{
 | 
			
		||||
    public abstract class Repository<T> : IRepository<T> where T : DbEntity
 | 
			
		||||
    {
 | 
			
		||||
        protected DbContext _context { get; set; }
 | 
			
		||||
        protected DbSet<T> _set { get; set; }
 | 
			
		||||
 | 
			
		||||
        public Repository(DbContext context)
 | 
			
		||||
        {
 | 
			
		||||
            _context = context;
 | 
			
		||||
            _set = context.Set<T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Add(T obj) =>
 | 
			
		||||
            _set.Add(obj);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,31 +0,0 @@
 | 
			
		||||
using NadekoBot.Services.Database.Repositories;
 | 
			
		||||
using NadekoBot.Services.Database.Repositories.Impl;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using NadekoBot.Core.Services.Database;
 | 
			
		||||
using NadekoBot.Core.Services.Database.Repositories;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Services.Database
 | 
			
		||||
{
 | 
			
		||||
    public sealed class UnitOfWork : IUnitOfWork
 | 
			
		||||
    {
 | 
			
		||||
        public NadekoContext _context { get; }
 | 
			
		||||
 | 
			
		||||
        public UnitOfWork(NadekoContext context)
 | 
			
		||||
        {
 | 
			
		||||
            _context = context;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public int SaveChanges() =>
 | 
			
		||||
            _context.SaveChanges();
 | 
			
		||||
 | 
			
		||||
        public Task<int> SaveChangesAsync() =>
 | 
			
		||||
            _context.SaveChangesAsync();
 | 
			
		||||
 | 
			
		||||
        public void Dispose()
 | 
			
		||||
        {
 | 
			
		||||
            _context.Dispose();
 | 
			
		||||
            GC.SuppressFinalize(this);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -57,6 +57,6 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
            return context;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public IUnitOfWork GetDbContext() => new UnitOfWork(GetDbContextInternal());
 | 
			
		||||
        public NadekoContext GetDbContext() => GetDbContextInternal();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -107,7 +107,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
        {
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                return uow._context.GuildConfigsForId(id, set => set)?.DmGreetMessageText;
 | 
			
		||||
                return uow.GuildConfigsForId(id, set => set)?.DmGreetMessageText;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -115,7 +115,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
        {
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                return uow._context.GuildConfigsForId(gid, set => set).ChannelGreetMessageText;
 | 
			
		||||
                return uow.GuildConfigsForId(gid, set => set).ChannelGreetMessageText;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -319,7 +319,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
        {
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                return uow._context.GuildConfigsForId(gid, set => set).ChannelByeMessageText;
 | 
			
		||||
                return uow.GuildConfigsForId(gid, set => set).ChannelByeMessageText;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -331,7 +331,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var gc = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var gc = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                settings = GreetSettings.Create(gc);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@@ -351,7 +351,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                conf.DmGreetMessageText = settings.DmGreetMessageText?.SanitizeMentions();
 | 
			
		||||
                conf.ChannelGreetMessageText = settings.ChannelGreetMessageText?.SanitizeMentions();
 | 
			
		||||
                conf.ChannelByeMessageText = settings.ChannelByeMessageText?.SanitizeMentions();
 | 
			
		||||
@@ -382,7 +382,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
            bool enabled;
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                enabled = conf.SendChannelGreetMessage = value ?? !conf.SendChannelGreetMessage;
 | 
			
		||||
                conf.GreetMessageChannelId = channelId;
 | 
			
		||||
 | 
			
		||||
@@ -404,7 +404,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
            bool greetMsgEnabled;
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                conf.ChannelGreetMessageText = message;
 | 
			
		||||
                greetMsgEnabled = conf.SendChannelGreetMessage;
 | 
			
		||||
 | 
			
		||||
@@ -421,7 +421,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
            bool enabled;
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                enabled = conf.SendDmGreetMessage = value ?? !conf.SendDmGreetMessage;
 | 
			
		||||
 | 
			
		||||
                var toAdd = GreetSettings.Create(conf);
 | 
			
		||||
@@ -437,7 +437,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
        {
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                return conf.SendDmGreetMessage;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@@ -446,7 +446,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
        {
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                return conf.SendChannelGreetMessage;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@@ -455,7 +455,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
        {
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                return conf.SendChannelByeMessage;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@@ -492,7 +492,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
            bool greetMsgEnabled;
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                conf.DmGreetMessageText = message;
 | 
			
		||||
                greetMsgEnabled = conf.SendDmGreetMessage;
 | 
			
		||||
 | 
			
		||||
@@ -509,7 +509,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
            bool enabled;
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                enabled = conf.SendChannelByeMessage = value ?? !conf.SendChannelByeMessage;
 | 
			
		||||
                conf.ByeMessageChannelId = channelId;
 | 
			
		||||
 | 
			
		||||
@@ -531,7 +531,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
            bool byeMsgEnabled;
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                conf.ChannelByeMessageText = message;
 | 
			
		||||
                byeMsgEnabled = conf.SendChannelByeMessage;
 | 
			
		||||
 | 
			
		||||
@@ -550,7 +550,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                conf.AutoDeleteByeMessagesTimer = timer;
 | 
			
		||||
 | 
			
		||||
                var toAdd = GreetSettings.Create(conf);
 | 
			
		||||
@@ -567,7 +567,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var conf = uow._context.GuildConfigsForId(id, set => set);
 | 
			
		||||
                var conf = uow.GuildConfigsForId(id, set => set);
 | 
			
		||||
                conf.AutoDeleteGreetMessagesTimer = timer;
 | 
			
		||||
 | 
			
		||||
                var toAdd = GreetSettings.Create(conf);
 | 
			
		||||
 
 | 
			
		||||
@@ -33,19 +33,19 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
        private bool InternalChange(ulong userId, string userName, string discrim, string avatar,
 | 
			
		||||
            string reason, long amount, bool gamble, IUnitOfWork uow)
 | 
			
		||||
            string reason, long amount, bool gamble, NadekoContext uow)
 | 
			
		||||
        {
 | 
			
		||||
            var result = uow._context.TryUpdateCurrencyState(userId, userName, discrim, avatar, amount);
 | 
			
		||||
            var result = uow.TryUpdateCurrencyState(userId, userName, discrim, avatar, amount);
 | 
			
		||||
            if (result)
 | 
			
		||||
            {
 | 
			
		||||
                var t = GetCurrencyTransaction(userId, reason, amount);
 | 
			
		||||
                uow._context.CurrencyTransactions.Add(t);
 | 
			
		||||
                uow.CurrencyTransactions.Add(t);
 | 
			
		||||
 | 
			
		||||
                if (gamble)
 | 
			
		||||
                {
 | 
			
		||||
                    var t2 = GetCurrencyTransaction(_bot.Id, reason, -amount);
 | 
			
		||||
                    uow._context.CurrencyTransactions.Add(t2);
 | 
			
		||||
                    uow._context.TryUpdateCurrencyState(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId, -amount, true);
 | 
			
		||||
                    uow.CurrencyTransactions.Add(t2);
 | 
			
		||||
                    uow.TryUpdateCurrencyState(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId, -amount, true);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return result;
 | 
			
		||||
 
 | 
			
		||||
@@ -56,7 +56,7 @@ namespace NadekoBot.Core.Services.Impl
 | 
			
		||||
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                var gc = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                var gc = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                gc.Locale = ci.Name;
 | 
			
		||||
                uow.SaveChanges();
 | 
			
		||||
            }
 | 
			
		||||
@@ -74,7 +74,7 @@ namespace NadekoBot.Core.Services.Impl
 | 
			
		||||
            {
 | 
			
		||||
                using (var uow = _db.GetDbContext())
 | 
			
		||||
                {
 | 
			
		||||
                    var gc = uow._context.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                    var gc = uow.GuildConfigsForId(guildId, set => set);
 | 
			
		||||
                    gc.Locale = null;
 | 
			
		||||
                    uow.SaveChanges();
 | 
			
		||||
                }
 | 
			
		||||
 
 | 
			
		||||
@@ -136,7 +136,7 @@ namespace NadekoBot
 | 
			
		||||
        public IEnumerable<GuildConfig> GetCurrentGuildConfigs()
 | 
			
		||||
        {
 | 
			
		||||
            using var uow = _db.GetDbContext();
 | 
			
		||||
            return uow._context.GuildConfigs.GetAllGuildConfigs(GetCurrentGuildIds()).ToImmutableArray();
 | 
			
		||||
            return uow.GuildConfigs.GetAllGuildConfigs(GetCurrentGuildIds()).ToImmutableArray();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void AddServices()
 | 
			
		||||
@@ -147,8 +147,8 @@ namespace NadekoBot
 | 
			
		||||
 | 
			
		||||
            using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                uow._context.EnsureUserCreated(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId);
 | 
			
		||||
                AllGuildConfigs = uow._context.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
 | 
			
		||||
                uow.EnsureUserCreated(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId);
 | 
			
		||||
                AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var s = new ServiceCollection()
 | 
			
		||||
@@ -314,7 +314,7 @@ namespace NadekoBot
 | 
			
		||||
                GuildConfig gc;
 | 
			
		||||
                using (var uow = _db.GetDbContext())
 | 
			
		||||
                {
 | 
			
		||||
                    gc = uow._context.GuildConfigsForId(arg.Id);
 | 
			
		||||
                    gc = uow.GuildConfigsForId(arg.Id);
 | 
			
		||||
                }
 | 
			
		||||
                await JoinedGuild.Invoke(gc).ConfigureAwait(false);
 | 
			
		||||
            });
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
        public void EnsureMigrated()
 | 
			
		||||
        {
 | 
			
		||||
            using var uow = _db.GetDbContext();
 | 
			
		||||
            using var conn = uow._context.Database.GetDbConnection();
 | 
			
		||||
            using var conn = uow.Database.GetDbConnection();
 | 
			
		||||
            
 | 
			
		||||
            // check if bot config exists
 | 
			
		||||
            using (var checkTableCommand = conn.CreateCommand())
 | 
			
		||||
 
 | 
			
		||||
@@ -21,7 +21,7 @@ namespace NadekoBot.Core.Services
 | 
			
		||||
        public void EnsureMigrated()
 | 
			
		||||
        {
 | 
			
		||||
            using var uow = _db.GetDbContext();
 | 
			
		||||
            using var conn = uow._context.Database.GetDbConnection();
 | 
			
		||||
            using var conn = uow.Database.GetDbConnection();
 | 
			
		||||
            Migrate(conn);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user