简体   繁体   English

嵌套泛型的扩展方法

[英]Extension method with nested generics

I work on a multilingual site and I have built this structure for my "localizable" entities. 我在一个多语言站点上工作,并且已经为我的“可本地化”实体构建了该结构。

The interfaces: 接口:

public interface ILocalizable<T>
    where T : ILocalized
{
    ICollection<T> Content { get; set; }
}

public interface ILocalized
{
    int LanguageId { get; set; }
    virtual Language Language { get; set; }
}

And implementation: 并实现:

public class Entity : ILocalizable<EntityLocalized>
{
    public int Id { get; set; }
    public ICollection<EntityLocalized> Content { get; set; }
}

public class EntityLocalized : ILocalized
{
    public int LanguageId { get; set; }
    public virtual Language Language { get; set; }

    public int EntityId { get; set; }
    public string Title { get; set; }
}

The reason for this is that I could write an extension method that allows me to get the right string this way: 这样做的原因是我可以编写一个扩展方法,使我可以通过这种方式获取正确的字符串:

Entity entity; // get entity from database
string localizedString = entity.Content.Translate(localized => localized.Title);

Everything works just fine. 一切正常。 I just had an idea to write another extension method that would save me some work while building a query. 我只是想写一个扩展方法,在构建查询时可以节省一些工作。 So that I don't have to write all the time: 这样我就不必一直写:

IQueryable<Entity> query; // some query
return query.Include(entity => entity.Content.Select(localized => localized.Language));

So I have this: 所以我有这个:

public static IQueryable<TEntity> WithLocalization<TEntity>(this IQueryable<TEntity> query)
{
    return query.Include(entity => entity.Content.Select(content => content.Language));
}

But obviously I need to specify the generic type. 但是显然我需要指定泛型类型。 This won't compile. 这不会编译。 I tried everything. 我尝试了一切。

public static IQueryable<TEntity> WithLocalization<TEntity>(this IQueryable<ILocalizable<ILocalized>> set) where TEntity : ILocalizable<ILocalized> {}

IQueryable<Entity> query; // get query
query.WithLocalization(); // intellisense doesn't display and this won't compile

I kind of understand the reason. 我有点理解原因。 It's been discussed there many times. 那里已经讨论了很多次了。 I'm just wondering if there's a way how to build such an extension method without a need to explicitly use and pass 2 generics types, like: 我只是想知道是否有一种方法可以构建这种扩展方法而无需显式使用和传递2个泛型类型,例如:

public static IQueryable<TEntity> WithLocalization<TEntity, TEntityLocalized>(this IQueryable<TEntity> set)           
    where TEntityLocalized : ILocalized
    where TEntity : ILocalizable<TEntityLocalized> {}

Thank you! 谢谢!

I think you want... 我想你要...

public static IQueryable<ILocalizable<TEntityLocalized>> WithLocalization<TEntityLocalized>(this IQueryable<ILocalizable<TEntityLocalized>> query)
    where TEntityLocalized : ILocalized {
    return query.Include(entity => entity.Content.Select(content => content.Language));
}

You aren't propagating the TEntity generic parameter into the signature correctly. 您没有将TEntity泛型参数正确传播到签名中。

public static IQueryable<ILocalizable<TEntity>> WithLocalization<TEntity>(this IQueryable<ILocalizable<TEntity>> query)
    where TEntity : ILocalized
  {
      return query.Include (entity => entity.Content.Select (content => content.Language));
  }

public class Test
{
   public Test()
   {
      IQueryable<Entity> query;
      query.WithLocalization ();
   }
}

compiles correctly and gives correct intellisense. 正确编译并给出正确的智能感知。

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

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