Skip to content

Instantly share code, notes, and snippets.

@1saeedsalehi
Created June 7, 2024 12:13
Show Gist options
  • Save 1saeedsalehi/bdb4a608fd30c4124ee25f66eac0b9cb to your computer and use it in GitHub Desktop.
Save 1saeedsalehi/bdb4a608fd30c4124ee25f66eac0b9cb to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
public class ExampleService
{
private readonly IRepository _repository;
public ExampleService(IRepository repository)
{
_repository = repository;
}
public List<string> GetItems()
{
return _repository.GetAllItems();
}
public string GetItemById(int id)
{
var item = _repository.GetItem(id);
if (item == null)
{
throw new ArgumentException("Item not found");
}
return item;
}
public void AddItem(string item)
{
if (string.IsNullOrWhiteSpace(item))
{
throw new ArgumentException("Item cannot be null or empty");
}
_repository.AddItem(item);
}
public void DeleteItem(int id)
{
var item = _repository.GetItem(id);
if (item == null)
{
throw new ArgumentException("Item not found");
}
_repository.DeleteItem(id);
}
}
public interface IRepository
{
List<string> GetAllItems();
string GetItem(int id);
void AddItem(string item);
void DeleteItem(int id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment