ChatGPT to be my code generator
As a developer, you're constantly writing code. Creating a new model class, implementing a repository, or configuring a database context are all essential steps in building a software application. However, these tasks can be time-consuming and repetitive, taking away from the time you could be spending on more critical tasks.
Fortunately, code generation offers a remedy. You may automate the writing of code by using code generators, which will save you time and lower the possibility of mistakes. And what's even more impressive is how accurately current language models like GPT-3 can generate code. GPT-4 is even better.
For instance, you have to construct a brand-new Author model class, a realted repository, and a database context. You could give the ChatGPT with some fundamental information about the entity (such the names of the properties and the data types), and the ChatGPT would handle the rest.
Using ChatGPT, a large language model based on GPT-3, we can generate code for the Author entity in just a few simple steps:
- Provide the essential details for the Author class, such as the property names and data types:
class Author
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
- Ask ChatGPT to generate the repository interface and Implementation for the Author class:
// Interface for the repository
interface IAuthorRepository
{
Task<Author> GetAuthorById(int id);
Task<IEnumerable<Author>> GetAllAuthors();
Task AddAuthor(Author author);
Task UpdateAuthor(Author author);
Task DeleteAuthor(Author author);
}
//Implementation for the repository
class AuthorRepository : IAuthorRepository
{
private readonly MyDbContext _dbContext;
public AuthorRepository(MyDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<Author> GetAuthorById(int id)
{
return await _dbContext.Authors.FindAsync(id);
}
public async Task<IEnumerable<Author>> GetAllAuthors()
{
return await _dbContext.Authors.ToListAsync();
}
public async Task AddAuthor(Author author)
{
await _dbContext.Authors.AddAsync(author);
await _dbContext.SaveChangesAsync();
}
public async Task UpdateAuthor(Author author)
{
_dbContext.Entry(author).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
}
public async Task DeleteAuthor(Author author)
{
_dbContext.Authors.Remove(author);
await _dbContext.SaveChangesAsync();
}
}
- Ask ChatGPT to generate the database context for the Author class:
<pre><code>class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
public DbSet<Author> Authors { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Author>()
.HasKey(a => a.Id);
modelBuilder.Entity<Author>()
.Property(a => a.Name)
.IsRequired();
modelBuilder.Entity<Author>()
.Property(a => a.Email)
.IsRequired();
modelBuilder.Entity<Author>()
.Property(a => a.Phone)
.IsRequired();
}
}
Additionally, ChatGPT can create code with astounding precision because it is based on a substantial language model. This lowers the possibility of errors in your application since the generated code is more likely to be accurate.
Overall, developers can benefit greatly from employing code generation tools like ChatGPT. Developers may concentrate their time and effort on more important duties, including planning and creating new features for their apps, by automating repetitive and time-consuming operations.
Microservices need to make it right
Last month, I went to Brisbane .Net User Group. One of the talks was about Microservice. The key point is we need to make the Microservice right, rather than pretending to have Microservice. I totally agree with that. Because the main key part of Microservice, in my understanding, is self-contained. The microservice won't need to require any external resources and the tasks will be carried out by a microservice is very small. Thus, that is very hard for architects.
HDMI and DVI cannot convert to the Display Port
I felt I am very silly. I got a work laptop, personal business PC and Mac Mini. Then my monitor got three sockets, two HDMI ports and a Display Port. I used two HDMI ports for my work laptop and personal business PC. So there is only a display port for my Mac Mini, but my Mac Mini got a HDMI port out. I do not want to waste it. At the end, I bought a HDMI to display port cable. I found that is not working, because the cable is only support only one direction, that is from Display Port Out (Computer) to the monitor with HDMI. We cannot do it another way. Then I found a forum post suggested to have a Display Port to DVI converter, then use the HDMI to DVI cable, to connect your computer to the adapter. Then I found it won't work too. The Display port to DVI adapter is one direction only too. That is to connect the display port out in the computer side. Then I finally admited I cannot convert HDMI to display port montior
String.Contains won't work in EF Core 3.0
In the past, when I used EF Core 1.1, I can use String.Contains() in where to preform string like search.
Such as:
_db.Customers.where(x=>x.FirstName.constains("michael"))
But we moved to EF Core 3.0. I found that is a problem, I got the exception regarding the LINQ translation. This sort of helper function in where is no longer support.
I need to re-write the query, like this:
_db.Customers.Where(x=>EF.Functions.Like(x.FirstName, "%michael%")
)
Local DB for a small application in C#
I got some old applications which is using spring.net and nhibernation with sqlite. By these names, you can know those frameworks are very old. That is the time to modernize them. I have research some local database solution. You can use System.data.sqlite, that is very light weight, but that is a plain sql without ORM. Or you can use MSSQLLocalDB with entity framework. But the SQL Express is still required. Finally, I found liteDB. This is a local NoSQL framework. You can add a single DLL into your project, not even simpler, just use Nuget to install it. The code syntax is similar with MongoDB.
Moreover, it has GUI tool LiteDB Studio, you can query the db file. Lastly, I checked the latest git commit is only 4 hours only. The project keeps to be active