简体   繁体   English

使用泛型和扩展方法的IDescription接口

[英]IDescription interface using generics and extension methods

I am trying to implement an IDescription Interface. 我正在尝试实现IDescription接口。 Basic purpose of this interface is that I have many different classes that have a list of multilingual descriptions and I want the the basic AddDescription EditDescription and some other basic behaviours to be defined by the interface and not implemented by the classes individually that inherits the interface. 该接口的基本目的是,我有许多不同的类,这些类具有多语言描述列表,并且我希望基本的AddDescription EditDescription和其他一些基本的行为由接口定义,而不是由继承接口的类单独实现。 I am trying to assign the behavior to the interface using extension methods. 我正在尝试使用扩展方法将行为分配给接口。

I have some road blocks such as how do I access the descriptions collection of the entity that I am passing on to the IDescription interface (entity.Descriptions.Add)? 我有一些障碍,例如如何访问要传递给IDescription接口(entity.Descriptions.Add)的实体的描述集合?

I am very new to generics, extension methods, anonymous types etc so please bear with me with my misunderstandings of how these are used. 我对泛型,扩展方法,匿名类型等非常陌生,因此请对我对它们的使用方式有误解。 Will appreciate if you can help me correct the below code. 如果您能帮助我更正以下代码,将不胜感激。 I wrote it to give the idea of what I am trying to achieve, it obviously fundamental errors in it. 我写它的目的是想知道我要实现的目标,它显然是其中的基本错误。 Thanks 谢谢

public class Company : IDescription<Company, CompanyDescription>
{
   public IList<CompanyDescription> Desriptions { get; set; }
}

public class Location : IDescription<Location, LocationDescription>
{
   public IList<LocationDescription> Desriptions { get; set; }
}


public interface IDescription<eT, dT>
{
   void AddDescription(eT, string text);
   void EditDescription(eT, dT, string text);
} 

public static DescriptionInterfaceExtensions
{

   public static void AddDescription(this IDescription<eT, dT> description, eT entity, string text)
   {
      dT newDescription = new dT(text);
      entity.Descriptions.Add(newDescription);
   }
}

I your example it seems the contract is that objects that have a description store a list of descriptions. 在您的示例中,似乎契约是具有描述的对象存储了一个描述列表。 So, to avoid having to declare Add and Remove methods in the classes directly, you could do something like this: 因此,为了避免直接在类中声明Add和Remove方法,可以执行以下操作:

Interfaces 接口

public interface IDescription<T>
{
}

public interface IHasDescription<THasDescription, TDescription>
    where THasDescription : IHasDescription<THasDescription, TDescription>
    where TDescription : IDescription<THasDescription>
{
    IList<TDescription> Descriptions { get; }
}

Concrete implementations 具体实施

public class CompanyDescription : IDescription<Company>
{
}

public class Company : IHasDescription<Company, CompanyDescription>
{
    private readonly IList<CompanyDescription> descriptions;

    public IList<CompanyDescription> Descriptions
    {
        get { return this.descriptions; }
    }
}

Extension methods 扩展方式

public static class DescriptionExtensions
{
    public static void AddDescription<THasDescription, TDescription>(
            this THasDescription subject,
            TDescription description)
        where THasDescription : IHasDescription<THasDescription, TDescription>
        where TDescription : IDescription<THasDescription>
    {
        subject.Descriptions.Add(description);
    }
}

But I don't think it's worth to do this just to have 但是我不认为仅仅为了拥有

mycompany.AddDescription(mydescription);

instead of 代替

mycompany.Descriptions.Add(mydescription);

Another possible rewrite that should work is to remove the Add/Edit methods from the interface and simply provide the required IList in the interface. 另一个可行的重写方法应该是从接口中删除Add / Edit方法,并在接口中简单地提供所需的IList。 Then, for ease of use, you can use the extension methods to make it easier. 然后,为了易于使用,您可以使用扩展方法使其更容易。

I'm not saying this example is a great use of generics or extension methods, but it will work: 我并不是说这个示例很好地使用了泛型或扩展方法,但是它将起作用:

public class CompanyDescription : IDescription { public string Text { get; set; } }
public class LocationDescription : IDescription { public string Text { get; set; } }

public class Company : IHaveDescriptions<CompanyDescription>
{
   public IList<CompanyDescription> Desriptions { get; set; }
}

public class Location : IHaveDescriptions<LocationDescription>
{
   public IList<LocationDescription> Desriptions { get; set; }
}

public interface IDescription
{
    string Text { get; set; }
}
public interface IHaveDescriptions<T>
    where T : class, IDescription, new()
{
    IList<T> Desriptions { get; set; }
}

public static class DescriptionInterfaceExtensions
{
    public static void AddDescription<T>(this IHaveDescriptions<T> entity, string text)
        where T : class, IDescription, new()
    {
        T newDescription = new T();
        newDescription.Text = text;
        entity.Desriptions.Add(newDescription);
    }
    public static void EditDescription<T>(this IHaveDescriptions<T> entity, T original, string text)
        where T : class, IDescription, new()
    {
        T newDescription = new T();
        newDescription.Text = text;
        entity.Desriptions.Remove(original);
        entity.Desriptions.Add(newDescription);
    }
} 

You can't add interface implementations to classes using extension methods, although that seems to be what you are trying to do. 您不能使用扩展方法向类添加接口实现 ,尽管这似乎是您要尝试的方法。

The purpose of extension methods is to add behavior to existing types (classes or interfaces). 扩展方法的目的是向现有类型(类或接口)添加行为

The problem with your code is that you declare that Company and Location should implement the IDescription interface; 代码的问题是,您声明Company and Location应该实现IDescription接口。 yet they don't. 但是他们没有。 They have no AddDescription or EditDescription methods, so that's not going to work. 它们没有AddDescription或EditDescription方法,因此将无法使用。

Why don't you instead define a concrete generic Description class and attach that class to Company and Location? 您为什么不定义一个具体的通用Description类,然后将该类附加到Company和Location?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM