Are you the worst?
Are you the resident genius on your team? Do you wrap wrappers in wrappers and frame frameworks in frameworks? Have you ignored/obscured whole swaths of .NET because your application is a special, special unicorn? Well, I’ve got some bad news. You’re the worst.
As for the rest of you, come with me. We’re going to painlessly add some business logic to an Entity Framework model.

Meddle’s IAddable, IUpdatable, and IDeletable interfaces respectively provide OnAdding, OnUpdating, and OnDeleting methods for your entity’s partial class to implement. As the naming would suggest, this is where you do your custom unicorn-type things to make your model sing and dance:
public partial class Product : IDeletable , IAddable
{
public void OnDeleting(IWork work)
{
if (this.PurchaseOrderDetails.Any())
throw new ApplicationException(
"This product is a special unicorn and it cannot be deleted." );
}
public void OnAdding(IWork work)
{
if (UserPreferences.NoDuplicateProductNames)
{
// the context is available via the work parameter
// for testing more intense rules involving other
// data in the system
var context = work as SpecialUnicornEntities;
if (context.Products.Count(p => p.Name.Equals(this.Name)) > 1)
{
var message = "A product with the same name already exists.";
if (UserPreferences.ErrorMessageIntensity == ErrorMessageIntensity.Abusive)
message += " Be more creative next time. Loser.";
throw new ApplicationException(message);
}
}
}
}
[ Meddle on GitHub ]
[ Meddle on NuGet ]