I have an two interfaces defined as follows:
public interface IFoo
{
...
}
Public interface IFooWrapper<T> where T : IFoo
{
T Foo {get;}
}
I want to be able to declare a collection of IFooWrappers but I don't want to specify the implementation of IFoo.
Ideally I want to do something like the:
IList<IFooWrapper<*>> myList;
I can't figure out a way around this.
public interface IFoo
{
...
}
public interface IFooWrapper : IFoo
{
...
}
public interface IFooWrapper<T> : IFooWrapper
where T : IFoo
{
...
}
IList<IFooWrapper> myList;
this is a way to do what you want
What's wrong with
IList<IFooWrapper<IFoo>> myList?
public class FooWrapper : IFooWrapper<IFoo>
What I'm about to suggest is overkill for most situations, since usually you can create an interface higher up in the hierarchy that you can use. However, I think this is the most flexible solution in some ways, and the most faithful representation of what you want:
public interface IFooWrapperUser<U> {
U Use<T>(IFooWrapper<T> wrapper);
}
public interface IFooWrapperUser {
void Use<T>(IFooWrapper<T> wrapper);
}
public interface IExistsFooWrapper {
U Apply<U>(IFooWrapperUser<U> user);
void Apply(IFooWrapperUser user);
}
public class IExistsFooWrapper<T> : IExistsFooWrapper {
private IFooWrapper<T> wrapper;
public IExistsFoo(IFooWrapper<T> wrapper) {
this.wrapper = wrapper;
}
public U Apply<U>(IFooWrapperUser<U> user) {
return user.Use(foo);
}
public void Apply(IFooWrapperUser user) {
user.Use(foo)
}
}
Now you can create an instance of an IList<IExistsFooWrapper> which can be used as if it's an IList<IFooWrapper<*>>. The downside is you'll need to create a class to encapsulate the logic you want to run on each element:
private class FooPrinter : IFooWrapperUser<string> {
public string Apply<T>(IFooWrapper<T> wrapper) {
return wrapper.Foo.ToString();
}
}
...
IFooWrapperUser<string> user = new FooPrinter();
foreach (IExistFooWrapper wrapper in list) {
System.Console.WriteLine(wrapper.Apply(user));
}
...
Related
I have been trying to understand why the following generates a compile error, and how to get around the error.
public interface IA { }
public class AImp : IA { }
public interface IConsumer<T> where T : IA
{
void Consume(T val);
}
public class Consumer : IConsumer<AImp>
{
public void Consume(AImp val)
{
// do smth
}
}
public class Program
{
public static void Main()
{
IList<IConsumer<IA>> l1 = new List<IConsumer<IA>>();
l1.Add(new Consumer()); // generate compile error cannot convert from consumer to IConsumer<IA>
}
}
If I can create IList<IA> l = new List<IA>(); and asign l.Add(new AImp()); , I dont see why the generic type is not working. Perhaps I am missing something fundamental.
I haven't been able to find any promising leads from google either.
You get an error message because the Consumer does not inherit from IConsumer<T>. It inherits from IConsumer<AImp>.
There is special technique to use this kind of template class, you have to create a IConsumer interface which is not generic type like this:
public interface IA { }
public class AImp : IA { }
public interface IConsumer
{
}
public interface IConsumer<T> : IConsumer
where T : IA
{
void Consume(T val);
}
public class Consumer : IConsumer<AImp>
{
public void Consume(AImp val)
{
// do smth
}
}
public class Program
{
public static void Main()
{
IList<IConsumer> l1 = new List<IConsumer>();
l1.Add(new Consumer()); // ok
}
}
This basically the idea behind the IEnumerable and ICollection generic and non generic versions.
Currently I am in the phase of refactoring my code after it has been unit tested, and I have some concerns about the refactoring from a design point of view with regards to type safety. My original code looked a bit like this:
Interfaces
public interface IBase
{
int ID { get; set; }
}
public interface IFirstSub : IBase
{
string Description { get; set; }
}
public interface ISecondSub : IBase
{
decimal Total { get; set; }
}
public interface IThirdSub : IBase
{
int Count { get; set; }
}
public interface IBaseContainer
{
void Add(IBase baseParam);
}
Implementations
public class FirstContainer : IBaseContainer
{
public void Add(IBase baseParam)
{
if (!(baseParam is IFirstSub || baseParam is ISecondSub))
{
throw new ArgumentException(nameof(baseParam));
}
// Do Something
}
}
public class SecondContainer : IBaseContainer
{
public void Add(IBase baseParam)
{
if (!(baseParam is IThirdSub))
{
throw new ArgumentException(nameof(baseParam));
}
// Do Something
}
}
With my original implementation of FirstContainer and SecondContainer, it was repeating the same logic at the start of the Add method, so I thought I would refactor the code to look something like this:
public abstract class BaseContainer : IBaseContainer
{
private readonly List<Type> _types = new List<Type>();
protected BaseContainer(params Type[] baseTypes)
{
_types.AddRange(baseTypes);
}
public void Add(IBase baseParam)
{
if (_types.All(type => !type.IsInstanceOfType(baseParam)))
{
throw new ArgumentException(nameof(baseParam));
}
DoSomething(baseParam);
}
protected abstract void DoSomething(IBase baseParam);
}
public class ThirdContainer : BaseContainer
{
public ThirdContainer() : base(typeof(IFirstSub)) { }
protected override void DoSomething(IBase baseParam)
{
// Do Something
}
}
With this refactoring done, it successfully removes the duplication of the code from the start of the Add method, but my main concern with the refactoring is the fact that the call to the base constructor base(typeof(IFirstSub)) is not really type safe. By that, I mean I can call the base constructor like base(typeof(object)) for example, and it will compile. For the purposes of my project, I'd like to constrain the types to ones that inherit IBase, and enforce at compile time.
Is there anyway to overcome this limitation, or would a new design be needed in order to achieve this?
No it's not type safe
Passing and validating types at run-time is not type-safe, as type-safety is a compile-time concept. In my opinion your refactoring effort does not improve the code, and in fact does something quite weird.
Function overloading
If you need a method that accepts either of two types, you can use function overloading:
public class FirstContainer : IBaseContainer
{
public void Add(IFirstSub param)
{
// Do Something
}
public void Add(ISecondSub param)
{
// Do Something
}
}
The compiler will automatically choose the right prototype for you, and will not allow anything other than an IFirstSub or ISecondSub.
Create another interface
Another approach requires you to add an interface for the types that have something in common, like this:
interface ICanBeHeldInFirstContainer
{ }
public interface IFirstSub : IBase, ICanBeHeldInFirstContainer
{
string Description { get; set; }
}
public interface ISecondSub : IBase, ICanBeHeldInFirstContainer
{
decimal Total { get; set; }
}
Then you do this:
public class FirstContainer : IBaseContainer
{
public void Add(ICanBeHeldInFirstContainer param)
{
// Do Something
}
}
or this:
public class FirstContainer : IBaseContainer
{
public void Add<T>(T param) where T : ICanBeHeldInFirstContainer
{
// Do Something
}
}
I have problem.
For example, considering these 4 classes.
public abstract ParentContainer<T> where T: Parentfoo
{
public List<T> fooList;
}
//Let there be many different ChildContainers with different types.
public class ChildContainer : ParentContainer<ChildFoo>
{
}
public abstract class ParentFoo
{
public string name;
}
public class ChildFoo : ParentFoo
{
}
How can I write a method which accepts a List of any arbitrary ChildContainers as a parameter,
which do operation on their fooLists?
Is it even possible?
Additional Explanation, there are many different childs of ParentContainer,
each with a List of a different child of foo.
public class ChildContainerB : ParentContainer<ChildFooB>{}
public class ChildContainerC : ParentCOntainer<ChildFooC>{}
...
public class ChildFooB : ParentFoo;
public class ChildFooC : ParentFoo;
Then I need a method something like this
//X means it can be any arbitrary ChildContainer
public void method(List<ChilContainerX> list)
{
foreach(ChildContainerX x in list)
{
print(x.fooList.Last().name);
}
}
So what you are asking isn't possible because you need a concrete type for the List<>. There are a couple of workarounds though.
Use List<object>. This is obviously not great because as it means you lose type checking completely and could end up with anything being added to the list. So for that reason, I wouldn't recommend this approach.
Make ParentContainer<> implement a marker interface. For example:
public interface IParentContainer
{
}
public abstract class ParentContainer<T> : IParentContainer
where T: ParentFoo
{
//snip
}
And now you can have your list like this:
var containers = new List<IParentContainer>
{
new ChildContainer(),
new ChildContainer2()
};
Unless I misunderstand the question, you want something like this ...
public void DoStuffWith<T>(List<ParentContainer<T>> containers) where T : Parentfoo
{
//TODO: implement
}
This would work on objects of type ...
List<ParentContainer<ParentFoo>>
List<ParentContainer<ChildFoo>>
where ChildFoo : ParentFoo
and solves the issue of "List<ParentContainer<ParentFoo>> does not implement IEnumerable<ParentContainer<ChuldFoo>>" which I suspect is the compiler you are seeing.
Taking this a step further something like ....
public void DoStuffWith<ContainerT,ElementT>(List<ContainerT<ElementT>> containers)
where ContainerT : ParentContainer
where ElementT : Parentfoo
{
//TODO: implement
}
... likely over complicates the issue but i suspect is what you are trying to achieve.
At this point I would question the data structures you have and give them some common parent for example ...
public class ParentContainer<T> : IEnumerable<T> { ... }
public class ChildContainer<T> : ParentContainer<T>, IEnumerable<T> { ... }
Since both implement IEnumerable you can now do ...
public void DoStuffWith<T>(IEnumerable<T> containers) where T : ParentFoo
{
//TODO: implement
}
This avoids the need to be concerned with the collection type at all.
class Program
{
static void Main(string[] args)
{
List<ParentContainer<ChildFoo>> ca = new List<ParentContainer<ChildFoo>>();
ProcessContainers processor = new ProcessContainers();
ca.Add(new ChildContainerA { fooList = new List<ChildFoo>() });
ca.Add(new ChildContainerA { fooList = new List<ChildFoo>() });
ca.Add(new ChildContainerA { fooList = new List<ChildFoo>() });
ca.Add(new ChildContainerB { fooList = new List<ChildFoo>() });
processor.Process(ca);
}
}
public abstract class ParentContainer<T> where T: ParentFoo
{
public List<T> fooList;
}
//Let there be many different ChildContainers with different types.
public class ChildContainerA : ParentContainer<ChildFoo>
{
}
public class ChildContainerB : ParentContainer<ChildFoo>
{
}
public class ProcessContainers
{
public void Process<T>(List<ParentContainer<T>> childContainers) where T : ParentFoo
{
foreach(var item in childContainers)
{
foreach(var foo in item.fooList)
{
foo.Porcess();
}
}
}
}
public abstract class ParentFoo
{
public string name;
public abstract void Porcess();
}
public class ChildFoo : ParentFoo
{
public override void Porcess()
{
//Do some processing
}
}
I defined 3 interfaces:
public interface IManufacturerInput
{
}
public interface IManufacturerOutput
{
}
public interface IManufacturerApi<in S, out T>
where S : IManufacturerInput
where T : IManufacturerOutput
{
T Calculate(S);
}
And I defined a specific Manufacturer:
public class ManufacturerAInput : IManufacturerInput
{
}
public class ManufacturerAOutput : IManufacturerOutput
{
}
public class ManufacturerAApi : IManufacturerApi<ManufacturerAInput, ManufacturerAOutput>
{
public ManufacturerAOutput Calculate(ManufacturerAInput)
{
return null;
}
}
And In Main() I created a ManufacturerAApi, and try assign it to IManufacturerApi.
IManufacturerApi<IManufacturerInput, IManufacturerOutput> api = new ManufacturerAApi();
But it failed. The error message said (just abstract meaning):
Can't convert from ManufacturerAApi to IManufacturerApi<IManufacturerInput, IManufacturerOutput>
So is there any way I can make the assignment work? Thanks in advance.
What you are proposing isn't type safe. Let's change the names of your types to make the issue clearer:
public interface IPetFood { }
public interface IPetSound { }
public interface IPetCage<in S, out T>
where S : IPetFood
where T : IPetSound
{
T Feed(S s);
}
public class DogFood : IPetFood { }
public class CatFood : IPetFood { }
public class Bark : IPetSound { }
public class DogCage : IPetCage<DogFood, Bark>
{
public Bark Feed(DogFood input)
{
return new Bark();
}
}
And now suppose this is legal:
IPetCage<IPetFood, IPetSound> api = new DogCage();
Then we could do the following:
api.Feed(new CatFood()); //oops we've just given the dog some catfood.
The assignment will not work because S is contravariant, which means that any possible IPetFood passed into api.Feed would need to be a subtype of DogFood and you have the opposite; IPetFood is a superset of DogFood.
Let's say I have some basic interface which is generics-driven:
public interface Inteface<T> {
void Foo(T t);
}
Now I have some concrete implementation of this interface which is also generic:
public class InterfaceImpl<T> {
public void Foo(T t) {
// Whatever
}
}
This looks OK, but now let's say I have other class:
public class Ololo {
public void BadFunction<TShouldModelInterface>(TShouldModelInterface shouldModelInterface) {
// Whatever
}
}
And let's say I want to perform a check if TShouldModelInterface actually implements any of the possible Interface<T>.
If the interface wasn't generic, I would simply write something like where TShouldModelInterface : Interface.
But is there any way to solve this problem if the interface is a declared as Interface<T>?
public class Ololo {
public void BadFunction<TShouldModelInterface, T>(TShouldModelInterface shouldModelInterface)
where TShouldModelInterface : Interface<T>
{
// Whatever
}
}