Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace Activator.CreateInstance<T>() with new T() #841

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion RepoDb.Core/RepoDb.Tests/RepoDb.IntegrationTests/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public static string GetUnicodeString()
/// <param name="strict">True if to be strict on the conversion.</param>
/// <returns>The instance of the converted object.</returns>
public static T ConverToType<T>(object obj, bool strict = true)
where T : new()
{
var fromType = obj.GetType();
var toTypeProperties = typeof(T).GetProperties();
Expand All @@ -100,7 +101,7 @@ public static T ConverToType<T>(object obj, bool strict = true)
}
if (result == null)
{
result = Activator.CreateInstance<T>();
result = new T();
}
toProperty.SetValue(result, property.GetValue(obj));
});
Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/BaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace RepoDb
/// <typeparam name="TEntity">The type of data entity object to be mapped on this repository.</typeparam>
/// <typeparam name="TDbConnection">The type of the <see cref="DbConnection"/> object.</typeparam>
public abstract partial class BaseRepository<TEntity, TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
where TEntity : class
{
#region Constructors
Expand Down
14 changes: 9 additions & 5 deletions RepoDb.Core/RepoDb/DbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace RepoDb
/// </summary>
/// <typeparam name="TDbConnection">The type of the <see cref="DbConnection"/> object.</typeparam>
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region Fields

Expand Down Expand Up @@ -290,8 +290,10 @@ public TDbConnection CreateConnection(bool force)
{
if (this.connection == null)
{
connection = Activator.CreateInstance<TDbConnection>();
connection.ConnectionString = ConnectionString;
connection = new TDbConnection
{
ConnectionString = ConnectionString
};
this.connection = connection;
}
else
Expand All @@ -302,8 +304,10 @@ public TDbConnection CreateConnection(bool force)
}
else
{
connection = Activator.CreateInstance<TDbConnection>();
connection.ConnectionString = ConnectionString;
connection = new TDbConnection
{
ConnectionString = ConnectionString
};
}
return connection;
}
Expand Down
5 changes: 3 additions & 2 deletions RepoDb.Core/RepoDb/Mappers/ClassHandlerMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public static class ClassHandlerMapper
/// <typeparam name="TType">The target .NET CLR type.</typeparam>
/// <typeparam name="TClassHandler">The type of the handler.</typeparam>
/// <param name="force">A value that indicates whether to force the mapping. If one is already exists, then it will be overwritten.</param>
public static void Add<TType, TClassHandler>(bool force = false) =>
Add(typeof(TType), Activator.CreateInstance<TClassHandler>(), force);
public static void Add<TType, TClassHandler>(bool force = false)
where TClassHandler : new() =>
Add(typeof(TType), new TClassHandler(), force);

/// <summary>
/// Adds a mapping between a .NET CLR type and a <see cref="IClassHandler{TEntity}"/> object.
Expand Down
30 changes: 18 additions & 12 deletions RepoDb.Core/RepoDb/Mappers/EntityMapFluentDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,9 @@ public EntityMapFluentDefinition<TEntity> DbType(Field field,
/// </summary>
/// <typeparam name="TClassHandler">The type of the <see cref="IClassHandler{TEntity}"/>.</typeparam>
/// <returns>The current instance.</returns>
public EntityMapFluentDefinition<TEntity> ClassHandler<TClassHandler>() =>
ClassHandler<TClassHandler>(Activator.CreateInstance<TClassHandler>());
public EntityMapFluentDefinition<TEntity> ClassHandler<TClassHandler>()
where TClassHandler : new() =>
ClassHandler(new TClassHandler());

/// <summary>
/// Defines a mapping between a data entity type and a <see cref="IClassHandler{TEntity}"/> object. It uses the <see cref="Activator.CreateInstance(Type)"/> method to create the instance of target <see cref="IClassHandler{TEntity}"/>.
Expand All @@ -406,8 +407,9 @@ public EntityMapFluentDefinition<TEntity> ClassHandler<TClassHandler>() =>
/// <typeparam name="TClassHandler">The type of the <see cref="IClassHandler{TEntity}"/>.</typeparam>
/// <param name="force">A value that indicates whether to force the mapping. If one is already exists, then it will be overwritten.</param>
/// <returns>The current instance.</returns>
public EntityMapFluentDefinition<TEntity> ClassHandler<TClassHandler>(bool force) =>
ClassHandler<TClassHandler>(Activator.CreateInstance<TClassHandler>(), force);
public EntityMapFluentDefinition<TEntity> ClassHandler<TClassHandler>(bool force)
where TClassHandler : new() =>
ClassHandler(new TClassHandler(), force);

/// <summary>
/// Defines a mapping between a data entity type and a <see cref="IClassHandler{TEntity}"/> object.
Expand Down Expand Up @@ -447,8 +449,9 @@ public EntityMapFluentDefinition<TEntity> ClassHandler<TClassHandler>(TClassHand
/// <typeparam name="TPropertyHandler">The type of the <see cref="IPropertyHandler{TInput, TResult}"/>.</typeparam>
/// <param name="expression">The expression to be parsed.</param>
/// <returns>The current instance.</returns>
public EntityMapFluentDefinition<TEntity> PropertyHandler<TPropertyHandler>(Expression<Func<TEntity, object>> expression) =>
PropertyHandler<TPropertyHandler>(expression, Activator.CreateInstance<TPropertyHandler>());
public EntityMapFluentDefinition<TEntity> PropertyHandler<TPropertyHandler>(Expression<Func<TEntity, object>> expression)
where TPropertyHandler : new() =>
PropertyHandler(expression, new TPropertyHandler());

/// <summary>
/// Defines a mapping between a data entity type property and a <see cref="IPropertyHandler{TInput, TResult}"/> object. It uses the <see cref="Activator.CreateInstance(Type)"/> method to create the instance of target <see cref="IPropertyHandler{TInput, TResult}"/>.
Expand All @@ -459,8 +462,9 @@ public EntityMapFluentDefinition<TEntity> PropertyHandler<TPropertyHandler>(Expr
/// <param name="force">A value that indicates whether to force the mapping. If one is already exists, then it will be overwritten.</param>
/// <returns>The current instance.</returns>
public EntityMapFluentDefinition<TEntity> PropertyHandler<TPropertyHandler>(Expression<Func<TEntity, object>> expression,
bool force) =>
PropertyHandler<TPropertyHandler>(expression, Activator.CreateInstance<TPropertyHandler>(), force);
bool force)
where TPropertyHandler : new() =>
PropertyHandler(expression, new TPropertyHandler(), force);

/// <summary>
/// Defines a mapping between a data entity type property and a <see cref="IPropertyHandler{TInput, TResult}"/> object.
Expand Down Expand Up @@ -500,8 +504,9 @@ public EntityMapFluentDefinition<TEntity> PropertyHandler<TPropertyHandler>(Expr
/// <typeparam name="TPropertyHandler">The type of the <see cref="IPropertyHandler{TInput, TResult}"/>.</typeparam>
/// <param name="propertyName">The name of the class property to be mapped.</param>
/// <returns>The current instance.</returns>
public EntityMapFluentDefinition<TEntity> PropertyHandler<TPropertyHandler>(string propertyName) =>
PropertyHandler<TPropertyHandler>(propertyName, Activator.CreateInstance<TPropertyHandler>(), false);
public EntityMapFluentDefinition<TEntity> PropertyHandler<TPropertyHandler>(string propertyName)
where TPropertyHandler : new() =>
PropertyHandler(propertyName, new TPropertyHandler(), false);

/// <summary>
/// Adds a <see cref="IPropertyHandler{TInput, TResult}"/> mapping into a data entity type property (via property name).
Expand Down Expand Up @@ -541,8 +546,9 @@ public EntityMapFluentDefinition<TEntity> PropertyHandler<TPropertyHandler>(stri
/// <typeparam name="TPropertyHandler">The type of the <see cref="IPropertyHandler{TInput, TResult}"/>.</typeparam>
/// <param name="field">The instance of <see cref="Field"/> object to be mapped.</param>
/// <returns>The current instance.</returns>
public EntityMapFluentDefinition<TEntity> PropertyHandler<TPropertyHandler>(Field field) =>
PropertyHandler<TPropertyHandler>(field, Activator.CreateInstance<TPropertyHandler>(), false);
public EntityMapFluentDefinition<TEntity> PropertyHandler<TPropertyHandler>(Field field)
where TPropertyHandler : new() =>
PropertyHandler(field, new TPropertyHandler(), false);

/// <summary>
/// Adds a <see cref="IPropertyHandler{TInput, TResult}"/> mapping into a data entity type property (via <see cref="Field"/> object).
Expand Down
40 changes: 24 additions & 16 deletions RepoDb.Core/RepoDb/Mappers/PropertyHandlerMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public static class PropertyHandlerMapper
/// <typeparam name="TType">The target .NET CLR type.</typeparam>
/// <typeparam name="TPropertyHandler">The type of the handler.</typeparam>
/// <param name="force">A value that indicates whether to force the mapping. If one is already exists, then it will be overwritten.</param>
public static void Add<TType, TPropertyHandler>(bool force = false) =>
Add(typeof(TType), Activator.CreateInstance<TPropertyHandler>(), force);
public static void Add<TType, TPropertyHandler>(bool force = false)
where TPropertyHandler : new() =>
Add(typeof(TType), new TPropertyHandler(), force);

/// <summary>
/// Type Level: Adds a mapping between a .NET CLR type and a <see cref="IPropertyHandler{TInput, TResult}"/> object.
Expand Down Expand Up @@ -167,8 +168,9 @@ public static void Remove(Type type)
/// <typeparam name="TPropertyHandler">The type of the property handler.</typeparam>
/// <param name="expression">The expression to be parsed.</param>
public static void Add<TEntity, TPropertyHandler>(Expression<Func<TEntity, object>> expression)
where TEntity : class =>
Add<TEntity, TPropertyHandler>(expression, Activator.CreateInstance<TPropertyHandler>(), false);
where TEntity : class
where TPropertyHandler : new() =>
Add(expression, new TPropertyHandler(), false);

/// <summary>
/// Property Level: Adds a property handler mapping into a data entity type property (via expression).
Expand All @@ -192,8 +194,9 @@ public static void Add<TEntity, TPropertyHandler>(Expression<Func<TEntity, objec
/// <param name="force">A value that indicates whether to force the mapping. If one is already exists, then it will be overwritten.</param>
public static void Add<TEntity, TPropertyHandler>(Expression<Func<TEntity, object>> expression,
bool force)
where TEntity : class =>
Add<TEntity, TPropertyHandler>(expression, Activator.CreateInstance<TPropertyHandler>(), force);
where TEntity : class
where TPropertyHandler : new() =>
Add(expression, new TPropertyHandler(), force);

/// <summary>
/// Property Level: Adds a property handler mapping into a data entity type property (via expression).
Expand All @@ -217,8 +220,9 @@ public static void Add<TEntity, TPropertyHandler>(Expression<Func<TEntity, objec
/// <typeparam name="TPropertyHandler">The type of the property handler.</typeparam>
/// <param name="propertyName">The instance of property handler.</param>
public static void Add<TEntity, TPropertyHandler>(string propertyName)
where TEntity : class =>
Add<TEntity, TPropertyHandler>(propertyName, Activator.CreateInstance<TPropertyHandler>(), false);
where TEntity : class
where TPropertyHandler : new() =>
Add<TEntity, TPropertyHandler>(propertyName, new TPropertyHandler(), false);

/// <summary>
/// Property Level: Adds a property handler mapping into a data entity type property (via property name).
Expand All @@ -242,8 +246,9 @@ public static void Add<TEntity, TPropertyHandler>(string propertyName,
/// <param name="force">A value that indicates whether to force the mapping. If one is already exists, then it will be overwritten.</param>
public static void Add<TEntity, TPropertyHandler>(string propertyName,
bool force)
where TEntity : class =>
Add<TEntity, TPropertyHandler>(propertyName, Activator.CreateInstance<TPropertyHandler>(), false);
where TEntity : class
where TPropertyHandler : new() =>
Add<TEntity, TPropertyHandler>(propertyName, new TPropertyHandler(), false);

/// <summary>
/// Property Level: Adds a property handler mapping into a data entity type property (via property name).
Expand Down Expand Up @@ -280,8 +285,9 @@ public static void Add<TEntity, TPropertyHandler>(string propertyName,
/// <typeparam name="TPropertyHandler">The type of the property handler.</typeparam>
/// <param name="field">The instance of <see cref="Field"/> object to be mapped.</param>
public static void Add<TEntity, TPropertyHandler>(Field field)
where TEntity : class =>
Add<TEntity, TPropertyHandler>(field, Activator.CreateInstance<TPropertyHandler>(), false);
where TEntity : class
where TPropertyHandler : new() =>
Add<TEntity, TPropertyHandler>(field, new TPropertyHandler(), false);

/// <summary>
/// Property Level: Adds a property handler mapping into a data entity type property (via <see cref="Field"/> object).
Expand All @@ -305,8 +311,9 @@ public static void Add<TEntity, TPropertyHandler>(Field field,
/// <param name="force">A value that indicates whether to force the mapping. If one is already exists, then it will be overwritten.</param>
public static void Add<TEntity, TPropertyHandler>(Field field,
bool force)
where TEntity : class =>
Add<TEntity, TPropertyHandler>(field, Activator.CreateInstance<TPropertyHandler>(), false);
where TEntity : class
where TPropertyHandler : new() =>
Add<TEntity, TPropertyHandler>(field, new TPropertyHandler(), false);

/// <summary>
/// Property Level: Adds a property handler mapping into a data entity type property (via <see cref="Field"/> object).
Expand Down Expand Up @@ -343,8 +350,9 @@ public static void Add<TEntity, TPropertyHandler>(Field field,
/// <typeparam name="TPropertyHandler">The type of the property handler.</typeparam>
/// <param name="propertyInfo">The instance of <see cref="PropertyInfo"/> to be mapped.</param>
internal static void Add<TEntity, TPropertyHandler>(PropertyInfo propertyInfo)
where TEntity : class =>
Add<TEntity, TPropertyHandler>(propertyInfo, Activator.CreateInstance<TPropertyHandler>(), false);
where TEntity : class
where TPropertyHandler : new() =>
Add<TEntity, TPropertyHandler>(propertyInfo, new TPropertyHandler(), false);

/// <summary>
/// Property Level: Adds a property handler mapping into a <see cref="PropertyInfo"/> object.
Expand Down
10 changes: 6 additions & 4 deletions RepoDb.Core/RepoDb/Mappers/TypeMapFluentDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public TypeMapFluentDefinition<TType> DbType(DbType? dbType,
/// </summary>
/// <typeparam name="TPropertyHandler">The type of the handler.</typeparam>
/// <returns>The current instance.</returns>
public TypeMapFluentDefinition<TType> PropertyHandler<TPropertyHandler>() =>
PropertyHandler<TPropertyHandler>(Activator.CreateInstance<TPropertyHandler>(), false);
public TypeMapFluentDefinition<TType> PropertyHandler<TPropertyHandler>()
where TPropertyHandler : new() =>
PropertyHandler(new TPropertyHandler(), false);

/// <summary>
/// Defines a mapping between a .NET CLR type and a <see cref="IPropertyHandler{TInput, TResult}"/> object. It uses the <see cref="Activator.CreateInstance(Type)"/> method to create the instance of target property handler.
Expand All @@ -62,8 +63,9 @@ public TypeMapFluentDefinition<TType> PropertyHandler<TPropertyHandler>() =>
/// <typeparam name="TPropertyHandler">The type of the handler.</typeparam>
/// <param name="force">A value that indicates whether to force the mapping. If one is already exists, then it will be overwritten.</param>
/// <returns>The current instance.</returns>
public TypeMapFluentDefinition<TType> PropertyHandler<TPropertyHandler>(bool force) =>
PropertyHandler<TPropertyHandler>(Activator.CreateInstance<TPropertyHandler>(), force);
public TypeMapFluentDefinition<TType> PropertyHandler<TPropertyHandler>(bool force)
where TPropertyHandler : new() =>
PropertyHandler(new TPropertyHandler(), force);

/// <summary>
/// Defines a mapping between a .NET CLR type and a <see cref="IPropertyHandler{TInput, TResult}"/> object.
Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/Average.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region Average<TEntity, TResult>

Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/AverageAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region AverageAll<TEntity>

Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/BatchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region BatchQuery<TEntity>

Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/Count.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region Count<TEntity>

Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/CountAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region CountAll<TEntity>

Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region Delete<TEntity>

Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/DeleteAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region DeleteAll<TEntity>

Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/Exists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region Exists<TEntity>

Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/Insert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region Insert<TEntity>

Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/InsertAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region InsertAll<TEntity>

Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbRepository/Max.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace RepoDb
{
public partial class DbRepository<TDbConnection> : IDisposable
where TDbConnection : DbConnection
where TDbConnection : DbConnection, new()
{
#region Max<TEntity, TResult>

Expand Down
Loading