samedi 3 juillet 2021

Design pattern to inherit derived members from non-modifiable base classes?

I have the following chicken/egg inheritance problem:

Here, base classes I can derive from, but they're on a framework, thus, I can't modify them:

class Editor
{

}

class ScriptedImporterEditor : Editor
{
}

Here, are classes in my project:

An editor with a preview, this works as expected, Cylinder and Torus have DrawPreview:

class EditorWithPreview : Editor
{
    public void DrawPreview(){}
}
   
class Cylinder : EditorWithPreview
{
    // DrawPreview is available
}

class Torus : EditorWithPreview
{
    // DrawPreview is available
}

But now I need a scripted importer editor that can also preview:

class ScriptedImporterEditorWithPreview : ScriptedImporterEditor
{
    // cannot inherit EditorWithPreview as it's not a ScriptedImporterEditor
}

class Cube : ScriptedImporterEditorWithPreview 
{
    // unable to use DrawPreview
}

class Sphere : ScriptedImporterEditorWithPreview 
{
    // unable to use DrawPreview
}

So basically,

  • I can't change neither Editor nor ScriptedImporterEditor as I don't own them
  • I therefore cannot import the logic of EditorWithPreview to ScriptedImporterEditor
  • Cube and Sphere can't inherit and use DrawPreview

Write a program which takes input as string and outputs the reverse of each word using a given pattern [closed]

Write a program which takes input as string and outputs the reverse of each word using a pattern as given in following examples. In pattern, each number represents the position of word in given string: [ Ideal Time : 45 mins - 1 hr ]

Example 1:

Input : A dead fox found in the forest

Output : tserof ni eht dnuof xof A daed

Patt : 7 5 6 4 3 1 2

Example 2:

Input : A dead fox found in the deep forest

Output : tserof eht peed in dnuof daed xof A

Patt : 8 6 7 5 4 2 3 1

vendredi 2 juillet 2021

What is the best design pattern for users groups [closed]

I'm new in my current company and they handled me an existed HR project that have multiple users group on it, I have a problem with maintain it because lets say we have a vacation request page . and we have three groups of users:

  1. employee
  2. supervisor
  3. manager

an employee create a request for vacation, his supervisor replay to it and also the manager.

the problem is the supervisor and the manger have the same view with different employees requests list depending on is the logged user is a (supervisor or manager), there are alot of IF statements in there and is hard to maintain or handling adding a new user group like a Department manger for example

I want to refactor the project to make it more expandable and maintainable without creating more pages for each group user, but I don't know which design pattern that can help me in the case

Please help me I'm having a hard time maintaining this project since there are 12 types of requests that employee could ask for.

Top usefull design patterns for C# web and desktop development

Please tell something from your development practice about design patterns. Especially in C#, not others languages. Which is the most useful, which isn't? If you can, show any examples please. Thanks.

( I will mark best answer as solution :) )

Deign pattern to correctly deduct credit balance in parallel HTTP requests

To keep it simple, let say user can purchase prepaid credits from my website. Then user can make HTTP requests to my API service. Each HTTP request will cost 1 credit. I also have transaction log the request (like ledger) in a table and calculate the new balance credit and update the value in another table.

My problem is when user have balance credit of 1 and starts making multiple HTTP requests (parallel) at same time, my authorization policy in my server will simply accept all their requests since they have 1 credit balance left over at the time of making that request. Is there any design pattern or any solution that i can implement to avoid this kind of race condition?

I have thought about some solutions such as implementing a queue system and database row locking. But this all seems convoluted and with little experience in this area i think many can go wrong.

Please suggest a solution, i also ready to fully rewrite if there is solid design pattern or solution that solve this issue.

Thanks in advance.

Unable to resolve service for type 'UnitOfWork' while attempting to activate MyController

I have Implemented a Generic Repository Pattern and UnitOfWork but I get an InvalidOperationException: Unable to resolve service for type 'UnitOfWork' while attempting to activate MyController

Here is my DBContext:

public class DBContext : DbContext
{
        public DBContext()
    {
       
    }
  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Nashrieh2");
    }
    public DbSet<User> Users { get; set; }
}

Here is User.cs class

public class User
{
    [Key]
    public string UserID { get; set; }
    public string Role { get; set; }
    public string UserName { get; set; }
    [Required(ErrorMessage = "Choose a password for your account")]
    public string Password { get; set; }
}

Here is my Repository Pattern GenericRepository.cs:

  public class GenericRepository<T> where T : class

{
    internal DBContext _context;

    public GenericRepository(DBContext context)
    {
        this._context = context;
    }
   public IEnumerable<T> GetList()
    {
      return  _context.Set<T>().ToList();
    }
    public async Task<IEnumerable<T>> ToListAsync()
    {
        return await _context.Set<T>().ToListAsync();
    }
    public virtual T Find(object id)
    {
        return _context.Set<T>().Find(id);
    }
  public virtual async Task AddAsync(T entity)
    {
         await _context.Set<T>().AddAsync(entity);
    }

Here is my unit of work:

public class UnitOfWork : IDisposable
{
    private DBContext _context;

    public UnitOfWork(DBContext context)
    {
        _context = context;
    }
    public GenericRepository<User> Users
    {
        get
        {

            if (this.user == null)
            {
                this.user = new GenericRepository<User>(_context);
            }
            return user;
        }
    }
    public async Task SaveChangesAsync()
    {
        await _context.SaveChangesAsync();
    }
    public void SaveChanges()
    {
        _context.SaveChanges();
    }
    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                _context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

Here is my controller:

  public class MyController : Controller
{
 private UnitOfWork _unitOfWork;
    public MyController(UnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
}

An Exception in occurred

InvalidOperationException: Unable to resolve service for type 'UnitOfWork' while attempting to activate MyController

HTML input pattern to match literally word in curly brackets like {name}

I want to make sure that a text input field includes somewhere in the string {name} literally, before submitting the form. For example it should match 123{name}456

I try the regex pattern {name}+ in regex101.com and other similar testers and it works as expected.

However, I learned that I should escape the brackets and after a lot of struggle I found how to match exactly {name} - with pattern = "\\{name\\}" But when I try to make it match when there are other characters around, it just doesn't work. I tried "\\{name\\}+" and "(\\{name\\})+" and don't get what's wrong.

Please help, it's been an hour already...