This repository has been archived by the owner on Feb 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace hbulens.Exam70487.Repositories | ||
{ | ||
public abstract class AdoRepository<T> : IRepository<T> | ||
{ | ||
#region Constructor | ||
|
||
public AdoRepository(string connection) | ||
{ | ||
string conectionString = ConfigurationManager.ConnectionStrings[connection].ConnectionString; | ||
this.Connection = new SqlConnection(conectionString); | ||
} | ||
|
||
public AdoRepository(string connection, string table) : this(connection) | ||
{ | ||
this.Table = table; | ||
} | ||
|
||
#endregion Constructor | ||
|
||
#region Properties | ||
|
||
private SqlConnection Connection { get; set; } | ||
protected virtual string Table { get; set; } | ||
|
||
#endregion Properties | ||
|
||
#region Methods | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <returns></returns> | ||
public IEnumerable<T> Get() | ||
{ | ||
this.Connection.Open(); | ||
|
||
using (SqlCommand command = this.Connection.CreateCommand()) | ||
{ | ||
command.CommandText = string.Format("SELECT * FROM {0}", this.Table); | ||
using (var reader = command.ExecuteReader()) | ||
{ | ||
while (reader.Read()) | ||
{ | ||
yield return this.Map(reader); | ||
} | ||
} | ||
} | ||
|
||
this.Connection.Close(); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="filter"></param> | ||
/// <returns></returns> | ||
public IEnumerable<T> Get(Expression<Func<T, bool>> filter) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
this.Connection.Dispose(); | ||
} | ||
|
||
protected T Map(IDataRecord record) | ||
{ | ||
T item = Activator.CreateInstance<T>(); | ||
IDictionary<string, int> fields = record.GetAllNames(); | ||
|
||
foreach (var property in typeof(T).GetProperties()) | ||
{ | ||
if (fields.Select(x => x.Key).Contains(property.Name) && !record.IsDBNull(record.GetOrdinal(property.Name))) | ||
{ | ||
property.SetValue(item, record[property.Name]); | ||
} | ||
} | ||
return item; | ||
} | ||
|
||
#endregion Methods | ||
|
||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
hbulens.Exam70487.Repositories/Utilities/AdoNetUtilities.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace hbulens.Exam70487.Repositories | ||
{ | ||
internal static class AdoNetUtilities | ||
{ | ||
internal static Dictionary<string, int> GetAllNames(this IDataRecord record) | ||
{ | ||
var result = new Dictionary<string, int>(); | ||
for (int i = 0; i < record.FieldCount; i++) | ||
{ | ||
result.Add(record.GetName(i), i); | ||
} | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
hbulens.Exam70487.WebApi/Repositories/CustomerRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using hbulens.Exam70487.Common; | ||
using hbulens.Exam70487.Repositories; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace hbulens.Exam70487.Core.Repositories | ||
{ | ||
public class CustomerRepository : AdoRepository<Customer> | ||
{ | ||
public CustomerRepository(string connection) : base(connection, "Customers") | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters