I have a SortedDictionary
of events which people can purchase tickets for. The event code is unique to that ticket which is stored as the key.
protected SortedDictionary<int, Event> Events = new SortedDictionary<int, Event>();
An example of this would look like so:
Events.Add(123, new Event(...));
I implemented a logger to be able to track the Event. Which looks like so:
class EventLogger
{
protected SortedDictionary<int, EventLogType[]> Events = new SortedDictionary<int, EventLogType[]>();
public EventLogger()
{
}
public void Log(int eventCode, EventLogType type)
{
KeyValuePair<int, EventLogType[]> e;
if (Events.ContainsKey(eventCode))
{
(e = Events.FirstOrDefault(x => x.Key.Equals(eventCode)))
.Value[e.Value.Count()] = type;
}
else Events.Add(eventCode, new EventLogType[] { type });
}
}
enum EventLogType
{
PURCHASE,
CREATE,
UPDATE,
DELETE
}
When, for example, I create an event, I have to now call this Log method:
(new EventLogger()).Log(123, EventLogType.CREATE); // EventLogger instance is stored static
Which is becoming a serious pain whenever I want to update, remove, or create. I looked on SO about changing how the set
works for the Array and tried this:
protected SortedDictionary<int, Event> Events { get; set {
this.save();
} } = new SortedDictionary<int, Event>();
But this throws me a IDE error:
Only auto-implemented properties can have initializers.
How can I access the set
call before anything is stored, or updated in the array. How can I check which action is being done? So rather than make a call to the Log separately, it can be done in the .Add
or when its Value is updated or deleted?
My current code looks like this:
Events.Add(123, new Event(...));
Logger.Log(123, EventLogType.CREATE);
Where as I want it to log the create when I just do a call to .Add
Why are you not using inheritance? Create a own for example "MySortedDictionary" which inherits from SortedDictionary and add the needed events to your "MySortedDictionary".
As an example you can try the following, just hacked it down I'm lazy so the class structure with the events isn't optimal.
using System;
using System.Collections.Generic;
namespace SortedDictionrayTest
{
class AddingEventArgs<TKey, TValue> : EventArgs
{
public TKey Key
{
get;
}
public TValue Value
{
get;
}
public AddingEventArgs(TKey key, TValue value)
{
this.Key = key;
this.Value = value;
}
}
class AddedEventArgs<TKey, TValue> : AddingEventArgs<TKey, TValue>
{
public AddedEventArgs(TKey key, TValue value) : base(key, value)
{
}
}
class MySortedDictionay<TKey, TValue> : SortedDictionary<TKey, TValue>
{
public event EventHandler<AddingEventArgs<TKey, TValue>> Adding;
public event EventHandler<AddedEventArgs<TKey, TValue>> Added;
public new void Add(TKey key, TValue value)
{
this.OnAdding(new AddingEventArgs<TKey, TValue>(key, value));
base.Add(key, value);
this.OnAdded(new AddedEventArgs<TKey, TValue>(key, value));
}
protected virtual void OnAdding(AddingEventArgs<TKey, TValue> args)
{
if (this.Adding != null)
{
Adding(this, args);
}
}
protected virtual void OnAdded(AddedEventArgs<TKey,TValue> args)
{
if (this.Added != null)
{
Added(this, args);
}
}
}
class Program
{
static void Main(string[] args)
{
var sortedDict = new MySortedDictionay<int, string>();
sortedDict.Adding += SortedDict_Adding;
sortedDict.Added += SortedDict_Added;
sortedDict.Add(2, "World");
sortedDict.Add(1, "Hello");
Console.WriteLine("---");
foreach (KeyValuePair<int, string> keyValuePair in sortedDict)
{
Console.WriteLine($"key: [{keyValuePair.Key}] value: [{keyValuePair.Value}]");
}
}
private static void SortedDict_Adding(object sender, AddingEventArgs<int, string> e)
{
Console.WriteLine($"before adding key: [{e.Key}] value: [{e.Value}]");
}
private static void SortedDict_Added(object sender, AddedEventArgs<int, string> e)
{
Console.WriteLine($"after adding key: [{e.Key}] value: [{e.Value}]");
}
}
}