caching - How to Remove Duplicated Codes in This Design? -


I have 30 data access classes and some of them have a GetAll method with parameters.

All of these appear below my code:

  public IEnumerable & lt; IHierarchyDivisionDailyResult & gt; GetAll (Small Master ID, DateTime startup, Short GBIID) {var cacheKey = this.Cache.CreateCacheKey (this, Master ID, StartDate, GBID); Var result = this.Cache.GetList & lt; IHararchyDivisionDailyResult & gt; (Cache); If (result! = Null) return result; Lock (object) {result = it. Cache.getlight & lt; Iheirkki Division Daily Result & gt; (Cache); If (result! = Null) return result; (Var dataContext = OscaDataContext.CreateWithCustomTimeOut ()) using {result = datacentext. Hieraki DivisionDaily (MasterID, StartDate, GBID) .Oolist (); This.Cache.Add (cacheKey, results); }} Return result; }   

I want to refocus the code so that duplicated codes are removed. How would it be possible?

What are common / different in each implementation:

  • CreateCacheKey always receives all the input parameters
  • Cache.GetList
  • Locking is present from each side
  • DataContact object
  • Datacentex

    One possible option I thought was that the use of reflection would be able to merge most code, input parameter and output type, but the reflection approach The performance will not be good I have read.

    Would it be ideal, add a "cache" attribute to the method so that the result is automatically cached! [Cache] Public IEnumerable GetAll (Short Master ID, DateTime startup, Short GBIID) {(var dataContext = OscaDataContext .CreateWithCustomTimeOut ()) {Return Datacontex Hierarchy Division Daily (MasterID, StartDate, GBIID) .Oolist (); }}

    What do you think?

    Thank you,

    If I were you, I would make an abstract base class In which there is a normal GetAll method such as ..

      Public Intangible Class DataAccessBase & lt; T & gt; Where T: Vehicle Base {Public Virtual T GetAll (Small Master ID, DateTime StartDate, Short GBIID) {...}}   

    Then you can make its solid implementation ..

      Public class CarDataAccess: Data AccessBase & lt; Cars & gt; {Public Override Car Getaway (Small Master ID, Datetime Startup, Short GBIID) {...}} Public Squared WANDATA AWES: DatabasesBase & lt; Wan & gt; {Public overlays van gateell (short master id, datetime startup, short GBIID) {...}}   

    In the base class you can keep all the behaviors that are normal for everything Overwritten methods, you can put anything that is specific to any particular type.

    Instead of using cars and vans, this is an example that is more specific for your code.

      Public abstract class data basebase & lt; T & gt; {Public virtual IEnumerable & lt; T & gt; GetAll (Small Master ID, DateTime startup, Short GBIID) {var cacheKey = this.Cache.CreateCacheKey (this, Master ID, StartDate, GBID); & Lt; T & gt; Results = this.Cache.GetList & lt; T & gt; (Cache); If (result! = Null) return result; Lock (object) {result = it. Cache.getlist & lt; T & gt; (Cache); If (result! = Null) return result; (Var dataContext = OscaDataContext.CreateWithCustomTimeOut ()) using {result = this.GetResult (dataContext, masterId, startDate, gbuId); This.Cache.Add (cacheKey, results); }} Return result; } Protected summary list & lt; T & gt; GetResult (var Data Contestect, Short MasterID, Datetime Startup, Short Gbid); } Public Class Heer Racky Division Delirset Data Access: Databasesbase & lt; Ihyrkki Division Deliraselt & gt; {Public virtual IEnumerable & lt; IHierarchyDivisionDailyResult & gt; GetAll (Short Master ID, Datetime Startup, Short GBIID) {return base.GetAll (MasterID, StartDate, GBID); } Safe Override List & lt; IHierarchyDivisionDailyResult & gt; GetResult (various datacontacts, short master id, datetime startup, short GBIID) {Return Data Contex Herearaki Division Daily (MasterID, StartDate, GBID) .Oolist (); }}    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -