open-generics

How can I register a generic object factory?

本秂侑毒 提交于 2020-01-04 02:26:04
问题 I have the following two classes: public class KeyedEntity<TEntity> { internal KeyedEntity() { } public Identifier Key { get; set; } public TEntity Entity { get; set; } } public static class KeyedEntity { public static KeyedEntity<TEntity> Create<TEntity>(Identifier key, TEntity entity) { return new KeyedEntity<TEntity> { Key = key, Entity = entity, }; } } The reason the constructor is internal and the second class exists is I want to enforce the more highly-maintainable KeyedEntity.Create(x,

Pattern for exposing non-generic version of generic interface

我们两清 提交于 2019-12-20 08:49:07
问题 Say I have the following interface for exposing a paged list public interface IPagedList<T> { IEnumerable<T> PageResults { get; } int CurrentPageIndex { get; } int TotalRecordCount { get; } int TotalPageCount { get; } int PageSize { get; } } Now I want to create a paging control public class PagedListPager<T> { public PagedListPager<T>(IPagedList<T> list) { _list = list; } public void RenderPager() { for (int i = 1; i < list.TotalPageCount; i++) RenderLink(i); } } The paging control has no

How to unit test open generic decorator chains in SimpleInjector 2.6.1+

此生再无相见时 提交于 2019-12-12 15:28:16
问题 Given the following open generic deocrator chain using SimpleInjector: container.RegisterManyForOpenGeneric(typeof(IHandleQuery<,>), assemblies); container.RegisterDecorator( typeof(IHandleQuery<,>), typeof(ValidateQueryDecorator<,>) ); container.RegisterSingleDecorator( typeof(IHandleQuery<,>), typeof(QueryLifetimeScopeDecorator<,>) ); container.RegisterSingleDecorator( typeof(IHandleQuery<,>), typeof(QueryNotNullDecorator<,>) ); With SimpleInjector 2.4.0, I was able to unit test this to

What is the open generic type of the array []?

[亡魂溺海] 提交于 2019-12-12 12:34:38
问题 When I do int[] , string[] , T[] - this is a generic array. An array is just an object like everything else. So what is the actual open generic type of []? I assume it is just some syntactic sugar over something like Array<> but I haven't been able to find anything of the sort. Bonus points if you can somehow answer this before Jon Skeet. 回答1: It's just System.Array; it isn't generic. See here for a brief discussion: http://blogs.msdn.com/b/ericlippert/archive/2007/10/17/covariance-and

What is the correct way to register FluentValidation with SimpleInjector?

拟墨画扇 提交于 2019-12-04 11:48:22
问题 I am able to register FluentValidation AbstractValidators using a FluentValidatorFactory . However, it doesn't feel right, because not all of the IoC container registrations happen during bootstrap / composition root. Instead, the fluent validators are registered by a separate factory: The composition root : public class SimpleDependencyInjector : IServiceProvider { public readonly Container Container; public SimpleDependencyInjector() { Container = Bootstrap(); } internal Container Bootstrap

How to register many for open generic in Autofac

一笑奈何 提交于 2019-12-03 08:24:32
问题 I'm new to Autofac (not to DI ). Here is the situation: I have these interfaces: public interface IQuery<out TResult> : IQuery { } public interface IQueryHandler<in TQuery, out TResult> where TQuery : IQuery<TResult> { TResult Handle(TQuery query); } and there is a lot of implementation of them in my solution: class GetPersonQuery : IQuery<PersonModel> { } class GetPersonQueryHandler : IQueryHandler<GetPersonQuery, PersonModel> { } class GetArticleQuery : IQuery<ArticleModel> { } class

What is the correct way to register FluentValidation with SimpleInjector?

守給你的承諾、 提交于 2019-12-03 07:12:46
I am able to register FluentValidation AbstractValidators using a FluentValidatorFactory . However, it doesn't feel right, because not all of the IoC container registrations happen during bootstrap / composition root. Instead, the fluent validators are registered by a separate factory : The composition root : public class SimpleDependencyInjector : IServiceProvider { public readonly Container Container; public SimpleDependencyInjector() { Container = Bootstrap(); } internal Container Bootstrap() { var container = new Container(); container.Register< // ...register all non-fluent-validator

How to register many for open generic in Autofac

久未见 提交于 2019-12-02 22:08:31
I'm new to Autofac (not to DI ). Here is the situation: I have these interfaces: public interface IQuery<out TResult> : IQuery { } public interface IQueryHandler<in TQuery, out TResult> where TQuery : IQuery<TResult> { TResult Handle(TQuery query); } and there is a lot of implementation of them in my solution: class GetPersonQuery : IQuery<PersonModel> { } class GetPersonQueryHandler : IQueryHandler<GetPersonQuery, PersonModel> { } class GetArticleQuery : IQuery<ArticleModel> { } class GetArticleQueryHandler : IQueryHandler<GetArticleQuery, ArticleModel> { } class GetSomethingQuery : IQuery

Pattern for exposing non-generic version of generic interface

久未见 提交于 2019-12-02 17:09:06
Say I have the following interface for exposing a paged list public interface IPagedList<T> { IEnumerable<T> PageResults { get; } int CurrentPageIndex { get; } int TotalRecordCount { get; } int TotalPageCount { get; } int PageSize { get; } } Now I want to create a paging control public class PagedListPager<T> { public PagedListPager<T>(IPagedList<T> list) { _list = list; } public void RenderPager() { for (int i = 1; i < list.TotalPageCount; i++) RenderLink(i); } } The paging control has no interest in T (the actual contents of the list). It only requires the number of pages, current page etc.

Get all AsClosedTypesOf registration variants from the Autofac Builder

允我心安 提交于 2019-12-02 16:36:55
问题 Lets assume these classes/interfaces: public interface ICommand { } public class SomeCommand : ICommand { } public interface ICommandHandler<T> where T : ICommand { void Handle(T arg); } public class SomeCommandHandler : ICommandHandler<SomeCommand> { void Handle(SomeCommand arg){ /* do something */ } } public interface ICommandBus { void RegisterHandler<T>(T t) where T : ICommandHandler<T>; void RegisterHandlerByParam<T2>(ICommandHandler<T2> t2) where T2 : ICommand; void