Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring.Net Steinar Dragsnes email: steinar.dragsnes at viz.no.

Similar presentations


Presentation on theme: "Spring.Net Steinar Dragsnes email: steinar.dragsnes at viz.no."— Presentation transcript:

1 Spring.Net Steinar Dragsnes email: steinar.dragsnes at viz.no

2 Agenda Dependency Injection (IoC) Spring.Net Services AOP Declarative Transaction Management through AOP Additional Spring.Net components  Spring.Web (out of scope)  Spring.Integration -NHibernate

3 Dependency Injection Problem:  How to put the various components and layers together without creating a too tightly coupled system?  How to allow for flexibility and technology changes (also plugging in modules/components)? IoC is not a new concept. DI is a reaction to the heavyweight complexity in the mainstream J2EE world (such as EJB framework). Similar patterns; Service Locator, Factory.

4 Dependency Injection vs. Service Locator Service Locator forces you to be dependent on the locator. With service locator the application class asks for a service by an explicit message to the locator.  PULL With injection there is no explicit request, the service appears in the application class.  PUSH Dependency Injection, less/no configuration of dependency in your source.

5 Sample Application Architecture Data Repository Domainobjects DAO implementations Service implementations Web interface Otherinterfaces PresentationLayer ServiceLayer Data Access Layer DAO interfaces Service interfaces

6 Defining objects in the containter Spring.Net uses XML to wire dependencies.  Programmatic (C#)  Annotations The syntax is quite simple and straigth farwards: Within the object tags you can specify constructor args and properties, just as you would do in a normal factory.

7 Example, UserService public class UserService : IUserService { private IUserDao dao; public UserService(IUserDao dao) { this.dao = dao; }... }

8 Defining the service in the IoC container...... public class UserService : IUserService { private IUserDao dao; public UserService(IUserDao dao) { this.dao = dao; }... }

9 Defining the service in the IoC container......

10 Container configuration <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> IApplicationContext context = ContextRegistry.GetContext(); IUserService service = (IUserService) context.GetObject(“userService");

11 DI and Application Wiring Through this very basic example you can get an idea on how you can configure and wire your application together. Tips:  Consider when you want to use DI and apply it where it gives you most ”bang for the buck”.  In the beginning, debugging the XML configuration can be difficult.  Whenever experiencing difficulties, do not hesitate to ask for help at the Spring.Net user forum [http://forum.springframework.net].

12 DI brings advantages Easy to switch implementations without the need for a recompile. Easy to facilitate testing, unit testing, integration testing. Better separation of concerns. Less dependencies within your application. Easier to write plugin modules. Encourages best practices such as TDD, decoupling, seperation of concerns, design by contract.

13 Spring.Net Services Portable Service Abstractions (PSA)  Write plain service classes (PONO)  Decide later how you will distribute your objects  Specify distribution technology through configuration..Net Remoting.Net Enterprise Services Web Services

14 Spring.Net Services How is this achieved?  Spring.Net creates a proxy at runtime that meets the implementation requirements of a specific distributed technology

15 Enterprise Services … Demo App ApplicationComponent Admin : Administrator role SpringApp.Com.UserService

16 Accessing the service from the client Administrative type registration is performed using the wellknown element in the client configuration section Seperate service interface and implementation in different assemblies Thus clients can obtain a proxy to a specific implementation with only a reference to the interface assembly. IApplicationContext context = ContextRegistry.GetContext(); IUserService service = (IUserService) context.GetObject(“userService");

17 Aspect Oriented Programming Problem, an operation that is done repeatedly across many classes and in the same class. Logging, security, transaction management to name a few. Extract the logic of these operations (aspects) into it’s own class. Apply this logic at various locations in your code (pointcut). Terminology  ‘what’ = advice, ‘where’ = pointcut

18 Aspect Oriented Programming Spring.Net provides an aspect library:  Logging  ExceptionHandling  Caching  Transaction  Parameter Validation Easy to create own aspects.  Create a class that implements one of the interceptor contracts (e.g. IMethodInterceptor)  Create a custome attribute  Either connect attribute and advice programmatic or preferably in the Spring.Net container.

19 <object id="transactionAdvisor" type="Spring.Transaction.Interceptor.TransactionAttributeSourceAdvisor, Spring.Data"> Configuring Aspects

20 <entry key="Viz.Core.Aop.Attributes.Log4NetLoggingAroundAttribute,Viz.Core” value="Viz.Core.Aop.Advice.Log4NetLoggingAroundAdvice,Viz.Core"/> <entry key="Viz.Core.Security.Attributes.AccessPriviligesAttribute,Viz.Core" value="Viz.Core.Security.Advices.AccessPriviligesAdvice,Viz.Core"/> <entry key="Viz.Core.Persistence.nHibernate.Session.Attributes.SessionScopeAttribute,Viz.Core.Persistence" value="Viz.Core.Persistence.nHibernate.Session.Advices.SessionScopeAdvice,Viz.Core.Persistence"/>

21 How this turns out in code [Transaction(ReadOnly = true)] public IList LoadAll(Type entityType) { return dao.LoadAll(entityType); } [Logging] [Permission(Permissions = Permission.ContentManager, Permission.PortfolioManager) [Transaction] public object SaveOrUpdate(object entity) { return dao.SaveOrUpdate(entity); }

22 Spring.Net Data Access API Provides a consistent programming model across different transaction APIs such as ADO.NET, Enterprise Services, System.Transactions, and NHibernate. Declarative transaction management with any of the above data access technologies (AOP) You can keep the same API across different data access technologies. Changing the underlying transaction implementation is a matter of configuration or a centralized programmatic change as compared to a major overhauling.

23 Spring.Net NHibernate Integration Spring.Net provides integration with NHibernate in terms of  Resource management DbProvider SessionFactory Sessions, SessionScope, OSIV  DAO implementation support  Transaction strategies You can use Spring's support for NHibernate without using the full Sprting.Net stack or Spring’s transaction management


Download ppt "Spring.Net Steinar Dragsnes email: steinar.dragsnes at viz.no."

Similar presentations


Ads by Google