c# - Isolate class to be instantiated only by a specific class: best practices -
c# - Isolate class to be instantiated only by a specific class: best practices -
ok, working 2 layers of class: somekind of "manager" class task take object, link items attached it, , homecoming caller, , "data" class task phone call database on specific request.
here's illustration of how work. here's "manager" class:
public class cardmanager { private static readonly carddata mcarddal = new carddata(); public list<carddisplay> listcardstoshow(int _pagenumber, string _querystring, string _rarity, string _type, string _color, out int _totalcount) { list<carddisplay> listtoreturn = mcarddal.listcardstoshow(_pagenumber, _querystring, _rarity, _type, _color, out _totalcount); linklistcarddisplaydata(listtoreturn); homecoming listtoreturn; } /// <summary> /// method links card set each cards of list. /// </summary> /// <param name="_listtoreturn"></param> private static void linklistcarddisplaydata(ienumerable<carddisplay> _listtoreturn) { seek { foreach (carddisplay item in _listtoreturn) { linkcarddisplaydata(item); } } grab (exception ex) { throw new exception(ex.message); } } private static void linkcarddisplaydata(carddisplay _item) { _item.mmastercardid = _item.mmastercard.mcardid; imagesmanager.getcardimages(_item); if (_item.mchildcard != null) { _item.mchildcardid = _item.mchildcard.mcardid; } } }
and here's "data" class, namely carddata
class in occurrence:
public class carddata { internal list<carddisplay> listcardstoshow(int _pagenumber, string _querystring, string _rarity, string _type, string _color, out int _totalcount) { using (databaseentity db = new databaseentity()) { db.database.connection.open(); list<card> carddata; list<cardinfo> listcards; if (!string.isnullorwhitespace(_querystring)) { var predicate = getcardpredicate(_querystring); if (_rarity != "all") { predicate = predicate.and(_item => _item.card_rarty == _rarity); } if (_color != "all") { predicate = predicate.and( _item => _item.card_mana_cost.contains(_color) || _item.card_color.contains(_color)); } if (_type != "all") { predicate = predicate.and(_item => _item.card_type.contains(_type)); } var cardqry = c in db.card.asexpandable().where(predicate) select c; _totalcount = cardqry.count(); int pagecount = _pagenumber - 1; carddata = cardqry.orderby(_x => _x.card_ide).skip(pagecount * 20).take(20).tolist(); (int = 0; < carddata.count; i++) { card card = carddata[i]; if (carddata.any(_item => _item.card_master_ide == card.card_ide)) { carddata.remove(card); } } listcards = dataconverter.listcarddatatolistcardinfo(carddata); } else { // if here user browsed 300 latest entries available. expression<func<card, bool>> cardpredicate = predicatebuilder.true<card>(); if (_rarity != "all") { cardpredicate = cardpredicate.and(_item => _item.card_rarty == _rarity); } if (_type != "all") { cardpredicate = cardpredicate.and(_item => _item.card_type.contains(_type)); } if (_color != "all") { cardpredicate = cardpredicate.and( _item => _item.card_mana_cost.contains(_color) || _item.card_color.contains(_color)); } var cardqry = (from c in db.card.asexpandable().where(_item => !_item.card_name.contains("(foil)")) select c).orderbydescending(_x => _x.card_set.card_set_reles_date).take(300); cardqry = cardqry.where(cardpredicate); _totalcount = cardqry.count(); int pagecount = _pagenumber - 1; carddata = cardqry.skip(pagecount * 20).take(20).tolist(); (int = 0; < carddata.count; i++) { card card = carddata[i]; if (carddata.any(_item => _item.card_master_ide == card.card_ide)) { carddata.remove(card); } } listcards = dataconverter.listcarddatatolistcardinfo(carddata); } list<carddisplay> listtoreturn = makelistcarddisplay(listcards); homecoming listtoreturn; } } }
my question not on "how" code (but sense free create constructive comment love learn), rather on how structured. i'd like, example, carddata
class exclusively instantiated "manager" class, meaning not, example, create public static readonly carddata mcarddal = new carddata();
object in controller.
is there way isolate class in such manner "forced" pass through manager object? , working in way, code good?
it possible deny access outside making carddata
nested class within manager
, block it's constructor making protected
, , create private
instance class:
public class manager { public class manager { carddata c = new carddatainternal(); } private class carddatainternal : carddata { public carddatainternal() { } } public class carddata { protected carddata() { } } }
c# optimization
Comments
Post a Comment