c# - How to make some base code for App class? -


for example, create wpf application , want add logic use @ each application - logging, unhandled exception handling, etc. use code (it doesn't matter - code something) in example wpf application:

public partial class app : application {     protected override void onstartup(startupeventargs e)     {         appdomain.currentdomain.unhandledexception              += new unhandledexceptioneventhandler(unhandledexceptionhandler);          serviceinjector.injectservices();          base.onstartup(e);          mainwindow window = new mainwindow();         window.show();     }      void unhandledexceptionhandler(object sender, unhandledexceptioneventargs args)     {         var exceptionobj = args.exceptionobject exception;         if (exceptionobj == null)             exceptionobj = new notsupportedexception(                 "unhandled exception doesn't derive system.exception: "                  + args.exceptionobject.tostring());          // todo: logging...          var messagebox = getservice<imessageboxservice>();         messagebox.show(             exceptionobj.message,              "unhandled exception.",             messageboxbutton.ok,             messageboximage.exclamation);          environment.exit(1);     }      #region resolve service dependencies members.      public tservicecontract getservice<tservicecontract>()         tservicecontract : class     {         return servicecontainer.instance.getservice<tservicecontract>();     }      #endregion resolve service dependencies members. } 

i want use code inside onstartup (unhandledexception subscribing, injecting services), unhandledexceptionhandler , getservice method in wpf applications without copy-pasting hands. best way?

i have read little base class app (derived application), seems has many drawbacks problems app.g.i.cs file (part of app class declaration), xaml support (need have app.xaml because fluent's components don't want work without adding reference through resourcedictionary inside app.xaml), etc.

if want re-use code across projects (and across project types: wpf, winforms, console, web, etc), might want creating separate class library project. these types of projects don't have user interface/application: instead compiled dll files, can reference in other .net projects see fit.

alternatively, within same solution can have multiple projects. it's not uncommon have number of projects dependencies set between them such 2 different application projects within same solution both use yet class library project shared code base.

see below:

enter image description here

this typical web application project template use. domain project @ bottom of dependency chain, consisting of business objects other projects reference. data project contains data access code other projects can access (except domain). web web application, , services console application server based utilities, etc. both share same domain , data (and few other) projects.

in case of above, in web project i'd set reference domainservices project, in web project:

using system; using template.domainservices; namespace template.web {     public class myclass {         ilogger logger = new domainservices.defaultlogger();         public void log(string msg) {             logger.log(msg);         }     } } 

we're using logger implementation domainservices shared project, calling web project, , same other project wanted (granted, hardly technically-correct in terms of code shown, idea?)

perhaps gives ideas on how can approached?

one complication deriving wpf base classes (or application platform specific classes) library classes tied particular application platform. in cases may desireable, if required functionality specific platform. in other cases, such loggers , such (and great many other things), can plain old c# objects (pocos) can consumed application platform. goal typically write platform-agnostic libraries except when there's specific need target or extend specific platform.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -