Suppose I have these two objects:
public class Object1
{
string prop1;
string prop2;
}
public class Object2
{
string prop1;
int prop2;
int prop3;
}
and the following classes & methods:
public class Object1Service
{
public Object1 GetObject(Object1 o) { return o; }
public void SaveProperty2(Object1 o, string s) { o.prop2 = s; }
}
public class Object2Service
{
public Object2 GetObject(Object2 o) { return o; }
public void SaveProperty2(Object2 o, int i) { o.prop2 = i; }
}
How do I turn this into a generic?
I preferably need something so that the services implement an interface and just call some generic base class if possible.
Would it help if the two objects share a common parent class? If so, how will they be structured?
Addendum:
It’s the return that is my real problem.
public T GetObjectByKey<T>(string key)
{
using (DBEntities db = new DBEntities())
{
try
{
T returnedEntity = default(T);
switch (EntityDictionary[typeof (T)])
// I have a dictionary setup like this dictionary<type, string>
{
case "Object1" :
returnedEntity = ( from r in db.ObjectONESets
where r.prop1 == key
select new T
{
prop1 = r.prop1,
prop2 = r.prop2
}).FirstOrDefault();
break ;
case "Object2" :
returnedEntity = ( from r in db.ObjectTWOSets
where r.prop1 == key
select new T
{
prop1 = r.prop1,
prop2 = r.prop2,
prop3 = r.prop3
}).FirstOrDefault();
break ;
}
return returnedEntity;
}
catch (NullReferenceException )
{
return default(T);
}
}
}
Without putting ALL my object properties in the base object, it has no way of knowing that prop1-3 is a property of T.
If I do put all properties (common or not) in the base object, and if I need Object1, it then has a prop3 attached to it which I do not need.
I’m not sure if I’m making much sense at this point, or if what I’m asking is even doable with Generics.
I believe that you will not gain a benefit with generics here. The reason is because
prop2is a different type in each of these classes. If they were the same, then you could put the common properties into a base class and do something like thisYou could at least make the GetObject a generic using your current example…however that entire method really seems pointless: