Project with several features for C# projects
Truncate a table
var context = new YourContext();
context.Truncate<YourTable>();
Insert batch data
var context = new YourContext();
context.BulkInsert(yourList);
// OR with options
var options = new DbContextOptions
{
CommitCount = 1000, // Record amount saved per batch: default 10000
AutoDetectChangesEnabled = false,
ValidateOnSaveEnabled = false
};
var context = new YourContext();
context.BulkInsert(yourList, options);
Delete all records in a table
var context = new YourContext();
context.DeleteAll<YourTable>();
Globally set varchar mapping over nvarchar
public class YourContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add<MakeAllStringsNonUnicode>();
base.OnModelCreating(modelBuilder);
}
}
Dynamically register all type configurations
public class YourContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.RegisterAllTypeConfigurations(Assembly.GetExecutingAssembly());
base.OnModelCreating(modelBuilder);
}
}
- HasAttribute<>
- Attributes<>
- Attribute<>
- ToBoolean
- ToDateTime
- ToDateTimeOrNull
- GetFirstDayOfMonth
- GetLastDayOfMonth
- ToShortDateString
- ToDecimal
- AddRangeOverride
- AddRangeNewOnly
- AddRange
- GetValueOrDefault
- Except
- AddRange
- RemoveRange
- Merge
- GetValue
- Attribute<>
- DisplayName
- SaveToFile (Save byte[] into a file)
- ToInt
- Clamp
- ToByteArray
- Truncate
- ReplaceFirstOccurance (Replace first occurrence of a string)
- ReplaceLastOccurance (Replace last occurrence of a string)
- ToLowerFirstLetter (First letter in lowercase)
- ToUpperFirstLetter (First letter in uppercase)
- RemoveAccents (Remove diacritics (accents) from a string)
- Nl2Br (A string extension method that newline 2 line break.)
- GetClassProperties
- IsClassValueType
- IsAssignableTo<>
Paged component
public ActionResult Index(QueryFilter filter)
{
var query = _context.Animals;
if (!string.IsNullOrWhiteSpace(filter.Search))
query = query.Where(q =>
q.Name.Contains(filter.Search)
);
var pagedQuery = query.Paginate(filter);
pagedQuery.AddSorterField(q => q.Id);
pagedQuery.AddSorterField(q => q.Name);
pagedQuery.AddSorterField(q => q.Age);
return View(pagedQuery);
}
Checks if an attribute is defined on an action or controller
[Authorize]
public abstract class BaseController : Controller
{
private bool IsRequestAuthorized(AuthorizationContext filterContext)
{
if (IsUserAuthenticated)
return true;
var descriptor = filterContext.ActionDescriptor;
var authorize = descriptor.IsDefinedInActionOrController<AuthorizeAttribute>();
var allowAnonymous = descriptor.IsDefinedInActionOrController<AllowAnonymousAttribute>();
return !authorize || allowAnonymous;
}
HtmlHelper to convert an object to JSON
@Html.ToJson(Model)
This extension method appends the assembly version to end of path
<script src="@Url.ContentVersioned("~/js/service.js")"></script>
<script src="@Url.ContentVersioned("~/js/module.js")"></script>
Which will be rendered like this:
<script src="/js/service.js?v=1.0.0.0"></script>
<script src="/js/module.js?v=1.0.0.0"></script>
public class UserMustBeAdmin : ISpecification
{
private readonly User _user;
public UserMustBeAdmin(User user) => _user = user;
public bool IsSatisfiedBy() => _user.IsAdmin;
public string ErrorMessage() => "You are not allowed to access this area";
}
public class ProposalMustBelongToTheUser : ISpecification
{
private readonly User _user;
private readonly Proposal _proposal;
public ProposalMustBelongToTheUser(User user, Proposal proposal)
{
_user = user;
_proposal = proposal;
}
public bool IsSatisfiedBy() => _proposal.IdUser = _user.Id;
public string ErrorMessage() => "This proposal does not belong to you";
}
public sealed class UserCanAccessProposal : Validator
{
public UserCanAccessProposal(User user, Proposal proposal)
{
Add(new UserMustBeAdmin(user));
Add(new ProposalMustBelongToTheUser(user, proposal));
}
}
// ...
public ActionResult Index(int id)
{
// ...
var result = new UserCanAccessProposal(user, proposal).Validate();
if (!result.IsValid)
throw new Exception(result.Message);
// ...
}