0

I have a problem that I'm trying to wrap my head around. I might be terribly mistaken but here is what I'm trying to do.

I have two interfaces. The second interfaces has a property that should be of a implementation of the first interface. Something like this:

public interface ITypeA{
   int Id {get;set;}
}

public interface IEntityA<T>: where T:ITypeA
{
   string Name{get;set;}
   T type{get;set;}
}

The implementations looks like this:

public class TypeA: ITypeA{
  int Id {get;set;}
}

public class EntityA: IEntityA<TypeA>{
  public string Name{get;set;}
  public TypeA type{get;set;
}

I might be doing something wrong already(?).

Now I'm implementing the repository pattern, and the interface for that looks like this:

public interface IRepo{
   IEnumerable<IEntityA<ITypeA>> GetObjects();
}

and the implementation:

public class DefaultRepo:Repo{

  //Cunstructors

   public IEnumerable<IEntitytA<ITypeA>> GetObjects(){
      var objects = SomeMethodThatReturnsTheOjects();//Get objects as EntityA[];
      return object.ToList();
   }
}

This doesn't work.

I've tried to cast it as well, but getting a warning that it is a suspicious cast.

objects.Cast<IEntityA<ITypeA>[]>().ToList();

Where am I doing/thinking wrong?

Help much appriciated :)

EDIT: Maybe the repository interface declaration should look like this

public interface IRepo<TEntityA> where TEntityA:IEntityA{
       IEnumerable<TEntityA> GetObjects();
    }

and the implementation:

public class DefaultRepo:Repo<EntityA>{
   ///Methods
}

Thoughts?? :)

4

2 回答 2

0

存储库基本上应该获取 json 数据并将其转换为实体。然而,json 看起来可能与不同的提供者非常不同,但它包含相同的数据(或多或少)。我使用 Json.NET 设置解析,因此必须在存储库返回的实体实现中以不同方式指定 [JsonProperty]

你为什么不拥有:

public interface IUserRepository 
{
   IEnumerable<IUser> Find();
}

然后有UserFromSourceAUserFromSourceB等等。

通过这种方式,您可以使用具有不同实现的 JSON.NET 属性,同时仍然公开相同的用户界面。

于 2012-11-14T16:29:03.053 回答
0

我已经找到了一个合适的解决方案。我正在使用 json.net,我可以通过这样做来解决它:

public interface ITypeA{
   int Id {get;set;}
}

public interface ITypeB{
   int id {get;set;}
}

public class TypeA:ITypeA
   string Name{get;set;}
   int id {get;set;}

   [JsonConverter(typeof (ConcreteTypeConverter<TypeB>))]
   ITypeB type{get;set;}
}

public class TypeB:ITypeB
{
   int id {get;set;}
}

转换器看起来像这样:

 public class ConcreteTypeConverter<TConcrete> : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            //assume we can convert to anything for now
            return true;
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            //explicitly specify the concrete type we want to create
            return serializer.Deserialize<TConcrete>(reader);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            //use the default serialization - it works fine
            serializer.Serialize(writer, value);
        }
    }

如果没有泛型,接口会变得更简洁、更易于阅读。

于 2012-12-02T09:32:13.610 回答