230 likes | 363 Vues
Explore the depths of Spring.NET in this detailed guide. Learn about the IApplicationContext, the core entry point for accessing application components and how it facilitates dependency injection through IObjectFactory. We cover essential concepts such as resource location, AOP for cross-cutting concerns like logging and security, and dynamic object creation. Dive into real-world applications with examples on data access using ADO.NET, Hibernate, and advanced transaction management. Equip yourself with the knowledge to effectively implement Spring.NET in your projects.
E N D
SpringFramework.Net Developer Session The Peer Frameworks Series - .Net and Java Chris Donnan
Desktop Windows Services 3rd Party Integration Web AOP Services Data Access Core SpringFramework.net 50,000 Ft View
Application Context “One Singleton to rule them all” • The IApplicationContext is the ‘normal’ top level entry point to use • SpringFramework.net • Inside you will find (along with other stuff) implementation of: • IObjectFactory • IResourceLocator • These are the interfaces that are most used in day to day programming • With Spring.
Spring ObjectFactory • The center of it all… Since the Application Context is an IObjectFactory – you can use it To act as a ‘dynamic factory’ for your objects. You simply ask the factory for your object – and you use it as you normally Would use any “new’d” object. The real beauty is that Spring manages assembling that object for you – Giving it whatever dependencies it needs.
Spring Resource Abstractions You can also use the IApplicationContext’s IResourceLocator interface: This allows you to get your resources from wherever they may be. This could Be at a URL, a File, an Embedded Resource, etc.
Spring.net Demo • DEMO. • Building a complex business object with many dependencies • Prototypes and singletons • Populating collections
Aspect Oriented Programming • Cross Cutting functionality • Logging • Transactions • Security • Other code that tend to repeat across an application
AOP Basic code – nothing special
AOP Code to invoke programmatically Output from execution
AOP XML Conifguration Same as programmatic usage – XML Configuration
Spring Web • Bidirectional data binding • Via attributes • Master Pages in .Net 1x • Dependency Injection for ASP.NET pages/ web services • Result Mapping • MVC Framework for ASP.Net
Data Binding public class UserRegistration : Spring.Web.UI.Page { [Binding("Text", "UserInfo.Email")] protected TextBox email; [Binding("Text", "UserInfo.Address.PostalCode")] protected TextBox postalCode; ……. } *Notice it uses Expression synatax
Result Mapping Used to externalize the mapping of action results to target pages: <object id="homePageResult" type="Spring.Web.Support.Result, Spring.Web"> <property name="TargetPage" value="~/Default.aspx"/> <property name="Mode" value="Transfer"/> <property name="Parameters"> <dictionary> <entry key="literal" value="My Text"/> <entry key="name" value="${UserInfo.FullName}"/> <entry key="host" value="${Request.UserHostName}"/> </dictionary> </property> </object>
Spring Data Access and Transactions • ADO.Net • Hibernate • IBatis.net • Basic patterns and utility classes/ base classes to speed up creating DAO and templated data access classes. • Transaction support • The goal is to reduce code needed to get rolling!
Hibernate Code Examples public class NHTestObjectDao : HibernateDaoSupport, ITestObjectDao { [Transaction()] public void Create(TestObject to) { HibernateTemplate.Save(to); } }
Hibernate Config Example <object id="DbProvider" type="Spring.Data.Support.SqlProvider, Spring.Data"> <property name="ConnectionString" value="Data Source=(local);Database=Spring;User ID=springqa;Password=springqa;Trusted_Connection=False"/> </object> <object id="SessionFactory" type="Spring.NHibernate.LocalSessionFactoryObject, Spring.NHibernate"> <property name="DbProvider" ref="DbProvider"/> <property name="MappingAssemblies"> <list> <value>Spring.NHibernate.Integration.Tests</value> </list> </property> <property name="HibernateProperties"> <dictionary> … </dictionary> </property> </object>
Spring Expressions Evaluation of text expressions against live objects. Sort of like XPath for objects. Simple Example: Inventor tesla = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian"); tesla.PlaceOfBirth.City = "Smiljan"; string evaluatedName = (string) ExpressionEvaluator.GetValue(tesla, "Name"); string evaluatedCity = (string) ExpressionEvaluator.GetValue(tesla, "PlaceOfBirth.City"));
Expressions Continued This can get more complex. You can evaluate objects sub parts, sub parts, etc. Inventor pupin = (Inventor) ExpressionEvaluator.GetValue(ieee, "Officers['president']"; string city = (string) ExpressionEvaluator.GetValue(ieee, "Officers['president'].PlaceOfBirth.City"); ExpressionEvaluator.SetValue(ieee, "Officers['advisors'][0].PlaceOfBirth.Country", "Croatia"); In the latest Spring.net release- there is support of literals, logic, properties, indexers, arrays, types, and lots more
SpringFramework.net DAF • Desktop Application Framework • Pub/ Sub like events • UI Application Container • Injection of dependencies into Controls/ Forms • Model/ View/ Controller | Presenter infrastructure • UI Services Model (ways to GET UI services in a consistent way) • Toolbars • Docking Managers • Status Bars - etc • Databind binding framework • Grid Binding • Expression based simple binding • Object Views (alternative to attributes) • Expression based validation
The Peer Frameworks Series - .Net and Java • 1) Spring Framework Developer Session - SpringFramework.net, SpringFramework.org2) Test Drive Development Developer Session - NUnit, JUnit; Rhino Mocks in .net and Easy Mock in Java3) Db4o Developer Session - Open Source Object Database in .net and Java4) ORM Developer Session - Hibernate, NHibernate / IBatis
Ruby does IoC Concrete Implementations