Roman Roman - 1 year ago 139
C# Question

How to register all implementations of Generic interface in autofac?

I have created generic interface that suppose to map entities to view models and backwards. I have to make around 80 registrations in autofac configuration. Is it possible to register them as batch?
Here is the interface:

public interface ICommonMapper<TEntity, TModel, TKey>
where TEntity : BaseEntity<TKey>
where TModel : BaseEntityViewModel<TKey>
where TKey : struct
TModel MapEntityToModel(TEntity entity);
TModel MapEntityToModel(TEntity entity, TModel model);
TEntity MapModelToEntity(TModel model);
TEntity MapModelToEntity(TModel model, TEntity entity);


Answer Source

You could use :


Where assemblies is a collection of the assemblies where your types belong.

if you have a PersonMapper that inherits from ICommonMapper<Person, PersonModel, Int32>, Autofac will be able to resolve ICommonMapper<Person, PersonModel, Int32>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download