The problem is really simple: I have a "Stock" class, I want to load its "StockName", "StockCode" property from db.
which patten should i use?
pattern 1) Use the service class to create it
public interface IStockService{ Stock GetStock(string stockCode); void SaveStock(Stock stock); } public class StockService : IStockService{ } IStockService stockService = new StockService(); Stock stock = stockService.GetStock();
pattern 2) Use the static method in stock
public class Stock{ public static Stock GetStock(){ Stock stock = new Stock;
pattern 3) Use constructor to load
public class Stock{ public Stock(){
for template 1: it seems that it uses so much code to create a stock object, and the "SaveStock" method looks a bit non-object oriented.
for template 2: the "Save" method looks fine, but the GetStock method is a static method, it seems that the Utility class always uses a static method.
for template 3: the constructor will load data from db during initialization. it is also embarrassing.
design initialization c # class
yi.
source share