Skip to content

Instantly share code, notes, and snippets.

View TarekNajem04's full-sized avatar

Tarek Najem TarekNajem04

View GitHub Profile
public class LoginController : AppController<ILoginService>
{
public LoginController(ILoginService service, IWebHostEnvironment environment) : base(service, environment) { }
[AllowAnonymous]
[HttpPost("AuthenticateAsync")]
public async Task<IActionResult> AuthenticateAsync([FromBody] LoginModel model) =>
(
await Service
.AuthenticateAsync(model.UserName, model.Password)
public static class ServiceResultExtensions
{
// Probably one of the exceptions we designed, so we test the exception first.
public static ServiceResult<T> GetResult<T>(this T result) => result is IResultPatternProxy proxy
? CreateServiceResult<T>(proxy.Kind, result, proxy.WarningDescription, proxy.ExceptionDescriptions)
: ServiceResult<T>.Success(result);
private static ServiceResult<T> CreateServiceResult<T>(ResultKinds kind, object result, string warning, ExceptionDescriptions exceptionDescriptions) =>
Activator.CreateInstance(typeof(ServiceResult<>).MakeGenericType(typeof(T)), kind, result, warning, exceptionDescriptions) as ServiceResult<T>;
}
public class Startup
{
public Startup(IConfiguration configuration) => Configuration = configuration;
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddResultPattern(this IServiceCollection services)
{
services.AddSingleton<IObjectBuilder, SimpleDynamicObjectBuilder>();
//services.AddSingleton(typeof(IResultPatternAspect<>), typeof(ResultPatternAspect<>));
services.AddSingleton<IResultPatternAspecFactory, ResultPatternAspecFactory>();
return services;
}
private static void Main()
{
var dynamicObjectBuilder = new DynamicObjectBuilder("Domain_Assembly");
Console.WriteLine("***** Object With Base Class & Interface *****");
ObjectWithBaseClassAndInterfaceAndCustomProperty(dynamicObjectBuilder);
Console.ReadLine();
ObjectWithInterface(dynamicObjectBuilder);
Console.ReadLine();
public class User
{
public string Name { get; set; }
}
public interface IInterface
{
public string Property1 { get; set; }
}
public class BuilderPropertyInfo
{
public string Name { get; set; }
public Type Type { get; set; }
public bool IsInterfaceImplementation { get; set; }
}
public interface IObjectBuilder
{
object CreateObject(string name, BuilderPropertyInfo[] properties = null, Type baseClass = null, Type[] interfaces = null, bool autoGenerateInterfaceproperties = false);
public interface IResultPatternAspecFactory
{
TService Create<TService>(TService service);
}
public class ResultPatternAspecFactory : IResultPatternAspecFactory
{
public ResultPatternAspecFactory(IServiceProvider serviceProvider) => ServiceProvider = serviceProvider;
public IServiceProvider ServiceProvider { get; }
public interface ILoginService : IService
{
Task<LoginDto> AuthenticateAsync(string userName, string pw);
LoginDto Authenticate(string userName, string pw);
}
public class LoginService : Service, ILoginService
{
public LoginService(IMapper mapper) : base(mapper) { }
public static class ServicesResultExtensions
{
public static IActionResult ToActionResult<T>(this ServiceResult<T> result, AppController controller, [CallerMemberName] string callerName = "") =>
// C# 8.0
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression
// In the Front-End program, we test the value of the Kind property
// For the object we received via Response,
// and on the basis of it we build the result for the user.
result?.Kind switch
{