So what is different between <T> and <in T>?
Różnica polega na tym, że in T
pozwala przechodzić bardziej rodzajowe (mniej pochodzi) typu niż to, co zostało określone.
And what is the purpose of contravariant here?
ReSharper sugeruje użycie kontrawariancji tutaj, ponieważ widzi jesteś przechodzącą parametr T
do metodę Validate
i chce, aby umożliwić Ci rozszerzyć typ wejścia przez co mniej rodzajowy.
Generalnie kontrawariancja jest tłumaczona na długość w Contravariance explained oraz w Covariance and contravariance real world example i oczywiście w całej dokumentacji na MSDN (jest tam great FAQ by the C# team).
Jest ładny przykład poprzez MSDN:
abstract class Shape
{
public virtual double Area { get { return 0; }}
}
class Circle : Shape
{
private double r;
public Circle(double radius) { r = radius; }
public double Radius { get { return r; }}
public override double Area { get { return Math.PI * r * r; }}
}
class ShapeAreaComparer : System.Collections.Generic.IComparer<Shape>
{
int IComparer<Shape>.Compare(Shape a, Shape b)
{
if (a == null) return b == null ? 0 : -1;
return b == null ? 1 : a.Area.CompareTo(b.Area);
}
}
class Program
{
static void Main()
{
// You can pass ShapeAreaComparer, which implements IComparer<Shape>,
// even though the constructor for SortedSet<Circle> expects
// IComparer<Circle>, because type parameter T of IComparer<T> is
// contravariant.
SortedSet<Circle> circlesByArea =
new SortedSet<Circle>(new ShapeAreaComparer())
{ new Circle(7.2), new Circle(100), null, new Circle(.01) };
foreach (Circle c in circlesByArea)
{
Console.WriteLine(c == null ? "null" : "Circle with area " + c.Area);
}
}
}
How can I apply the usage of contravariant in this example?
Powiedzmy mamy nasze podmioty:
public class Entity : IEntity
{
public string Name { get; set; }
}
public class User : Entity
{
public string Password { get; set; }
}
Mamy też IBusinessManager
interfejs i implementację BusinessManager
, który akceptuje IBusinessValidator
:
public interface IBusinessManager<T>
{
void ManagerStuff(T entityToManage);
}
public class BusinessManager<T> : IBusinessManager<T> where T : IEntity
{
private readonly IBusinessValidator<T> validator;
public BusinessManager(IBusinessValidator<T> validator)
{
this.validator = validator;
}
public void ManagerStuff(T entityToManage)
{
// stuff.
}
}
Teraz, powiedzmy, że stworzyliśmy ogólny walidator dla każdego IEntity
:
public class BusinessValidator<T> : IBusinessValidator<T> where T : IEntity
{
public void Validate(T entity)
{
if (string.IsNullOrWhiteSpace(entity.Name))
throw new ArgumentNullException(entity.Name);
}
}
A teraz chcemy przekazać BusinessManager<User>
i IBusinessValidator<T>
. Ponieważ jest to contravariant, mogę go przekazać BusinessValidator<Entity>
.
Jeśli usuniemy słowa kluczowego in
, otrzymujemy następujący błąd:
Jeśli uwzględnimy to, to kompiluje grzywny.
Czy przeczytałeś już dokumentację? https://msdn.microsoft.com/en-us/library/dd799517(v=vs.110).aspx –
Tak, już to przeczytałem. Mam jednak mały problem ze zrozumieniem tego artykułu. W każdym razie link pomógł. –