gacutil.exe -i -f C:\pathtodll\my.dll
Startup.cs example for .NET Core API w/ Swagger
using Swashbuckle.AspNetCore.Swagger;
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore().AddApiExplorer();
services.AddSwaggerGen();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
services.AddTransient<ISomeInterface, SomeDependency>();
}
// -----------------
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API");
});
}
app.UseMvc();
}
Resolve dependencies manually in .NET Core
var serviceProvider = services.BuildServiceProvider();
using (var scope = serviceProvider.CreateScope())
{
var app = serviceProvider.GetRequiredService<ISomeInterface>();
app.SomeMethod();
}
Display total lines of code in a git branch
git diff 5cec833a 4ef01d13 --stat
// where the hex are commit id's to differentiate.
Windows GAC location
C:\Windows\Microsoft.NET\assembly
Serialize enum as string
[JsonConverter(typeof(StringEnumConverter))]
Group list items by more than one property in Linq
// Group people by name and age
var groupedPeople = people.GroupBy(x => new
{
x.Name,
x.Age
});
Change git author name
git config --global user.name "Christopher Snay"
Save git credentials
git config --global credential.helper store
How to mock IConfiguration
var configuration = new Mock<IConfiguration>();
var configurationSection = new Mock<IConfigurationSection>();
configurationSection.Setup(a => a.Value).Returns("testvalue");
configuration.Setup(a => a.GetSection("TestValueKey")).Returns(configurationSection.Object);
Mock any implementation of a generic interface with one declaration.
services.AddTransient(typeof(IGenericInterface<>), typeof(GenericClass<>));
.NET C# Factory Pattern Using Reflection
// class implements IModelFactory -> IFactoryModel CreateInstance(string modelName)
private Dictionary<string, Type> _availableTypes;
public ModelFactory() {
LoadTypes();
}
public IFactoryModel CreateInstance(string modelName)
{
Type t = GetTypeToCreate(modelName);
//NOTE: handle null here
return Activator.CreateInstance(t) as IFactoryModel;
}
private void LoadTypes()
{
_availableTypes = new Dictionary<string, Type>();
Type[] assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();
assemblyTypes.Where(x => x.GetInterface(typeof(IFactoryModel).ToString()) != null).ToList()
.ForEach(y => _availableTypes.add(y.Name.ToLower(), y));
}
private IFactoryModel GetTypeToCreate(string name)
{
_availableTypes.TryGetValue(name, out Type t);
return t ?? null;
}
source:
https://app.pluralsight.com/library/courses/patterns-library/table-of-contents
Convert Python to Exe
#Install if needed
pip install pyinstaller
#Generate exe
C:/>pyinstaller -w -F myfile.py
Reset IIS
iisreset /noforce
list all globally installed npm modules
npm ls -g --depth 0
Dependency Injection With Multiple Implementations Of The Same Interface
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<AddOperationRepository>();
services.AddTransient<SubtractOperationRepository>();
services.AddTransient<MultiplyOperationRepository>();
services.AddTransient<DivideOperationRepository>();
services.AddTransient<Func<MathOperationType, IMathOperationRepository>>(serviceProvider => key =>
{
switch (key)
{
case MathOperationType.Add:
return serviceProvider.GetService<AddOperationRepository>();
case MathOperationType.Subtract:
return serviceProvider.GetService<SubtractOperationRepository>();
case MathOperationType.Multiply:
return serviceProvider.GetService<MultiplyOperationRepository>();
case MathOperationType.Divide:
return serviceProvider.GetService<DivideOperationRepository>();
default:
throw new KeyNotFoundException();
}
});
. . .
}
...
public class ValuesController : ControllerBase
{
private Func<MathOperationType, IMathOperationRepository> _mathRepositoryDelegate;
public ValuesController(Func<MathOperationType, IMathOperationRepository> mathRepositoryDelegate)
{
_mathRepositoryDelegate = mathRepositoryDelegate;
}
[HttpPost]
public ActionResult<OperationResult> Post([FromBody] OperationRequest opRequest)
{
IMathOperationRepository mathRepository = _mathRepositoryDelegate(opRequest.OperationType);
OperationResult opResult = mathRepository.PerformOperation(opRequest);
return new ObjectResult(opResult);
}
}
Install Specific Package Version
npm install [email protected]
Build Angular Project for Production w/ Relative Path
ng build --prod --base-href ./