While refactoring my application and trying to add some abstraction to it, I found out a problem which I cannot solve by myself.

Here is an abstraction of my real problem, hope this will be easier to imagine and understand.

There is a `Container` class with a `List<IContainerItem>`.

In my application there are two types of `IContainerItem`:

* `ContainerItemA`
* `ContainerItemB`

There is also `IContainerItemFactory` with one method:

```
IContainerItem Create();

```

Also for both types of `IContainerItem` there is `IContainerItemFactory` implementation:

* `ContainerItemAFactory`
* `ContainerItemBFactory`

Now, while building the `Container` by IoC, specific type of `IContainerItemFactory` is injected, list can be populated by specific factory and everything works.

Problem started, when I added to my application `IContainerItemPart` and its two implementations:

* `ContainerItemAPart`
* `ContainerItemBPart`

Now to create `ContainerItem(A|B)` you have to pass by the constructor `ContainerItem(A|B)Part` to it. You cannot pass `IContainerItemPart` because each implementation of `IContainerIte`m use properties from specific `IContainerItemPart` implementation (different names, different number of properties)

What to do with the factories now?
Create method in the interface now looks like:

```
IContainerItem Create(IContainerItemPart part);

```

And I have two options:

1. Add casting from `IContainerItemPart` to specific in the factory Create method and pass the implementation to the constructor
2. Pass `IContainerItemPart` to `IContainerItem` implementations and cast inside (inside the constructor)

Which solution is better in your opinion? Maybe none of them? Why?
Maybe the problem is with my architecture? How to solve it then?

**UPDATE:**

I am providing some code to clarify. Code is valid C# code, except Create() methods in factories.

```
    interface IContainerItem { }

    class ContainerItemA : IContainerItem
    {
        public ContainerItemA(ContainerItemAPart part)
        {
            Console.WriteLine(part.SpecificForA);
        }
    }

    class ContainerItemB : IContainerItem
    {
        public ContainerItemB(ContainerItemBPart part)
        {
            Console.WriteLine(part.SpecificForB);
        }
    }

    interface IContainerItemPart
    {
        int Id { get; }
    }

    class ContainerItemAPart : IContainerItemPart
    {
        public int Id { get; set; }
        public int SpecificForA { get; set; }
    }

    class ContainerItemBPart : IContainerItemPart
    {
        public int Id { get; set; }
        public int SpecificForB { get; set; }
    }

    interface IContainerItemFactory
    {
        IContainerItem Create(IContainerItemPart part);
    }

    class ContainerItemAFactory : IContainerItemFactory
    {
        public IContainerItem Create(IContainerItemPart part)
        {
            //dont work here, downcasting needed
            return new ContainerItemA(part);
        }
    }
    class ContainerItemBFactory : IContainerItemFactory
    {
        public IContainerItem Create(IContainerItemPart part)
        {
            //dont work here, downcasting needed
            return new ContainerItemB(part);
        }
    }

    class Container
    {
        private IList<IContainerItem> _items;
        public Container(IList<IContainerItemPart> parts, IContainerItemFactory factory)
        {
            _items = new List<IContainerItem>();
            foreach (var part in parts)
                _items.Add(factory.Create(part));
        }
    }

```