Skip to content

Commit

Permalink
Update codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanjarogic committed Sep 7, 2022
1 parent efbe361 commit 922252b
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 641 deletions.
610 changes: 0 additions & 610 deletions .editorconfig

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public IDisposable Subscribe(ICustomObserver<IEvent> observer) =>
public IDisposable Subscribe<T>(ICustomObserver<T> newObserver)
where T : IEvent
{
if (!_typedObservers.TryGetValue(typeof(T), out List<IObserver<IEvent>> observers))
if (!_typedObservers.TryGetValue(typeof(T), out var observers))
{
observers = new List<IObserver<IEvent>>();
_typedObservers[typeof(T)] = observers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace OrderManagement.Infrastructure;

public class OrderManagementContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Customer> Customers => Set<Customer>();
public DbSet<Order> Orders => Set<Order>();

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder.UseSqlite("Data Source=storage.db");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public interface IServiceLocator
{
void AddService<T>(T service);
void AddService<T>(string serviceName, T service);
T GetService<T>();
object GetService<T>(string serviceName);
void AddService<T>(T service) where T : notnull;
void AddService<T>(string serviceName, T service) where T : notnull;
T GetService<T>() where T : notnull;
object GetService<T>(string serviceName) where T : notnull;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public ServiceLocator()
};
}

public void AddService<T>(T service) =>
public void AddService<T>(T service) where T : notnull =>
_services.Add(typeof(T).Name, service);

public void AddService<T>(string serviceName, T service) =>
public void AddService<T>(string serviceName, T service) where T : notnull =>
_services.Add(serviceName, service);

public T GetService<T>() =>
public T GetService<T>() where T : notnull =>
(T)_services[typeof(T).Name];

public object GetService<T>(string serviceName) =>
public object GetService<T>(string serviceName) where T : notnull =>
(T)_services[serviceName];

/*public void AddService(string serviceName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected Entity()

public static bool operator !=(Entity a, Entity b) => !(a == b);

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj is not Entity other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace UnitOfWorkLibrary.Example1.Infrastructure;

public class OrderManagementContext1 : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Customer> Customers => Set<Customer>();
public DbSet<Order> Orders => Set<Order>();

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder.UseSqlite("Data Source=storage1.db");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ public virtual IEnumerable<T> Get(Expression<Func<T, bool>> predicate) =>
.Where(predicate)
.ToList();

public virtual T GetById(int id) =>
_context.Find<T>(id);
public virtual T GetById(int id)
{
var entity = _context.Find<T>(id);
return entity ?? throw new ArgumentException($"Entity with {id} doesn't exists");
}

public virtual void Delete(T entity) =>
_context.Remove(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace UnitOfWorkLibrary.Example2.Infrastructure;

public class OrderManagementContext2 : DbContext, IUnitOfWork2
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Customer> Customers => Set<Customer>();
public DbSet<Order> Orders => Set<Order>();

public async Task<bool> SaveChangesAndDispatchDomainEventsAsync(CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ public int Altitude

public void Climb(int heightToClimb) => Altitude += heightToClimb;

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj.GetType() != GetType())
if (obj is not Aircraft incoming)
{
return false;
}

var incoming = (Aircraft)obj;
return CallSign.Equals(incoming.CallSign, StringComparison.Ordinal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class GoogleSubscriber
{
public GoogleSubscriber(StockTicker stockTicker)
{
stockTicker.StockChange += CheckFilterWhenStockChanged;
stockTicker.StockChange += CheckFilterWhenStockChanged!;
}

private void CheckFilterWhenStockChanged(object sender, StockChangeEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class MicrosoftSubscriber

public MicrosoftSubscriber(StockTicker stockTicker)
{
stockTicker.StockChange += CheckFilterWhenStockChanged;
stockTicker.StockChange += CheckFilterWhenStockChanged!;
}

private void CheckFilterWhenStockChanged(object sender, StockChangeEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Doneness is a gauge of how thoroughly cooked a cut of meat is based on
/// its color, juiciness, and internal temperature when cooked.
/// In this example we use celsisus degree as a unit of temperature measurment.
/// In this example we use celsisus degree as a unit of temperature measurement.
/// </summary>
public abstract class Doneness
{
Expand All @@ -15,14 +15,15 @@ public abstract class Doneness
protected int _temperatureChangeStep;

protected Doneness(Doneness state)
: this()
{
_steak = state.Steak;
_currentTemperature = state.CurrentTemperature;
_temperatureChangeStep = 1;
}

protected Doneness()
protected Doneness(Steak steak)
{
_steak = steak;
_temperatureChangeStep = 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ namespace StateLibrary.SteakCookingExample.States;
public class Uncooked : Doneness
{
public Uncooked(Steak steak, double currentTemperature)
: base(steak)
{
_steak = steak;
_currentTemperature = currentTemperature;

_lowerTemperature = 0;
_upperTemperature = 49;
_isSafeToEat = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ public static class SystemExtension
public static T DeepClone<T>(this T source)
{
var serialized = JsonConvert.SerializeObject(source);
return JsonConvert.DeserializeObject<T>(serialized);
var clonedObject = JsonConvert.DeserializeObject<T>(serialized);

if (clonedObject == null)
{
throw new JsonException("The source can't be serialized and deserialized successfully.");
}

return clonedObject;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public static void Execute()
string s3 = s1;

Console.WriteLine("Enter s4: ");
string s4 = Console.ReadLine();
string s4 = Console.ReadLine()!;

Console.WriteLine("Enter s5: ");
string s5 = string.Intern(Console.ReadLine());
string s5 = string.Intern(Console.ReadLine()!);

// In this case, s1 and s2 are two separate strings, and hence the output should be false.
// Surprisingly, the output is true! This is because .NET runtime in order to save space,
Expand Down

0 comments on commit 922252b

Please sign in to comment.