Custom Queries and Executing stored procedure #258
Replies: 3 comments 6 replies
-
I think you need to call the |
Beta Was this translation helpful? Give feedback.
-
An example of getting data with stored procedure and entity framework. Program.cs using Microsoft.EntityFrameworkCore;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddTransient<IStudentService, StudentService>();
builder.Services.AddTransient<IStudentBroker, StudentBroker>();
string conString = builder.Configuration.GetConnectionString("StudentDbConStr") ??
throw new InvalidOperationException("Invalid con str");
builder.Services.AddSqlServer<StudentContext>(conString);
WebApplication app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/weatherforecast", (IStudentService service) =>
{
IEnumerable<Student> students = service.GetStudents();
return students;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
app.Run(); Model public class Student
{
public int Id { get; set; }
public string Name { get; set; }
} Service layer public interface IStudentService
{
public IEnumerable<Student> GetStudents();
}
public class StudentService : IStudentService
{
private readonly IStudentBroker broker;
public StudentService(IStudentBroker broker)
{
this.broker = broker
}
public IEnumerable<Student> GetStudents()
{
return this.broker.GetStudents();
}
} Broker layer public interface IStudentBroker
{
public IEnumerable<Student> GetStudents();
}
public class StudentBroker : IStudentBroker
{
private readonly StudentContext context;
public StudentBroker(StudentContext context)
{
this.context = context
}
public IEnumerable<Student> GetStudents()
{
IEnumerable<Student> students = this.context.Database.SqlQueryRaw<Student>("GetStudents");
return students;
}
}
public class StudentContext : DbContext
{
public DbSet<Student> Students { get; set; }
public StudentContext(DbContextOptions<StudentContext> options) : base(options)
{
}
} The stored procedure: CREATE PROCEDURE GetStudents
AS
BEGIN
SET NOCOUNT ON;
SELECT Id, Name
FROM Students
ORDER BY Name;
END You need to put the connection string of the database in the You need to install Microsoft.EntityFrameworkCore.SqlServer NuGet package. |
Beta Was this translation helpful? Give feedback.
-
Let's say you have a You have two options to achieve this. In the first option, you can create a custom entity which have a combination of properties from In the second option, you can get the You need to measure the performance between these two architectures. If the stored procedures architecture is not giving you a big performance advantage, then you can go with the second architecture because that is more readable and testable. |
Beta Was this translation helpful? Give feedback.
-
Hi @hassanhabib
As I read The Standard In read about Services
-> foundation services (Basic Purpose is to validate and abstract)
-> Processing service (Processing about entity is done here)
and so on.
These Services and Brokers are purely related to Entity, but in many application to get the better performance developer create stored procedures where multiple entities are involved and we fetch the data which is not for any specific entity.
Where we should handle these scenarios. Do we need to create sperate brokers and services for it or is there any other way suggested by The Standard community.
Thanks to this community who gives newbies a chance to learn from most competent Engineers of current era.
Beta Was this translation helpful? Give feedback.
All reactions