Giter VIP home page Giter VIP logo

dado.componentmodel.mutations's Introduction

Dado.ComponentModel.Mutations

Provides attributes that are used to help ensure data integrity for objects used as data sources.

NuGet: https://www.nuget.org/packages/Dado.ComponentModel.Mutations

Sample Usage

The following class shows that a property should be converted to lowercase and replaced of invalid characters.

using System.Text.RegularExpressions;

public class User
{
	private string _userName;

	public string UserName {
		get {
			return _userName;
		}
		set {
			value = value.ToLower();

			_userName = Regex.Replace(value, @"[^a-z0-9._]", String.Empty);
		}
	}
}

Altering the example to use a mutation attributes will produce the following code.

using Dado.ComponentModel.DataMutations;

public class User
{
	[
		ToLower,
		RegexReplace(@"[^a-z0-9._]")
	]
	public string UserName { get; set; }
}

Now, when Mutator.Mutate(context); is executed the value of User.UserName will be lowercased and have all invalid characters replaced.

API Documentation

Further Development

This project is still initial development. APIs have been submitted to dotnet/corefx#7660 for review and are likely to change.

License

Dado.ComponentModel.Mutations is licensed under the Apache License, Version 2.0.

dado.componentmodel.mutations's People

Contributors

roydukkey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

dado.componentmodel.mutations's Issues

Add ToTitleCaseAttribute

Sure. Here an example that follows the same pattern for the existing ToLowerAttribute and ToUpperAttribute.

using System;
using System.Globalization;

namespace Dado.ComponentModel.DataMutations
{
	[AttributeUsage(AttributeTargets.Property)]
	public class ToTitleCaseAttribute : MutationAttribute
	{
		public CultureInfo CultureInfo { get; set; } = CultureInfo.CurrentCulture;
		public override int Priority { get; set; } = 40;

		protected override object MutateValue(object value, IMutationContext context)
		{
			if (value is string valueAsString && !String.IsNullOrWhiteSpace(valueAsString)) {
				return CultureInfo.TextInfo.ToTitleCase(valueAsString);
			}

			if (value is char valueAsChar) {
				return CultureInfo.TextInfo.ToTitleCase();
			}

			return value;
		}
	}
}

This should probably be including in this package.

Originally posted by @roydukkey in #16 (comment)

Add EnforceRange attribute for numeric range values

This attribute would ensure the value is within the bounds of a numeric range.

Determine Numeric Range Value
The range value should be determined firstly by the attribute's own value via the constructor or property. Should the attribute not specify a value, a RangeAttribute attribute would be sought.

Research
It is not yet determined if the range values should be including or exclude as part of the acceptable range. This behaviour will be determine bases on the function on the RangeAttribute.

Add ToSentenceCaseAttribute

Hi,
id like to know if its possible mutates value of property:

  • First char like Uppercase
  • The others chars in lowercase
    f.i. : the REAL thing ===> The real thing

thanks in advance

Add EnforceMaxLength attribute for strings and collections

This attribute would ensure the length of a string, array, or IEnumerable is not greater than a maximum length.

Determine Maximum Length Value
The maximum length should be determined firstly by the attribute's own value via the constructor or property. Should the attribute not specify a value, a MaxLengthAttribute attribute would be sought. And lastly, when the value being mutated is a string, the StringLengthAttribute will be used.

EnsureMinLength
An equivalent attribute for minimum length will not be made. Such an attribute would be too opinionated as it would need to accept a value/s that would be used fill any empty positions. Instead a IMutableObject interface or custom MutationAttribute is more suitable.

Make `new MutationContext<T>` injectable.

Since the MutationContext<T> is not injectible into my code because of the ctor, I cant Moq it or test it.

IE:

.AddSingleton<MutationContext<Customer>>()

Later:

public class CustomerRepository
{
    public MutationContext<Customer> customerMutationContext { get; set; }

    public CustomerRepository(MutationContext<Customer> customerMutationContext)
    {
        this.customerMutationContext = customerMutationContext;
    }

    public void SaveCustomer(Customer cust)
    {
        this.customerMutationContext.Init(cust).Mutate();
    }
}

What calls mutate?

This is a pretty cool library, thanks! What's not clear to me though is, what actually triggers the mutation since this isn't a normal ability of an Attribute.

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.