public void RegisterForDbContext( Type dbContextType, IIocManager iocManager, AutoRepositoryTypesAttribute defaultAutoRepositoryTypesAttribute) { var autoRepositoryAttr = dbContextType.GetTypeInfo().GetSingleAttributeOrNull <AutoRepositoryTypesAttribute>() ?? defaultAutoRepositoryTypesAttribute; RegisterForDbContext( dbContextType, iocManager, autoRepositoryAttr.RepositoryInterface, autoRepositoryAttr.RepositoryInterfaceWithPrimaryKey, autoRepositoryAttr.RepositoryImplementation, autoRepositoryAttr.RepositoryImplementationWithPrimaryKey ); if (autoRepositoryAttr.WithDefaultRepositoryInterfaces) { RegisterForDbContext( dbContextType, iocManager, defaultAutoRepositoryTypesAttribute.RepositoryInterface, defaultAutoRepositoryTypesAttribute.RepositoryInterfaceWithPrimaryKey, autoRepositoryAttr.RepositoryImplementation, autoRepositoryAttr.RepositoryImplementationWithPrimaryKey ); } }
static AutoRepositoryTypesAttribute() { Default = new AutoRepositoryTypesAttribute( typeof(IRepository <>), typeof(IRepository <,>), typeof(EfRepositoryBase <, , ,>), typeof(EfRepositoryBase <, , , ,>) ); }
static AutoRepositoryTypesAttribute() { Default = new AutoRepositoryTypesAttribute( typeof(IRepository<>), typeof(IRepository<,>), typeof(EfRepositoryBase<,,,>), typeof(EfRepositoryBase<,,,,>) ); }
private static Type GetMakedGenericType <TTenantId, TUserId>(bool hasPrimaryKey, IIocManager iocManager, AutoRepositoryTypesAttribute autoRepositoryAttr, Type dbContextType, Type entityType, Type primaryKeyType) { var genericArgs = hasPrimaryKey ? autoRepositoryAttr.RepositoryImplementationWithPrimaryKey.GetGenericArguments() : autoRepositoryAttr.RepositoryImplementation.GetGenericArguments(); var types = new List <Type>(); foreach (var ga in genericArgs) { switch (ga.Name) { case "TDbContext": types.Add(dbContextType); break; case "TEntity": types.Add(entityType); break; case "TPrimaryKey": types.Add(primaryKeyType); break; case "TTenantId": types.Add(typeof(TTenantId)); break; case "TUserId": types.Add(typeof(TUserId)); break; } } var implType = hasPrimaryKey ? autoRepositoryAttr.RepositoryImplementationWithPrimaryKey.MakeGenericType(types.ToArray()) : autoRepositoryAttr.RepositoryImplementation.MakeGenericType(types.ToArray()); if (string.IsNullOrEmpty(implType.FullName)) { var makedDbContextType = dbContextType.MakeGenericType(typeof(TTenantId), typeof(TUserId)); if (!iocManager.IsRegistered(makedDbContextType)) { iocManager.Register(makedDbContextType, DependencyLifeStyle.Transient); } if (types[0] == dbContextType) { types[0] = makedDbContextType; } else { throw new InvalidOperationException("Type of TDbContext can not be substituted."); } implType = hasPrimaryKey ? autoRepositoryAttr.RepositoryImplementationWithPrimaryKey.MakeGenericType(types.ToArray()) : autoRepositoryAttr.RepositoryImplementation.MakeGenericType(types.ToArray()); } return(implType); }