Ampla Webservice Data Access and classes that supports a model based repository pattern
- Repository pattern for data access
- Attribute based declaration
- Automatic binding of Ampla data to C# classes
- Automatic mapping of Local date time to/from UTC
- Support for ASP.NET MVC Controllers
- Support for Forms Authentication
- Support for passing Ampla session via QueryString
Provides ReadOnly access to Ampla data.
public interface IReadOnlyRepository<TModel>
{
IList<TModel> GetAll();
TModel FindById(int id);
IList<TModel> FindByFilter(params FilterValue[] filters);
}IRepository Extends IReadOnlyRepository to provide write access
public interface IRepository<TModel> : IReadOnlyRepository<TModel>
{
/// Read access from IReadOnlyRepository
/// IList<TModel> GetAll();
/// TModel FindById(int id);
/// IList<TModel> FindByFilter(params FilterValue[] filters);
void Add(TModel model);
void Delete(TModel model);
void Update(TModel model);
void Confirm(TModel model);
void Unconfirm(TModel model);
}Attributes are used to declare models for use in the repository. Class attributes:
[AmplaLocation(Location="Enterprise.Site.Area.Point")][AmplaModule(Module="Production")][AmplaDefaultFields("Confirmed=True")]
Field attributes:
[AmplaField(Field = "Sample Period")]
These attributes are used to mark model classes for use in the Repository. Example:
namespace AmplaWeb.Models
{
[AmplaLocation(Location = "Enterprise.Site.Area.Example")]
[AmplaModule(Module = "Production")]
public class ExampleModel
{
public int Id {get; set;}
[AmplaField(Field = "Sample Period")]
public DateTime Sample { get; set; }
public string Shift { get; set; }
public double Tonnes { get; set; }
}
}The AmplaRepository<TModel> class will automatically map the properties to and from the model class.
Features include:
- Automatic property binding
- Automatic defaults for required fields such as
Sample Period - Id property is updated when a new record is added.
- DateTime properties are automatically presented as Local time in the model.
Example:
// set up Repository
IRepositorySet repositorySet = new AmplaRepositorySet(CredentialsProvider.ForUsernameAndPassword("User", "password"));
IRepository<ExampleModel> repository = repositorySet.GetRepository<ExampleModel>();
// create the model
ExampleModel model = new ExampleModel {Shift = "Day", Tonnes = 95.3};
// insert the model will create the model in Ampla
repository.Add(model);
// model's Id property will be updated.
int insertedId = model.Id;
// retrieve the current model
ExampleModel updatedModel = repository.FindById(insertedId);
// All properties are automatically bound to the Ampla values.Provides RepositoryController<TModel> to provide actions for models.
Example:
using AmplaWeb.Data;
using AmplaWeb.Data.Controllers;
using AmplaWeb.Sample.Models;
namespace AmplaWeb.Sample.Controllers
{
public class ProductionController : RepositoryController<ProductionModel>
{
public ProductionController(IRepository<ProductionModel> repository) : base(repository)
{
}
}
}The IRepositoryController<TModel> provides the following actions for models.
GET/{Model}GET/{Model}/IndexGET/{Model}/Details/{Id}GET/{Model}/CreatePOST/{Model}/CreateGET/{Model}/Edit/{Id}POST/{Model}/EditGET/{Model}/Delete/{Id}POST/{Model}/Delete/{Id}
using System.Web.Mvc;
namespace AmplaWeb.Data.Controllers
{
/// <summary>
/// Interface for Repository operations for the model
/// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam>
public interface IRepositoryController<in TModel>
{
/// <summary>
/// GET /{Model}/
/// </summary>
/// <returns></returns>
ActionResult Index();
/// <summary>
/// GET /{Model}/Details/{id}
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
ActionResult Details(int id = 0);
/// <summary>
/// GET /{Model}/Create
/// </summary>
/// <returns></returns>
ActionResult Create();
/// <summary>
/// POST /{Model}/Create
/// </summary>
/// <returns></returns>
[HttpPost]
ActionResult Create(TModel model);
/// <summary>
/// Get /{Model}/Edit/id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
ActionResult Edit(int id = 0);
/// <summary>
/// POST /{Model}/Edit
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
ActionResult Edit(TModel model);
/// <summary>
/// GET /{Model}/Delete/id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
ActionResult Delete(int id = 0);
/// <summary>
/// POST /{Model}/Delete/id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost, ActionName("Delete")]
ActionResult DeleteConfirmed(int id);
}
}Forms Authentication is supported using either a Username and password or passing the ampla session in the query string.
To pass the session via the query string, use the amplaSession key.
Example: http://localhost/?amplaSession=c064fac5-26f5-4ce4-9955-8c7a5491ead5
- Add a
Documentitem - Set the Modules
Production,Quality, etc - Specify the Url
http://localhost/production/index - Ensure that the
AppendSessionIdproperty isTrue
