March 2011 - Posts

NHibernate part 1

Here is a recap of what I’ve learned so far.

basically all the mapping can happened on the hbm files, I recommend NHibernate 3.0.Cookbook

How it works

on runtime  NHibernate will take each xml Mapping, deserialize into HbmMappingObject than add hbmMapping object to NHibernate configuration

if you want to generate hbmMapping by code than it will generate the hbm files, you can use ConfOrm.

if you want to set the Configuration,mapping on C# Code use FluentNHibernate

4 Steps to use it : Configuration,Mapping,Get Session through Factory, Query it

Mapping

component is used for defining property but it is coming from the same table,best for value object on DDD

if you have a class to choose and would like NHibernate to mapped it as string, kind of enum but it is a sub class use Discriminator.

There are one-to-many, many-to-many

if you want to define where but for the Joined property , NHibernate will generate alias based on level, example: where="accessory0_1_.ACS_EndDate is null"

SubClass has 3 types, that control how you want your Database tables to follow your classes

  • table per class hierarchy (there is no mapping for child class only value on a property of base class)
  • table per class (each get foreign key of base class and only what belongs to that child class is generated)
  • table per concrete (means each class has separate table with separate ID)

There  are a couple types of Collection in NHibernate, set,bag,map,ISet

 

  Bag Set List Map
Allow duplicates Yes no Yes Keys must be unique.Values may be duplicate
Order is significant No No Yes No
Type IList Iesi.Collections IList IDictionary

Model First

To use Model first approach, use the SchemaExport.

DDD

for DDD practice, I’ve implement UnitOfWork for NHibernate to Support unit of work pattern.

you could use like

   1: using (UnitOfWork.Start())
   2: {
   3:     //do something here
   4: }
   5:  
   6: //if i want transaction
   7: With.Transaction(() => { 
   8:              //do something here
   9: });

and we need to override Equals and GetHashcode for entity base,

since it takes different approach , as it could be proxy class or original class.

If on your value object class, you want to define ID for Database but don’t have a Property to mapped on that class, you could define so , only you have to Add Type property

   1: <id column="LTT_ID" type="Int32">
   2:     </id>

Query

When you want to query you have options

  • Linq to NHibernate
  • HQL
  • Session.Load<T>

load will return proxy class and always return class although you can't find it, so if you know you would only want to update it with out the overhead of loading it first.

this is the most recommended approach

  • Session.Get<T>

Get will return null if you can't find it

Wanna Execute StoreProc or View?

 

POID

NHibernate can generate for use the ID , before it get’s persisted. you can generate many type like using Hilo algorithm, GUID.

for my case i use native and set the unsaved-value=-1

 

Mapping Enum

If you want to use enum and get persisted in a Field as integer or string, you can mapped it from NHibernate mapping

Starting from NHibernate 2.2 you can use the type on property tag. NHibernate.Type.EnumStringType`1[[Namespace.EnumClass,Assembly]], NHibernate

Example:

   1: <property name="AcctType" not-null="true" type="NHibernate.Type.EnumStringType`1[[Eg.Core.AccountTypes,Eg.Core]], NHibernate"></property>

 

 

 

Basically every thing that is by default is lazy load, you want have the object directly, it will just a Proxy class, not the real class.

and NHibernate will query to db, only when you use it. for example you do for each on the Class Bound collection

Share this post: | | | |
Posted by cipto with no comments

Ie9 RTM and Iframe

after upgrading to ie9, some features does not run.

autocomplete does not run only on ie 9, after investigating it

I found out that appending Iframe directly is not working on IE9.

you have to create the element first than set it.

http://stackoverflow.com/questions/5344029/invalid-character-dom-exception-in-ie9

from css point of view, the measurement seems to be correct, not going to work correct without width and height

and if you’re out of luck , let’s set it to compability mode. until we understand what they have changed.

http://www.telerik.com/community/forums/aspnet-ajax/window/ie9-modal-dialog-issue.aspx.

do you think all the Hack, Wrapper, will end after IE 9 is released? think again

Share this post: | | | |
Posted by cipto with no comments

NHibernate Profiler

I’ve just make a test sample, and use join mapping, now i want to see my HQL . i know there is tag for output_sql to console but where it the visual one.

apparently there is profiler for nhibernate. read the manual how to setup and use it , it’s easy. but this is not free one. 30 days trial, fine.

now i can see my hql output, or perhaps just use sql profiler or another free option would set the log4net and read the textfile. the profiler is more than analysing hql, it can track your session,transaction, how many query statements, give alert

image

 

Share this post: | | | |
Posted by cipto with no comments

Domain Driven Design Part 2

Nice Link Value Object Explanation.

it takes lot of practice and asking around for DDD.

so I’m on 2nd and 3rd Step.

Define Aggregate,Aggregate Root and Services.

Aggregate is the principle to group the entities and value object which are 1 unit.

to easily follow this i read that , what ever that effected by a cascade down much like on the DB, is the candidates to be grouped into 1 Aggregate

define 1 aggregate root for each aggregate, external object can only reference other aggregate root.Explanation

Services can live in the Application and Domain.we call it Domain services and Application Services. as we now our presentation layer can call Either Application or Domain services.

what differs from both?

Application Layer is used for Workflow Layer.

anything that domain layer can’t handle, or it takes more than 1 service method to coordinate , anything that require transactional , send email.

we use application Services in application Layer.

Here is some answer about this confusion from the ddd mailing list:

“If you have experience with JEE you might think of application services as a stateless session facade. In an application service you want to have coarse-grained methods that take as many parameters as required to execute a single use case flow. You begin and commit/rollback transactions here. Any user authorization to execute this business method is also asserted here. But you don't execute any business logic here, nor does the application layer maintain any business state. These application services just coordinate tasks on the domain layer, so there isn't much code in the methods.the business logic should be on Domain Layer.”

“In my daily design and coding practice,service object in application layer used for coordinating transaction call to domain layer object or just a facade object for operation non single architectural resources.
For an example,in student registration use case,every success registration,student notified,let say by email or texts. In my ddd paradigm,there is no place on domain layer space to have an operation for calling or make a notification object,so code it on application layer service.
Service object of application layer can take place in application which need translation result of multiple client type as front end.
Service in domain layer used for encapsulate operation on domain which you can't naturally coding them on entity of your domain. Usually,this encapsulation hiding operation on more than one entity.”

 

another guidance of this pattern if the presentation layer is MVC, is SHarpArchitecture

after looking at wiki ,there are lot’s of ORM ,Framework and anything related to ddd especially in .Net world.

Code Fluent

Data Object

nREco

open mdx

Modeling Soft

Evolving Soft

naked object

ReMotion

 

Share this post: | | | |
Posted by cipto with no comments

Entity vs Value Object , how to differentiate it

Referring to Eric Evans Book

Entity:

There is a category of objects which seem to have an identity,
which remains the same throughout the states of the software.
For these objects it is not the attributes which matter, but a
thread of continuity and identity, which spans the life of a
system and can extend beyond it. Such objects are called Entities

Value Objects:

“An object that represents a descriptive aspect of the domain with no conceptual identity is called a VALUE OBJECT. VALUE OBJECTS are instantiated to represent elements of the design that we care about only for what they are, not who or which they are.”

 

If you read and digest this carefully and practice it. you will still have this concept of database centric mind set.

You might have define all the entity but still on making decision , whether it has identity or not you try to put in ID like you do on database Tables.

the way to have it right, is to stop thinking like Developer, and focus on User Domain. Good Explanation here

What attribute does this Entity can be unique, is it his name, or combination username and Date.

ValueObject is a subset or character that support Entity there for can’t stand by it’s own.

Value Object should be immutable, can’t change it’s property always initiate new instance.

That means , on my case:

(Retail Frame Lenses Company)

WebUser is Entity , the identity is UserID. Exporter ,Optician, Exporter Client, MineSite is just another type or inheritance from WebUser with it’s own characteristic there for Value Objects

Order is Entity, it’s a strong one and being passed to other entity/objects on domain.  identity: OrderID (generated one)

Frame,Lens,Accessory and Contract is Entity

and so on.

Now that I’ve complete this next is to determine the aggregate and aggregate Root.

than determine the Service, I’ll try not to make my object anaemic.

 

Share this post: | | | |
Posted by cipto with no comments

Domain Driven Design part 1

Basically this is a principle. that uses the common practice pattern and incorporate existing OOD,Gof,Solid,TDD etc.

the substance difference is we want to keep the Domain Clear and not Interfere by other . we want to keep Domain readability , and separate from other.

I find it’s a bit hard to understand at first.

To see it in action and a glimpse,

  • i read a chapter about this on Asp.net MVC 2 in action.

and after this to further have deeper insight,

I’ve got to read the Domain-Driven Design: Tackling Complexity in the Heart of Software by Eric Evans, kind a lost here what a abstract book, found a better one

  • Read Domain Driven Design Quickly , it’s only 106 pages. I’ve read all of it and really grasp the theoretical concept. but to make it correctly or at least follow the true separation on the real world?? See the Example Project  , and here
  • More to read
  • And to really give you guidance it’s the .Net domain Driven Design with C#  , Wrox. Currently I’m reading it.

DDD will shine on Complex domain project

About the Application Layer , What it should do? as i guess from the very start it’s almost a Facade. which will be used by Presentation Layer

“I would like to approach the application layer as an application programming interface (API), or almost
a fa ç ade, to the domain model. The reason I say almost a fa ç ade is because the application will be doing a
little bit more than just making it simple to use the domain model; it will also be coordinating actions
between different domain objects as well as maintaining the state of a particular task. The classes in this
layer will be composed of mostly static methods, thus making it easy for a class in the presentation
layer to do some work. An example is having a ProjectService class in the application layer that has
very simple methods to load and save a Project, such as Project.GetProject(1) and Project

.Save(project) .”

Active Record pattern is ideal to used when 1:1, and have few operation. on Active Record the Persistence is on the model.

while on DDD the Persistence is on Service Class.

Some People use UnitofWork when they use Hibernate,and EF to track changes.

while for Linq to sql it’s already a single unitofwork per instance of dataContext

“The DataContext conforms to the Unit of Work design pattern. Unless you are moving data between physical tiers between each operation you should keep your DataContext alive for the duration of work.”

 

 

Aggregate is an entity which can stand by it self

Value Object is an entity which won’t be exists if there is no aggregate

Aggregate Root is the root which define a boundary  between aggregate, all value object may not have direct reference to another Aggregate root

Repository, all persistence and load existing object must use repository. any need for direct link without going to aggregate should use repository

Factory, any creation of object should use factory method or if too complex use factory class

Service, any operation that involve more than 1 entity and operation does not belong to certain entity should use service

Specification, any constraint and validation should use specification

 

I’ve seen some sample that doesn’t use the Application Layer at all, what distinguish domain layer and application layer?

on some of discussion Link, it  is said by udi dahan that application layer is “workflow layer” or “process layer”

if your application in need a certain WorkFlow to interact and coordinate with other layers, than application layer is fit this.

http://dddsample.sourceforge.net/architecture.html

http://dddsample.sourceforge.net/characterization.html

Application is something anything that is high level for your application to run, session.

Create Immutable with readonly property on .net.

Domain Event :

The essence of a Domain Event is that you use it to capture things that can trigger a change to the state of the application you are developing. These event objects are then processed to cause changes to the system, and stored to provide an Audit Log.

 

Even though all the domain specific business rules should be encapsulated in the domain layer, some application designs put the rules in facade classes, which leads to domain classes becoming "anemic" in terms of business rules logic. This may be an acceptable solution in small size applications, but it is not recommended for mid-size to large enterprise applications that contain complex business rules. A better design option is to put the rules where they belong, inside the domain objects. If a business rule logic spans two or more Entity objects, then it should become part of a Service class.

Domain Element
State/Behavior

Entity, Value Object, Aggregate
State and Behavior

Data Transfer Object
State only

Service, Repository
Behavior only

 

How to Differentiate entity and object Value?

if found that it can be answered by this question:

If two instances of the same object have different attribute values, but same identity value, are they the same entity?

Share this post: | | | |
Posted by cipto with no comments