Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Thinking DDD with Patterns - Part 2 -

In previous post, I described the architecture diagram of our project. In this post I will describe the architecture layers. As shown in the architecture diagram, our layers are based on the approach DDD (UI, Application, Domain and Infrastructure).

  1. User Interface (Presentation Layer): Responsible for presenting information to the user and interpreting user commands. As shown in the diagram above, this layer can be represented by one of the following interfaces:
    • Silverlight or WPF: In this part, you should use the pattern MVVM (Model-View-ViewModel). MVVM offers a flexible way to create applications Silverlight / WPF and allow to reusing of code, simplifying maintenance and testing. It is composed of :
      • View: XAML for WPF or silverlight screens
      • ViewModel: relation between the view and model
      • Model: represents the entities and the business code on the server side.

     

    • ASP.NET MVC: As its name implies, the pattern used here is the MVC (Model-View-Controller).
      • Controller: Allows you to manage user interaction, working with the model and, finally, select the view that will allow rendering the user interface.
      • View: display made ​​in ASP engine or Razor
      • Model: represents the entities and the business code on the server side.

     

    • ASP.NET Webform: It doesn't follow any pattern. It is the strong coupling between the view and the code behind. Additional work is needed from the entire team to reduce the maximum possible code behind.


       

  2. Application layer: This is a thin layer which coordinates the application activity. It doesn't contain business logic. It doesn't hold the state of the business objects, but it can hold the state of an application task progress..
  3. Domain layer: This layer contains the information about the domain. This is the heart of business software. The state of business objects is held here. Persistence of business objects and possibly their state is delegated to the infrastructure layer.
  4. Infrastructure Layer: This layer acts as a supporting library for all the other layers. It provides communication between layers, implements the persistence of business objects contains supporting libraries for UI layer.

Other than main layers, I added two needed libraries:

  • WCF service: exposes the necessary contracts for the UI layer.
  • UTILS: It is a utility containing common code for any generic solution.

In next post, I will describe components used and how to create our project with VS 2010…

How to Dependency Injection with NHibernate and Autofac in domain layer ?

There are two ways to DI with nhibernate and Autofac in domain layer (Entities) :
1. You can use directly autofacContainerProvider.Resolve().
Pros:
- simple and quickly
Cons :
- strong coupling between IoC and Entities
- cannot create unit tests for domain layer
- don't support with different UI (Wcf, Web, ..)
2. To resolve these cons, we have implemented this solution :
In Global.asax.cs, in Application_Start add this code after builder.Build() :


var builder = new ContainerBuilder();
builder.RegisterType<SomeDependency>();
// ... continue registering dependencies...
// Once you're done registering things, set the container
// provider up with your registrations.
builder.RegisterAssemblyTypes(typeof (ProductEntity).Assembly).InstancePerDependency();
_containerProvider = new ContainerProvider(builder.Build());


Web Application :

Nhibernate.Cfg.Environment.BytecodeProvider
= new AutofacBytecodeProvider
(new ContainerProviderContainer(AutofacContainerProvider)
, new ProxyFactoryFactory()
, new DefaultCollectionTypeFactory());

WCF Application :
Nhibernate.Cfg.Environment.BytecodeProvider
= new AutofacBytecodeProvider(AutofacContainer
, new ProxyFactoryFactory()
, new DefaultCollectionTypeFactory());

In nhibernate.cfg.xml you should use this factory :
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory
, NHibernate.ByteCode.Castle</property>

and in Entities of domain layer, you should declare the repository interfaces in constructors.