Web/Web Api
-----------
public class SubProjApiProductController : ApiController
{
private readonly IWebApiBusinessFactory _webApiBusinessFactory;
public SubProjApiProductController(IWebApiBusinessFactory webApiBusinessFactory)
{
_webApiBusinessFactory = webApiBusinessFactory;
}
[Route("api/products")]
[HttpPost]
public async Task<IHttpActionResult> GetProducts([FromBody] IDictionary<string, string> data)
{
try
{
return Ok(await _webApiBusinessFactory.QueryProductService().GetProductsAsync(data).ConfigureAwait(false));
}
catch (Exception ex)
{
return Content(HttpStatusCode.NotFound, ex.Message);
//return NotFound();
}
}
[Route("api/addproduct")]
[HttpPost]
public async Task<IHttpActionResult> AddProduct([FromBody] Product product)
{
try
{
await _webApiBusinessFactory.CommandProductService().InsertAsync(product).ConfigureAwait(false);
return Ok();
}
catch (Exception ex)
{
return Content(HttpStatusCode.NotFound, ex.Message);
//return NotFound();
}
}
}
public static class UnityConfig
{
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below.
// Make sure to add a Unity.Configuration to the using statements.
// container.LoadConfiguration();
// container.AddNewExtension<BusinessUnityContainerExtension>(); //for non-parameter
container.AddExtension(new BusinessUnityContainerExtension(_connectionString, _connectionTimeout));
//Product Api
container.RegisterType<SubProjApiProductController>(new InjectionConstructor(new ResolvedParameter<IWebApiBusinessFactory>()));
}
}
Business Layer
--------------
public class BusinessUnityContainerExtension : UnityContainerExtension
{
#region Fields
private readonly string _connectionString;
private readonly int? _connectionTimeout;
#endregion Fields
public BusinessUnityContainerExtension(string connectionString, int? connectionTimeout)
{
_connectionString = connectionString;
_connectionTimeout = connectionTimeout ?? 0;
}
protected override void Initialize()
{
Container.RegisterType<IUnitOfWorkFactory, UnitOfWorkFactory>(new HierarchicalLifetimeManager());
Container.RegisterType<IWebApiBusinessFactory, WebApiBusinessFactory>(new HierarchicalLifetimeManager(), new InjectionConstructor(new ResolvedParameter<IUnitOfWorkFactory>()));
}
}
No comments:
Post a Comment