In my scenario, I have two types of objects. A plan and a tier. Each one shares certain values, while the tier has 3 additional values (Rate, Min, and Max).
I would like to make a generic method to conditionally evaluate whether or not the new object can be written to the database, based on the following conditions:
Creation:
- A tier:
((o1.Rate!=o2.Rate) || ((o1.Start!=o2.Start) && (o1.End!=o2.End))) && (MinVal && MaxVal) - A tier or plan:
(o1.Name!=o2.Name) - A tier or plan: Only one can have
(Active == true), the rest must be false.
Is it possible to create a generic method for something like this? If so, are there any examples of something like this? Or does anyone have suggestions?
thanks!
MORE:
So I have a list of objects, and I need to compare a newly created one with each other object in the database before I actually write it to the database.
In order for this to work, the generic method (or type) would need a constraint that allowed it to understand all of the individual properties you have listed (
Rate,Date,MinVal,MaxValue,Start, andEnd).If all of the types which you are worried about derive from the same base class or interface, you don’t need generics – just build a method that takes two arguments of that base type.
If, however, they don’t, again, generics will likely not be a good fit here – however, there is another option. You could use
dynamicto effectively use these properties using runtime binding. Just realize that, if you pass an argument of a type without those properties, it will fail at runtime.