Using default constructor with unit-of-work

22 Views Asked by At

I am developing a project with unit-of-work and what I want to ask is whether the code is in the right place or not. Can you help me with this?

Question: is it correct to use the context class in this way?

public class SqlServerContext : DbContext
{
    public SqlServerContext(DbContextOptions<SqlServerContext> options):base(options)
    {
    }

    public SqlServerContext()
    {
    }

     public DbSet<Component> Components { get; set; }
     public DbSet<Order> Orders { get; set; }
}

Why do I need to use like this (why do I need an empty constructor):

public List<OrderDetailsDto> GetAllOrderDetails()
{
    using (SqlServerContext context = new SqlServerContext())
    {
        var result = from o in context.Orders
                     join c in context.Components
                          on o.ComponentId equals c.ComponentId
                     select new OrderDetailsDto
                            {
                                OrderId = o.OrderId,
                                OrderCount = o.Count,
                                OrderDate = o.OrderDate,
                                OrderUrgent = o.Urgent,

                                ComponentId = c.ComponentId,
                                ComponentCode = c.Code,
                                ComponentDescription = c.Description,
                            };

        return result.ToList();
    }
}

I have Business, DataAccess, Entity and UI layers. I reach the DataAccess layer with the Service Manager logic I created in the Business layer, not directly through DataAccess.

And at some point I need to write a linq query. I have a query as above. Is it a correct method to use this query in this way? I can not be sure.

0

There are 0 best solutions below