I am trying to create a generic class, that will allow me save/delete Customers, Products, so that I can have all the basic implementation at one place.
public class Product : ItemDataService<Product>
{
public int id {get; set;}
}
public class Customer : ItemDataService<Customer>
{
public int id {get; set;}
}
public abstract class ItemDataService<T, V>
{
public T Item { get; set; }
public int Id { get; set; }
public ItemDataService(T item)
{
Item = item;
}
public void SaveItem(T item)
{
if (Item.Id <= 0)
{
InsertItem(item);
}
}
}
How can i access the Id
property of customer
class in ItemDataService
class, so that i can check Item.Id <= 0
A generic type is declared by specifying a type parameter in an angle brackets after a type name, e.g. TypeName<T> where T is a type parameter.
Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.
An Abstract class can have constants and fields.
Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.
Define an interface ISomeInterface
with a field Id
, like:
public interface ISomeInterface
{
int Id { get; }
}
And then you can make your abstract class implement that interface and also add a generic constraint that requires T
to be an implementation of that interface, like this:
public abstract class ItemDataService<T> : ISomeInterface
where T : ISomeInterface
{
public int Id { get; set; }
// ...
public void SaveItem(T item)
{
if (Item.Id <= 0) // Id is accessible now..
{
InsertItem(item);
}
}
}
EDIT
Actually, given your interesting inheritance tree, you don't need the interface at all. You can simply add a generic constraint that enforces T
to be a child of ItemDataService<T>
. It looks funny, but it works:
public abstract class ItemDataService<T>
where T : ItemDataService<T>
{
public int Id { get; set; }
// ...
public void SaveItem(T item)
{
if (Item.Id <= 0) // Id is accessible now..
{
InsertItem(item);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With