Created
March 28, 2018 22:06
-
-
Save malexandersalazar/3da49aabbbe080c231550857d65a7e64 to your computer and use it in GitHub Desktop.
[.NET] ¿Cómo trabajar async con Enterprise Library?
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
using AEL.Api.Entities; | |
using AEL.Api.Repositories; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
namespace AEL.Api.Controllers | |
{ | |
public class EntitiesController : ApiController | |
{ | |
public List<MyEntity> Get() | |
{ | |
var repository = new MyEntityRepository(); | |
return repository.GetAllEntities(); | |
} | |
public void Put(MyEntity myEntity) | |
{ | |
var repository = new MyEntityRepository(); | |
repository.Update(myEntity); | |
} | |
} | |
} |
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
using AEL.Api.Entities; | |
using AEL.Api.Repositories; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
namespace AEL.Api.Controllers | |
{ | |
public class EntitiesController : ApiController | |
{ | |
public async Task<List<MyEntity>> Get() | |
{ | |
var repository = new MyEntityRepository(); | |
return await repository.GetAllEntitiesAsync(); | |
} | |
public async Task Put(MyEntity myEntity) | |
{ | |
var repository = new MyEntityRepository(); | |
await repository.UpdateAsync(myEntity); | |
} | |
} | |
} |
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
using AEL.Api.Entities; | |
using Microsoft.Practices.EnterpriseLibrary.Data; | |
using System; | |
using System.Collections.Generic; | |
using System.Data.Common; | |
using System.Threading.Tasks; | |
namespace AEL.Api.Repositories | |
{ | |
public class MyEntityRepository | |
{ | |
public List<MyEntity> GetAllEntitiesAsync() | |
{ | |
var database = DatabaseFactory.CreateDatabase(); | |
var entities = new List<MyEntity>(); | |
using (var reader = database.ExecuteReader("[dbo].[myTable_sel]")) | |
{ | |
var a = reader.GetOrdinal("Id"); | |
var b = reader.GetOrdinal("Names"); | |
var c = reader.GetOrdinal("Address"); | |
var d = reader.GetOrdinal("Email"); | |
var e = reader.GetOrdinal("Code"); | |
var f = reader.GetOrdinal("Lat"); | |
var g = reader.GetOrdinal("Lng"); | |
while (reader.Read()) | |
{ | |
entities.Add(new MyEntity | |
{ | |
Id = reader.GetInt32(a), | |
Names = reader.GetString(b), | |
Address = reader.GetString(c), | |
Email = reader.GetString(d), | |
Code = reader.GetString(e), | |
Lat = reader.GetDouble(f), | |
Lng = reader.GetDouble(g) | |
}); | |
} | |
} | |
return entities; | |
} | |
public void Update(MyEntity myEntity) | |
{ | |
var database = DatabaseFactory.CreateDatabase(); | |
using(var connection = database.CreateConnection()) | |
database.ExecuteNonQuery("[dbo].[myTable_upd]", myEntity.Code, myEntity.Email); | |
} | |
} | |
} |
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
using AEL.Api.Entities; | |
using Microsoft.Practices.EnterpriseLibrary.Data; | |
using System; | |
using System.Collections.Generic; | |
using System.Data.Common; | |
using System.Threading.Tasks; | |
namespace AEL.Api.Repositories | |
{ | |
public class MyEntityRepository | |
{ | |
public Task<List<MyEntity>> GetAllEntitiesAsync() | |
{ | |
var taskCompletionSource = new TaskCompletionSource<List<MyEntity>>(); | |
var database = DatabaseFactory.CreateDatabase(); | |
database.BeginExecuteReader("[dbo].[myTable_sel]", (asyncResult) => | |
{ | |
try | |
{ | |
var entities = new List<MyEntity>(); | |
var db = (Database)asyncResult.AsyncState; | |
using (var reader = db.EndExecuteReader(asyncResult)) | |
{ | |
var a = reader.GetOrdinal("Id"); | |
var b = reader.GetOrdinal("Names"); | |
var c = reader.GetOrdinal("Address"); | |
var d = reader.GetOrdinal("Email"); | |
var e = reader.GetOrdinal("Code"); | |
var f = reader.GetOrdinal("Lat"); | |
var g = reader.GetOrdinal("Lng"); | |
while (reader.Read()) | |
{ | |
entities.Add(new MyEntity | |
{ | |
Id = reader.GetInt32(a), | |
Names = reader.GetString(b), | |
Address = reader.GetString(c), | |
Email = reader.GetString(d), | |
Code = reader.GetString(e), | |
Lat = reader.GetDouble(f), | |
Lng = reader.GetDouble(g) | |
}); | |
} | |
} | |
taskCompletionSource.TrySetResult(entities); | |
} | |
catch (Exception ex) | |
{ | |
taskCompletionSource.TrySetException(ex); | |
} | |
}, database); | |
return taskCompletionSource.Task; | |
} | |
public Task UpdateAsync(MyEntity myEntity) | |
{ | |
var taskCompletionSource = new TaskCompletionSource<bool>(); | |
var database = DatabaseFactory.CreateDatabase(); | |
var connection = database.CreateConnection(); | |
database.BeginExecuteNonQuery("[dbo].[myTable_upd]", (asyncResult) => | |
{ | |
try | |
{ | |
dynamic states = asyncResult.AsyncState; | |
var db = (Database)states.database; | |
var cn = (DbConnection)states.connection; | |
db.EndExecuteNonQuery(asyncResult); | |
cn.Close(); | |
taskCompletionSource.TrySetResult(true); | |
} | |
catch (Exception ex) | |
{ | |
taskCompletionSource.TrySetException(ex); | |
} | |
}, new { database, connection }, myEntity.Code, myEntity.Email); | |
return taskCompletionSource.Task; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment