public EntityUpdateHandler(DecodedDto dtoInfo, DecodedEntityClass entityInfo, IWrappedConfigAndMapper configAndMapper, DbContext context) { _dtoInfo = dtoInfo ?? throw new ArgumentNullException(nameof(dtoInfo)); _entityInfo = entityInfo ?? throw new ArgumentNullException(nameof(entityInfo)); _configAndMapper = configAndMapper ?? throw new ArgumentNullException(nameof(configAndMapper)); _context = context ?? throw new ArgumentNullException(nameof(context)); }
public CreateConfigGenerator(Type dtoType, DecodedEntityClass entityInfo, object configInfo) { var myGeneric = typeof(ConfigGenerator <,>); var copierType = myGeneric.MakeGenericType(dtoType, entityInfo.EntityType); Accessor = Activator.CreateInstance(copierType, new object[] { configInfo }); }
public EntityUpdateHandler(DecodedDto dtoInfo, DecodedEntityClass entityInfo, IWrappedAutoMapperConfig wrapperMapperConfigs, DbContext context) { _dtoInfo = dtoInfo ?? throw new ArgumentNullException(nameof(dtoInfo)); _entityInfo = entityInfo ?? throw new ArgumentNullException(nameof(entityInfo)); _wrapperMapperConfigs = wrapperMapperConfigs ?? throw new ArgumentNullException(nameof(wrapperMapperConfigs)); _context = context ?? throw new ArgumentNullException(nameof(context)); }
public TestDecodedDtoKeyIsString() { var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { _EntityInfo = new DecodedEntityClass(typeof(DddCompositeIntString), context); } }
public TestDecodedDto() { var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { _bookEntityInfo = new DecodedEntityClass(typeof(Book), context); } }
public TestCreateMapGenerator() { var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { _bookInfo = new DecodedEntityClass(typeof(Book), context); _authorInfo = new DecodedEntityClass(typeof(Author), context); } }
public void TestReadOnlyEntityDecoded() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { //ATTEMPT var decoded = new DecodedEntityClass(typeof(ReadOnlyEntity), context); //VERIFY decoded.EntityStyle.ShouldEqual(EntityStyles.ReadOnly); decoded.CanBeUpdatedViaProperties.ShouldBeFalse(); decoded.HasPublicParameterlessCtor.ShouldBeFalse(); decoded.CanBeUpdatedViaMethods.ShouldBeFalse(); decoded.CanBeCreatedByCtorOrStaticMethod.ShouldBeFalse(); } }
public void TestCompositeKeyCopyBack() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { var decodedEntity = new DecodedEntityClass(typeof(DddCompositeIntString), context); //ATTEMPT var entity = new DddCompositeIntString("Hello", 999); var dto = new DddCompositeIntStringCreateDto(); entity.CopyBackKeysFromEntityToDtoIfPresent(dto, decodedEntity); //VERIFY dto.MyString.ShouldEqual("Hello"); dto.MyInt.ShouldEqual(999); } }
public void TestAbstractSetterKeyNotCopiedBack() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { var decodedEntity = new DecodedEntityClass(typeof(NormalEntity), context); //ATTEMPT var entity = new NormalEntity { Id = 123 }; var dto = new NormalEntityKeyAbstractDto(); entity.CopyBackKeysFromEntityToDtoIfPresent(dto, decodedEntity); //VERIFY dto.Id.ShouldEqual(0); } }
public void TestNormalKeyExtract() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { var decodedEntity = new DecodedEntityClass(typeof(NormalEntity), context); var decodeDto = new DecodedDto(typeof(NormalEntityDto), decodedEntity, new GenericServicesConfig(), null); //ATTEMPT var dto = new NormalEntityDto { Id = 123 }; var keys = context.GetKeysFromDtoInCorrectOrder(dto, decodeDto); //VERIFY ((int)keys[0]).ShouldEqual(123); } }
public void TestAbstractMainDecodedEntityClass() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { //ATTEMPT var decoded = new DecodedEntityClass(typeof(TestAbstractMain), context); //VERIFY decoded.EntityStyle.ShouldEqual(EntityStyles.Hybrid); decoded.PrimaryKeyProperties.Single().Name.ShouldEqual(nameof(TestAbstractBase.Id)); decoded.CanBeUpdatedViaMethods.ShouldBeFalse(); decoded.CanBeUpdatedViaProperties.ShouldBeTrue(); decoded.CanBeCreatedByCtorOrStaticMethod.ShouldBeTrue(); decoded.PublicCtors.Length.ShouldEqual(1); decoded.PublicStaticCreatorMethods.Length.ShouldEqual(0); decoded.PublicSetterMethods.Length.ShouldEqual(0); decoded.PropertiesWithPublicSetter.Length.ShouldEqual(2); } }
public void TestDddCtorAndFactEntityDecoded() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { //ATTEMPT var decoded = new DecodedEntityClass(typeof(DddCtorAndFactEntity), context); //VERIFY decoded.EntityStyle.ShouldEqual(EntityStyles.DDDStyled); decoded.CanBeCreatedViaAutoMapper.ShouldBeFalse(); decoded.CanBeUpdatedViaProperties.ShouldBeFalse(); decoded.HasPublicParameterlessCtor.ShouldBeFalse(); decoded.CanBeUpdatedViaMethods.ShouldBeTrue(); decoded.CanBeCreatedByCtorOrStaticMethod.ShouldBeTrue(); decoded.PublicCtors.Length.ShouldEqual(1); decoded.PublicSetterMethods.Length.ShouldEqual(3); decoded.PublicStaticCreatorMethods.Length.ShouldEqual(1); } }
public void OrderDecodedEntityClass() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { //ATTEMPT var decoded = new DecodedEntityClass(typeof(Order), context); //VERIFY decoded.EntityStyle.ShouldEqual(EntityStyles.DDDStyled); decoded.PrimaryKeyProperties.Single().Name.ShouldEqual(nameof(Order.OrderId)); decoded.CanBeUpdatedViaMethods.ShouldBeTrue(); decoded.CanBeUpdatedViaProperties.ShouldBeFalse(); decoded.CanBeCreatedByCtorOrStaticMethod.ShouldBeTrue(); decoded.PublicCtors.Length.ShouldEqual(0); decoded.PublicStaticCreatorMethods.Length.ShouldEqual(1); decoded.PublicSetterMethods.Length.ShouldEqual(1); decoded.PropertiesWithPublicSetter.Length.ShouldEqual(0); } }
public void AuthorDecodedEntityClass() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { //ATTEMPT var decoded = new DecodedEntityClass(typeof(Author), context); //VERIFY decoded.EntityStyle.ShouldEqual(EntityStyles.Standard); decoded.PrimaryKeyProperties.Single().Name.ShouldEqual(nameof(Author.AuthorId)); decoded.CanBeUpdatedViaMethods.ShouldBeFalse(); decoded.CanBeUpdatedViaProperties.ShouldBeTrue(); decoded.CanBeCreatedByCtorOrStaticMethod.ShouldBeFalse(); decoded.PublicCtors.Length.ShouldEqual(1); decoded.PublicStaticCreatorMethods.Length.ShouldEqual(0); decoded.PublicSetterMethods.Length.ShouldEqual(0); decoded.PropertiesWithPublicSetter.Select(x => x.Name) .ShouldEqual(new [] { nameof(Author.AuthorId), nameof(Author.Name), nameof(Author.Email), nameof(Author.BooksLink) }); } }
public static void CopyBackKeysFromEntityToDtoIfPresent <TDto>(this object newEntity, TDto dto, DecodedEntityClass entityInfo) { var dtoKeyProperies = typeof(TDto).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var entityKeys in entityInfo.PrimaryKeyProperties) { var dtoMatchingProperty = dtoKeyProperies.SingleOrDefault( x => x.Name == entityKeys.Name && x.PropertyType == entityKeys.PropertyType); if (dtoMatchingProperty == null) { continue; } dtoMatchingProperty.SetValue(dto, entityKeys.GetValue(newEntity)); } }
public GenericMapper(DbContext context, IWrappedConfigAndMapper wrappedMapper, DecodedEntityClass entityInfo) { _context = context ?? throw new ArgumentNullException(nameof(context)); _wrappedMapper = wrappedMapper ?? throw new ArgumentNullException(nameof(wrappedMapper)); _entityInfo = entityInfo ?? throw new ArgumentNullException(nameof(entityInfo)); }
public CreateMapper(DbContext context, IWrappedConfigAndMapper configAndMapper, Type tDto, DecodedEntityClass entityInfo) { var myGeneric = typeof(GenericMapper <,>); var genericType = myGeneric.MakeGenericType(tDto, entityInfo.EntityType); var constructor = genericType.GetConstructors().Single(); Accessor = GetNewGenericMapper(genericType, constructor).Invoke(context, configAndMapper, entityInfo); //Using Activator.CreateInstance with dynamic takes twice as long as LINQ new - see TestNewCreateMapper //Accessor = Activator.CreateInstance(genericType, context, configAndMapper, entityInfo); }
public static void CopyBackKeysFromEntityToDtoIfPresent <TDto>(this object newEntity, TDto dto, DecodedEntityClass entityInfo) { var dtoKeyProperies = typeof(TDto).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var entityKeys in entityInfo.PrimaryKeyProperties) { var dtoMatchingProperty = dtoKeyProperies.SingleOrDefault( x => x.Name == entityKeys.Name && x.PropertyType == entityKeys.PropertyType); if (dtoMatchingProperty != null && //found one dtoMatchingProperty.CanWrite && //CanWrite dtoMatchingProperty.SetMethod.IsPublic) //setter is public { dtoMatchingProperty.SetValue(dto, entityKeys.GetValue(newEntity)); } } }