Created
June 7, 2024 12:13
-
-
Save 1saeedsalehi/bdb4a608fd30c4124ee25f66eac0b9cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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