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.
Cable tv is out dated
When I was in Hong Kong, our home got cable tv. Recently, Hong Kong cable stopped its cable tv service. In the past, cable tv offered a lot of choices for tv channels, such as BBC, CNN, and discovery channel. Nowadays, we got YouTube and Netflix.
I got a lot of things to watch. Mainly, my friends got a paid tv subscription because of their sports channel. I think paid tv service started to fade out. Even I did not watch local tv channels for years. Of course, I still watch local tv, but I watch them via their app. I can choose to watch the program when I have time.
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.
I do not need a powerful desktop anymore
I think I do not need a very, very powerful desktop. Because I found I can put some services on the cloud. I am a software developer. I am doing some A.I. training now. I can use Google Colab. I do not need to install anything on my desktop. That is very handy.
Back up, Back Up, Back Up!
Recently, I have received the help requests from a number of friends regarding the data recovery. Some of them are wipe all of phone data by accident. Some of them lost some of data due with the data store failures. There is no easy solution to recover the data. Thus, please back up your data. My photo will back up automatically to my NAS via an app. Moreover, all of my important will be sync with my NAS too. I got some super important even having a cloud back up. In addition, I won't be just use the NAS as the backup storage. I still got some external hard disks to backup nas too. I got at least two back up copy of my data, and my NAS is running with two hard disks in the mirror mode. So, Back Up, Back Up, Back Up