eysidi
eysidi
Explore posts from servers
CC#
Created by eysidi on 2/28/2024 in #help
Dependency Inversion
Hello all, I had asked this before and was answered I need to implement dependency inversion I have Shape abstract class and and interface which is responsible for drawing the shape.
c#
public interface IShapeDrawer
{
void Draw(Vector2 position);
}
public abstract class Shape : IShapeDrawer
{
public abstract string Name; // Name to display

public abstract void Draw(Vector2 position); // Interface to abstract
}
c#
public interface IShapeDrawer
{
void Draw(Vector2 position);
}
public abstract class Shape : IShapeDrawer
{
public abstract string Name; // Name to display

public abstract void Draw(Vector2 position); // Interface to abstract
}
The problem is: each shape uses position to calculate coordinates, for circle it would be single Vector2 for square, rectangle etc it would be 2 and for triangle it would be 3. So I need an interface, right
c#
public interface ICoordinateCalculator
{
Vector2 CalculateCoordinate(Vector2 position);
}

public interface IMultipleCoodinateCalculator
{
Vector2[] CalculateCoordinates(Vector2 position);
}

public CircleCoordinateCalculator : ICoordinateCalculator
{
Vector2 CalculateCoordinate(Vector2 position) => position;
}
c#
public interface ICoordinateCalculator
{
Vector2 CalculateCoordinate(Vector2 position);
}

public interface IMultipleCoodinateCalculator
{
Vector2[] CalculateCoordinates(Vector2 position);
}

public CircleCoordinateCalculator : ICoordinateCalculator
{
Vector2 CalculateCoordinate(Vector2 position) => position;
}
Now my problem is, how do I implement this in my Shapeclass
40 replies
CC#
Created by eysidi on 8/28/2023 in #help
❔ Serializing related object EF
Hello all, I have 2 models
public class City {
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string CountryName { get; set; }
public Coordinates? Coordinates { get; set; }
}

public class Coordinates {
public int Id { get; set; }
public double X { get; set; }
public double Y { get; set; }
public int CityId { get; set; }
public City City { get; set; } = null!;
}
public class City {
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string CountryName { get; set; }
public Coordinates? Coordinates { get; set; }
}

public class Coordinates {
public int Id { get; set; }
public double X { get; set; }
public double Y { get; set; }
public int CityId { get; set; }
public City City { get; set; } = null!;
}
And I would like to include Coordinates of City in my response however I get error JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64 My query is
return db.Cities
.Where(c => c.Name.ToLower().Contains(city.ToLower()))
.Include(c => c.Coordinates)
.ToList();
return db.Cities
.Where(c => c.Name.ToLower().Contains(city.ToLower()))
.Include(c => c.Coordinates)
.ToList();
I have googled it and found different approaches but from the examples I saw, this should be working. Why does this happen?
47 replies