Skip to content

Commit 3e88598

Browse files
committed
fixed: #414
1 parent 7ea96ee commit 3e88598

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

docs/DryIoc.Docs/Wrappers.cs

+41
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- [Meta or Tuple of A with Metadata](#meta-or-tuple-of-a-with-metadata)
1818
- [Dictionary Metadata](#dictionary-metadata)
1919
- [IEnumerable or array of A](#ienumerable-or-array-of-a)
20+
- [Custom order of items in the collection](#custom-order-of-items-in-the-collection)
2021
- [Open-generics](#open-generics)
2122
- [Co-variant generics](#co-variant-generics)
2223
- [Composite Pattern support](#composite-pattern-support)
@@ -433,6 +434,46 @@ public B(A a) { }
433434
} /*md
434435
```
435436
437+
#### Custom order of items in the collection wrapper
438+
439+
To achieve the custom ordering we may use the [Meta or Tuple with Metadata](#meta-or-tuple-of-a-with-metadata) wrapper and the [Decorator](Decorator.md) feature.
440+
441+
```cs md*/
442+
class Collection_with_custom_order
443+
{
444+
public sealed class OrderMetadata // may be an suitable data structure defined by you
445+
{
446+
public readonly int Value;
447+
public OrderMetadata(int value) => Value = value;
448+
}
449+
450+
// The "decorator" method sorting the `I` collection based on the `OrderMetadata` before returning it.
451+
static IEnumerable<I> SortByOrderMetadata(IEnumerable<Tuple<I, OrderMetadata>> ii) =>
452+
ii.OrderBy(x => x.Item2.Value).Select(x => x.Item1);
453+
454+
[Test] public void Example()
455+
{
456+
var container = new Container();
457+
container.Register<I, C>(setup: Setup.With(new OrderMetadata(3)));
458+
container.Register<I, A>(setup: Setup.With(new OrderMetadata(1)));
459+
container.Register<I, B>(setup: Setup.With(new OrderMetadata(2)));
460+
461+
container.RegisterDelegate<IEnumerable<Tuple<I, OrderMetadata>>, IEnumerable<I>>(SortByOrderMetadata, setup: Setup.Decorator);
462+
463+
var items = container.Resolve<I[]>();
464+
465+
Assert.IsInstanceOf<A>(items[0]);
466+
Assert.IsInstanceOf<B>(items[1]);
467+
Assert.IsInstanceOf<C>(items[2]);
468+
}
469+
interface I {}
470+
class A : I { }
471+
class B : I { }
472+
class C : I { }
473+
}/*md
474+
```
475+
476+
436477
#### Open-generics
437478
438479
If you registered both closed and open-generic implementation of the service,

docs/DryIoc.Docs/Wrappers.md

+41
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [Meta or Tuple of A with Metadata](#meta-or-tuple-of-a-with-metadata)
1717
- [Dictionary Metadata](#dictionary-metadata)
1818
- [IEnumerable or array of A](#ienumerable-or-array-of-a)
19+
- [Custom order of items in the collection](#custom-order-of-items-in-the-collection)
1920
- [Open-generics](#open-generics)
2021
- [Co-variant generics](#co-variant-generics)
2122
- [Composite Pattern support](#composite-pattern-support)
@@ -432,6 +433,46 @@ class Filtering_not_resolved_services
432433
}
433434
```
434435

436+
#### Custom order of items in the collection wrapper
437+
438+
To achieve the custom ordering we may use the [Meta or Tuple with Metadata](#meta-or-tuple-of-a-with-metadata) wrapper and the [Decorator](Decorator.md) feature.
439+
440+
```cs
441+
class Collection_with_custom_order
442+
{
443+
public sealed class OrderMetadata // may be an suitable data structure defined by you
444+
{
445+
public readonly int Value;
446+
public OrderMetadata(int value) => Value = value;
447+
}
448+
449+
// The "decorator" method sorting the `I` collection based on the `OrderMetadata` before returning it.
450+
static IEnumerable<I> SortByOrderMetadata(IEnumerable<Tuple<I, OrderMetadata>> ii) =>
451+
ii.OrderBy(x => x.Item2.Value).Select(x => x.Item1);
452+
453+
[Test] public void Example()
454+
{
455+
var container = new Container();
456+
container.Register<I, C>(setup: Setup.With(new OrderMetadata(3)));
457+
container.Register<I, A>(setup: Setup.With(new OrderMetadata(1)));
458+
container.Register<I, B>(setup: Setup.With(new OrderMetadata(2)));
459+
460+
container.RegisterDelegate<IEnumerable<Tuple<I, OrderMetadata>>, IEnumerable<I>>(SortByOrderMetadata, setup: Setup.Decorator);
461+
462+
var items = container.Resolve<I[]>();
463+
464+
Assert.IsInstanceOf<A>(items[0]);
465+
Assert.IsInstanceOf<B>(items[1]);
466+
Assert.IsInstanceOf<C>(items[2]);
467+
}
468+
interface I {}
469+
class A : I { }
470+
class B : I { }
471+
class C : I { }
472+
}
473+
```
474+
475+
435476
#### Open-generics
436477

437478
If you registered both closed and open-generic implementation of the service,

0 commit comments

Comments
 (0)