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

unnecessary codes was removed #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,5 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd
/.vscode
26 changes: 14 additions & 12 deletions AdoWrapper/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ internal static List<PropertyInfo> GetWritableProperties<T>()
{
var type = typeof(T);

return TypeProperties.GetOrAdd(type, _ =>
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
//Only writable properties
.Where(p => p.GetSetMethod() is not null).ToList();
});
// return TypeProperties.GetOrAdd(type, _ =>
// {
// return type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
// //Only writable properties
// .Where(p => p.GetSetMethod() is not null).ToList();
// });
return GetWritableProperties(type);
}

internal static List<PropertyInfo> GetWritableProperties(this object obj)
{
var type = obj.GetType();

return TypeProperties.GetOrAdd(type, _ =>
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
//Only writable properties
.Where(p => p.GetSetMethod() is not null).ToList();
});
// return TypeProperties.GetOrAdd(type, _ =>
// {
// return type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
// //Only writable properties
// .Where(p => p.GetSetMethod() is not null).ToList();
// });
return GetWritableProperties(type);
}

internal static List<PropertyInfo> GetWritableProperties(this Type type)
Expand Down
15 changes: 13 additions & 2 deletions AdoWrapper/Infrastructure/AdoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public AdoProvider(IOptions<ConnectionStringModel> connectionStringModel)

var listProperties = TypeExtensions.GetListProperties<T>();

while (reader.Read())
//The original version of this method runs to the last record and returns the last item, in fact, LastOrDefault

bool anyRecordReaded = false;

while (reader.Read() && !anyRecordReaded )
{
result = new T();
foreach (var property in properties)
Expand Down Expand Up @@ -85,9 +89,12 @@ public AdoProvider(IOptions<ConnectionStringModel> connectionStringModel)
var value = reader.GetValue(reader.GetOrdinal(property.Name));
property.SetValue(result, value);
}
anyRecordReaded = true;
}

return result;

// return this.GetFirstOrDefaultAsync<T>(sql).Result;
}

public async Task<T> GetFirstOrDefaultAsync<T>(string sql) where T : class, new()
Expand All @@ -103,8 +110,10 @@ public AdoProvider(IOptions<ConnectionStringModel> connectionStringModel)
await using SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection).ConfigureAwait(false);

var listProperties = TypeExtensions.GetListProperties<T>();
//The original version of this method runs to the last record and returns the last item, in fact, LastOrDefault

while (await reader.ReadAsync().ConfigureAwait(false))
bool anyRecordReaded = false;
while (await reader.ReadAsync().ConfigureAwait(false) && !anyRecordReaded)
{
result = new T();

Expand Down Expand Up @@ -155,6 +164,8 @@ public AdoProvider(IOptions<ConnectionStringModel> connectionStringModel)
var value = await reader.GetFieldValueAsync<object>(reader.GetOrdinal(property.Name)).ConfigureAwait(false);
property.SetValue(result, value);
}

anyRecordReaded = true;
}

return result;
Expand Down