Cody
Cody

Reputation: 8964

Using Generics in C# to compare conditionally

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:

  1. A tier: ((o1.Rate!=o2.Rate) || ((o1.Start!=o2.Start) && (o1.End!=o2.End))) && (MinVal && MaxVal)
  2. A tier or plan: (o1.Name!=o2.Name)
  3. 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.

Upvotes: 3

Views: 192

Answers (4)

Yahia
Yahia

Reputation: 70379

I am really not sure what the question is... but if all this is some sort of "pre-check" before it gets created in the database I usually resort to a stored procedure in the database which takes alle relevant params for creation and return a primary key or when it fails an error code... in case of success then load with the primary key the created object from database...

The database is usually very efficient on such comparisons - and if I ever need to run multiple instances of the program in parallel and/or create objects independently from different programs the database solution takes care of it...

EDIT - as per comment/request:
I usually work with Oracle so I don't have any SQL Server Stored procedure at hand... but these links should provide some insight:

Upvotes: 1

Adam Tuliper
Adam Tuliper

Reputation: 30152

Your options are basically:

  1. use a common inherited base object with those props
  2. implement an interface with those props
  3. use reflection to get the values and compare them if the objects are really unrelated or if you only know the compared field values at runtime.

Reed mentioned below dynamic objects - that may work I'd have to trust his answer on that but includes the dynamic requirement though.

Upvotes: 0

Daniel Pryden
Daniel Pryden

Reputation: 60997

Maybe something like this:

// I'm making up the types here; modify to match your actual types
public interface IMyObject {
    public int Rate { get; set; }
    public DateTime Date { get; set; }
    public int MinVal { get; set; }
    public int MaxVal { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
}

public static class MyObjectFactory {
    public static T Create<T>(int rate, DateTime date, /* etc.. */) 
            where T : IMyObject, new() {

        if (/* tests */) {
            throw new MyObjectException("Constraint violated ...");
        }             

        T obj = new T();

        obj.Rate = rate;
        obj.Date = date;
        // etc ....

        return obj;
    }
}

And then you would use it like so:

public class SomeMyObject : IMyObject {
    // implementation omitted for brevity ...
}

// somewhere:
SomeMyObject obj = MyObjectFactory.Create<SomeMyObject>(rate, date, /* etc. */);

Is that what you're asking for?

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564851

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, and End).

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 dynamic to 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.

Upvotes: 2

Related Questions