Monday, 15 July 2013

c# 4.0 - Add behavior to existing implementation - C# / Design Pattern -



c# 4.0 - Add behavior to existing implementation - C# / Design Pattern -

my current implementation service , business layer straight forwards below.

public class myentity { } // business layer public interface ibusiness { ilist<myentity> getentities(); } public class mybusinessone : ibusiness { public ilist<myentity> getentities() { homecoming new list<myentity>(); } } //factory public static class mill { public static t create<t>() t : class { homecoming new mybusinessone() t; // returns instance based on t } } //service layer public class myservice { public ilist<myentity> getentities() { homecoming factory.create<ibusiness>().getentities(); } }

we needed changes in current implementation. reason beingness info grew on time , service & client cannot handle volume of data. needed implement pagination current service. expect more features (like homecoming fault when info more threshold, apply filters etc), design needs updated.

following new proposal.

public interface ibusiness { ilist<myentity> getentities(); } public interface ibehavior { ienumerable<t> apply<t>(ienumerable<t> data); } public abstract class mybusiness { protected list<ibehavior> behaviors = new list<ibehavior>(); public void addbehavior(ibehavior behavior) { behaviors.add(behavior); } } public class paginationbehavior : ibehavior { public int pagesize = 10; public int pagenumber = 2; public ienumerable<t> apply<t>(ienumerable<t> data) { //apply behavior here homecoming info .skip(pagenumber * pagesize) .take(pagesize); } } public class myentity { } public class mybusinessone : mybusiness, ibusiness { public ilist<myentity> getentities() { ienumerable<myentity> result = new list<myentity>(); this.behaviors.foreach(rs => { result = rs.apply<myentity>(result); }); homecoming result.tolist(); } } public static class mill { public static t create<t>(list<ibehavior> behaviors) t : class { // returns instance based on t var instance = new mybusinessone(); behaviors.foreach(rs => instance.addbehavior(rs)); homecoming instance t; } } public class myservice { public ilist<myentity> getentities(int currentpage) { list<ibehavior> behaviors = new list<ibehavior>() { new paginationbehavior() { pagenumber = currentpage, } }; homecoming factory.create<ibusiness>(behaviors).getentities(); } }

experts please suggest me if implementation right or on killing it. if right design pattern - decorator or visitor.

also service returns json string. how can utilize behavior collections serialize selected properties rather entire entity. list of properties comes user request. (kind of column picker)

looks don't have plenty points comment on question. so, gonna create assumption not c# expert.

assumption 1: looks getting info first , applying pagination using behavior object. if so, wrong approach. lets there 500 records , showing 50 records per fetch. instead of fetching 50 records db, fetching 500 records 10 times , on top of adding costly filter. db improve equipped job c# or java.

i not consider pagination behavior respect service. behavior of presentation layer. service should worry 'data granularity'. looks 1 of client wants info in 1 go , others might want subset of data.

option 1: in dao layer, have 2 methods: 1 pagination , other regular fetch. based on incoming params decide method call.

option 2: create 2 methods @ service level. 1 little subset of info , other whole set of data. since said json, should restful service. based on incoming url, phone call right method. if utilize jersey, should easy.

in service, new behaviors can added exposing new methods or adding new params existing methods/functionalities (just create sure changes backward compatible). don't need decorator or visitor pattern. concern no existing user should affected.

c#-4.0 design-patterns

No comments:

Post a Comment