Init
- aggiunto un po di tutto comeil progetto del prof
This commit is contained in:
41
SeniorAssistant/Extensions/DataConnectionExtension.cs
Normal file
41
SeniorAssistant/Extensions/DataConnectionExtension.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using LinqToDB;
|
||||
using LinqToDB.Data;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SeniorAssistant.Extensions
|
||||
{
|
||||
public static class DataConnectionExtensions
|
||||
{
|
||||
public static void CreateTableIfNotExists<T>(this DataConnection db) => RunIgnoringException(() => db.CreateTable<T>());
|
||||
|
||||
public static async Task CreateTableIfNotExistsAsync<T>(this DataConnection db) => await RunIgnoringExceptionAsync(async () => await db.CreateTableAsync<T>());
|
||||
|
||||
static void RunIgnoringException(Action action)
|
||||
{
|
||||
if (action == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
action.Invoke();
|
||||
}
|
||||
catch (SqliteException)
|
||||
{
|
||||
// Ignore Exception
|
||||
}
|
||||
}
|
||||
|
||||
static async Task RunIgnoringExceptionAsync(Func<Task> action)
|
||||
{
|
||||
if (action == null) await Task.Yield();
|
||||
|
||||
try
|
||||
{
|
||||
await action.Invoke();
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
}
|
||||
}
|
||||
25
SeniorAssistant/Extensions/EnumExtensions.cs
Normal file
25
SeniorAssistant/Extensions/EnumExtensions.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace SeniorAssistant.Extensions
|
||||
{
|
||||
public static class EnumExtensions
|
||||
{
|
||||
public static string GetDescription(this Enum @enum)
|
||||
{
|
||||
var type = @enum.GetType();
|
||||
var memInfo = type.GetMember(@enum.ToString());
|
||||
|
||||
if (memInfo != null && memInfo.Length > 0)
|
||||
{
|
||||
var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
if (attrs != null && attrs.Length > 0)
|
||||
{
|
||||
return ((DescriptionAttribute)attrs[0]).Description;
|
||||
}
|
||||
}
|
||||
|
||||
return @enum.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user