interface INotification
{
string Title { get; set; }
string Content { get; set; }
string From { get; set; }
string To { get; set; }
}
interface ISender
{
INotification Notification { get; set; }
SenderTypes Type { get; private set; }
void Send ( );
}
public class SenderBridge
{
private ISender sender;
public SenderBridge(ISender sender)
{
this.sender = sender;
}
public void Send(INotification notification)
{
this.sender.Notification = notification;
this.sender.Send();
}
}
public static class NotificationSenders
{
private static List<ISender> list;
public static void Send(this INotification source, SenderTypes type)
{
if(!Exist(type))
throw new Exception(string.Format("Sender from {0} not found",type.ToString());
var sender = Get(type);
sender.Notification = source;
sender.Send();
}
public static ISender Get(SenderTypes type)
{
return list.Where(s => s.Type.Equals(type)).FirstOrDefault();
}
public static bool Exist(SenderTypes type)
{
return list.Any(s => s.Type.Equals(type));
}
public static void AddSender(ISender sender)
{
if ( !list.Any(s => s.Type.Equals(sender.Type)) )
list.Add(sender);
else
throw new Exception("Duplicate senders");
}
public static void Remove(SenderTypes type)
{
var sender = list.Where(s => s.Type.Equals(type)).FirstOrDefault();
if ( sender != null )
list.Remove(sender);
}
public static void Clear()
{
list.Clear();
}
}
public enum SenderTypes
{
EMail,
SMS
}
public static bool ExistById<T> (this DbSet<T> source, int Id)
{
string sql = source.ToString();
var regex = new Regex("FROM (?<table>.*) AS");
var match =regex.Match(sql);
var tableName = match.Groups["table"].Value;
string query = string.Format("SELECT [dbo].[{0}].[Id] FROM [dbo].[{0}] WHERE [dbo].[{0}].[Id] == {1}", tableName, Id.ToString());
var result = source.SqlQuery(query).FirstOrDefault();
return result != null ? true : false;
}