Reputation: 20491
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.
Upvotes: 1
Views: 165
Reputation: 2201
Justin is correct, without modifying the OP's objects, there is no way to make this "neatly" generic.
Here is a messy solution:
interface IObject<T>
{
T prop2 {get;set;}
}
class ObjectService<T, Z> where T : IObject<Z>
{
public T GetObject(T o)
{
return o;
}
public void SetValue(T o, Z val)
{
o.prop2 = val;
}
}
Upvotes: 1
Reputation: 19416
Here is a solution which should do what you're after:
public interface IPropertyProvider<T>
{
T Prop2 { get; set; }
}
public class ObjectService<T, TProp> where T : IPropertyProvider<TProp>
{
public void SaveProperty(T o, TProp i)
{
o.Prop2 = i;
}
}
public class Object1 : IPropertyProvider<string>
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
public class Object2 : IPropertyProvider<int>
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
public int Prop3 { get; set; }
}
public class Object1Service : ObjectService<Object1, string>
{
}
public class Object2Service : ObjectService<Object2, int>
{
}
Upvotes: 3
Reputation: 67075
I believe that you will not gain a benefit with generics here. The reason is because prop2
is 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 this
public class BaseObject
{
string prop1;
string prop2
}
public class Object1 : BaseObject
{
}
public class Object2 : BaseObject
{
int prop3;
}
public class ObjectService<T> where T is BaseObject
{
public T GetObject(T o) { return o; }
public void SaveProperty2(T o, string i) { o.prop2 = i; }
}
You could at least make the GetObject a generic using your current example...however that entire method really seems pointless:
public class ObjectService
{
public T GetObject<T>(T o) { return o; }
public void SaveObject2Property2(Object2 o, int i) { o.prop2 = i; }
public void SaveObject1Property2(Object1 o, string s) { o.prop2 = s; }
}
Upvotes: 5