I have an interface defined as IStore, with two methods:
public interface IStore<TEntity>
{
TEntity Get(object identifier);
void Put(TEntity entity);
}
I want an event to be raised on the success of Put (for reference, Put could store a row in a db, or file on the file system etc...)
So, a class implementing Istore for type of Product would look a bit like this:
class MyStore : IStore<Product>
{
public Product Get(object identifier)
{
//whatever
}
public void Put(Product entity)
{
//Store the product in db
//RAISE EVENT ON SUCCESS
}
}
What i'm after, is a way of ensuring that every implementation of IStore raises the event - should i have an abstract class instead, or as well as, the interface?
my suggestion:
public abstract class Store<TEntity>
{
public abstract TEntity Get(object identifier);
public void Put(TEntity entity)
{
//Do actions before call
InternalPut(entity);
//Raise event or other postprocessing
}
protected abstract void InternalPut(TEntity entity);
}
then override InternalPut
in your class
There is really no way of ensuring that every implementation of IStore raises an event. You could have an abstract class that has the put method, but that still doesn't mean that you could have a put method in a subclass of the abstract class that totally ignores the abstract class's method.
In the end, the best way to encourage the raising of the event is to write the method the developer should use, by way of an abstract class. That way, they have to go out of their way not to use it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With