GithubHelp home page GithubHelp logo

mclarkkeefe / microentities Goto Github PK

View Code? Open in Web Editor NEW

This project forked from burnettderek/microentities

0.0 0.0 0.0 22 KB

MicroEntities are generic CRUD (Create Read Update Delete) templates used to rapidly develop and configure multi-tiered applications.

License: Apache License 2.0

C# 100.00%

microentities's Introduction

MicroEntities

MicroEntities are generic CRUD (Create Read Update Delete) templates used to rapidly develop and configure multi-tiered applications.

Creation and Configuration

Create a SQL Server data layer for object 'User' using table 'Users' for storage, and has methods Create, Select, Update, and Delete:

var dataLayer = new SqlServerSystemLayer<User>("server=[server_name];database=[database_name];Trusted_Connection=SSPI", "Users");

Create a validation layer for 'User' which validates the username, password, and balance fields:

var validationLayer = new FluentValidationLayer<User>(validator =>
{
	validator.RuleFor(employee => employee.UserName).Length(1, 50).Matches("^[a-zA-Z'., -]*$");
	validator.RuleFor(employee => employee.Password).Length(1, 50).Matches("^[a-zA-Z0-9]*$");
	validator.RuleFor(user => user.Balance).GreaterThanOrEqualTo(0);
});

Create a layer which benchmarks the underlying layers performance and logs the time take for each operation:

var benchmarkingLayer = new BenchmarkingLayer<User>();

Create a public layer which maps the internal object to one used for the public web service:

var userSystem = new PublicEntitySystem<UserDto, User>();

Create an entire stack for object 'User', with database, benchmarking and validation:

var userSystem = new PublicEntitySystem<UserDto, User>();

userSystem
.AddLayer(new BenchmarkingLayer<User>())
.AddLayer(new FluentValidationLayer<User>(validator =>
{
	validator.RuleFor(employee => employee.UserName).Length(1, 50).Matches("^[a-zA-Z'., -]*$");
	validator.RuleFor(employee => employee.Password).Length(1, 50).Matches("^[a-zA-Z0-9]*$");
	validator.RuleFor(user => user.Balance).GreaterThanOrEqualTo(0);
}))
.AddLayer(new SqlServerSystemLayer<User>("server=[computer_name];database=[database_name];Trusted_Connection=SSPI", "Users"));

Operations

Create a valid 'User' object via webservice:

[HttpPost]
public async Task<ActionResult> Create([FromBody] UserDto user)
{
	try
	{
		NotNull.Check(nameof(user.UserName), user.UserName); NotNull.Check(nameof(user.Password), user.Password);
		NotNull.Check(nameof(user.Balance), user.Balance);
		var result = await _userSystem.Create(user);
		return Ok(result);
	}
	catch (ArgumentException ex)
	{
		return BadRequest(ex.Message);
	}
	catch (Exception ex)
	{
		return StatusCode((int)HttpStatusCode.InternalServerError);
	}
}

Read a 'User' where 'Key' field is equal to 'key':

[HttpGet]
public async Task<ActionResult> Read(Guid key)
{
	try
	{
		NotNull.Check(nameof(key), key);
		var result = await _userSystem.Select(Where.Equal("Key", key ));
		return Ok(result);
	}
	catch (ArgumentException ex)
	{
		return BadRequest(ex.Message);
	}
	catch (Exception ex)
	{
		return StatusCode((int)HttpStatusCode.InternalServerError);
	}
}

Update an entire 'User' object:

[HttpPut("/edit")]
public async Task<ActionResult> EditUser([FromBody] UserDto user)
{
	try
	{
		NotNull.Check(nameof(user.Key), user.Key);
		var result = await _userSystem.Update(user, Where.Equal(nameof(user.Key), user.Key));
		return Ok(result);
	}
	catch (ArgumentException ex)
	{
		return BadRequest(ex.Message);
	}
	catch (Exception ex)
	{
		return StatusCode((int)HttpStatusCode.InternalServerError);
	}
}

Update a single property of the 'User' class as well as an auditing field:

[HttpPut("/edit/{key}/{property}/{value}")]
public async Task<ActionResult> Edit(string key, string property, string value)
{
	try
	{
		NotNull.Check(nameof(key), key); NotNull.Check(nameof(property), property); NotNull.Check(nameof(value), value);
		if (!property.Equals("Balance"))
		{
			var result = await _userSystem.Update(Set.Value(property, value).And("LastUpdatedOn", DateTime.Now), 
													Where.Equal("Key", key));
			return Ok(result);
		}
		else
		{
			var result = await _userSystem.Update(Set.Value(property, decimal.Parse(value)).And("LastUpdatedOn", DateTime.Now), 
													Where.Equal("Key", key));
			return Ok(result);
		}
	}
	catch (ArgumentException ex)
	{
		return BadRequest(ex.Message);
	}
	catch (Exception ex)
	{
		return StatusCode((int)HttpStatusCode.InternalServerError);
	}
}

Delete a user based on a field:

[HttpDelete]
public async Task<ActionResult> Delete(Guid value)
{
	try
	{
		NotNull.Check("the value specified", value);
		await _userSystem.Delete(Where.Equal("Key", value));
		return Ok();
	}
	catch (ArgumentException ex)
	{
		return BadRequest(ex.Message);
	}
	catch (Exception ex)
	{
		return StatusCode((int)HttpStatusCode.InternalServerError);
	}
}

microentities's People

Contributors

burnettderek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.