Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
danielplawgo / EnumExtensions1.cs
Created July 21, 2018 04:30
Jak zmierzyć wydajność kodu .NET? BenchmarkDotNet
public static string ToDisplayString(this Enum value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string description = value.ToString();
EnumDescriptionAttribute[] attributes = (EnumDescriptionAttribute[])value.GetType().GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
@danielplawgo
danielplawgo / Program1.cs
Created July 9, 2018 05:42
Używanie napisów w aplikacji
//użycie bezpośrednie napisu
var applicationName = ConfigurationManager.AppSettings["ApplicationName"];
//skorzystanie z stałej
var applicationName = ConfigurationManager.AppSettings[SettingsNames.ApplicationName];
public class EnumDescriptionAttribute : Attribute
{
public Type ResourceType { get; set; }
public EnumDescriptionAttribute(Type resourceType)
{
ResourceType = resourceType;
}
}
@danielplawgo
danielplawgo / FilterConfig.cs
Created July 8, 2018 05:37
Log using action filters
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new LogFilterAttribute());
}
}
@danielplawgo
danielplawgo / Program1.cs
Created July 1, 2018 06:56
Entity Framework - aktualizacja danych bez ich pobierania
using (DataContext db = new DataContext())
{
db.Database.Log = m => _logger.Info(m);
var product = db.Products.FirstOrDefault(p => p.Id == productId);
if (product == null)
{
return;
}
@danielplawgo
danielplawgo / BaseIntegrationTests.cs
Created June 27, 2018 04:35
Testowanie wysyłki email w ASP.NET MVC
public class BaseIntegrationTests
{
protected Lazy<IEmailServiceFactory> CreateEmailServiceFactory()
{
return new Lazy<IEmailServiceFactory>(() =>
new EmailServiceFactory(new Lazy<IHostingEnviromentService>(() => new TestHostingEnviromentService())));
}
}
@danielplawgo
danielplawgo / BaseMailer.cs
Created June 19, 2018 04:52
Hangfire - wysyłka email w tle
public class BaseMailer
{
protected void Send(Email email)
{
var mailerName = GetType().Name.Replace("Mailer", string.Empty);
var viewsPath = Path.GetFullPath(string.Format(HostingEnvironment.MapPath(@"~/Views/Emails/{0}"), mailerName));
var engines = new ViewEngineCollectionWithoutResolver();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data/elmah" />
<errorMail from="[email protected]" to="[email protected]" subject="Application Exception" async="false" smtpPort="25" smtpServer="***" userName="***" password="***" />
@danielplawgo
danielplawgo / IRuleBuilderExtensions.cs
Created June 14, 2018 12:35
Fluent Validation własny walidator
public static class IRuleBuilderExtensions
{
public static IRuleBuilderOptions<T, TProperty> Nip<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder)
{
return ruleBuilder.SetValidator(new NipValidator());
}
}
@danielplawgo
danielplawgo / Program.cs
Created June 14, 2018 11:10
Fluent Validation
static void Main(string[] args)
{
var user = new User()
{
CreateInvoice = true,
Nip = "123",
Email = "[email protected]",
Orders = new List<Order>()
{
new Order(){Product = "Laptop"},