In a typical business application we have a set of session-bound classes, for example our business classes, services, etc... Each method, property or constructor in these classes can be either transactional or not transactional. We should decide which of them will be transactional and inform ORM about this, using a set of attribute-based rules. I'd like these attributes to allow me to specify following rules:
- All members of this class are not transactional by default
- All public methods of this class and all its inheritors are transactional by default
- This method requires new nested transaction even if some transaction is already open
- This member doesn't require transaction
- This method requires transaction with "ReadCommited" isolation level
When I starting to implement my model and business logic layer, I decide which policy to use. I can make all public members in all classes of my model transactional by marking base class as "Transactional", and then mark some members as not transactional by appropriate attribute. On other hand I can make everything not transactional, except public methods in some service classes, and open transactions manually each time I need them. I can use third approach, for example make methods transactional, but properties not. It depends on my solution architecture, and it's impossible to find universal right way.
So I think we need something like this:
[Transactional] attribute that can be applied to a session-bound class or its member. Attribute should have following options:
- Should this attribute be applied to inheritors or not
- Which members it should be applied to (methods, properties, constructors)
- Visibility of members this attribute should be applied to (public, internal, protected)
- Are members this attribute applied to transactional or not. Another alternative is to mark not transactional members by special [NotTransactional] attribute
- Required isolation level for transaction
- Whether new transaction is required or any existing one is enough.
- [NotTransactional(InheritBehavior = true)]
- public class MyEntityBase : Entity
- {
- }
- public class Document : Entity
- {
- [Transactional]
- public void BusinessMethod()
- {
- }
- [Transactional(Options = TransactionOptions.New,
- IsolationLevel = IsolationLevel.Serializable)]
- public void AnotherBusinessMethod()
- {
- }
- }
- [Transactional(
- MembersType = MemberTypes.Method,
- MembersVisibility = Visibility.Public)]
- public class DocumentService : SessionBound
- {
- public Document FindLastDocument()
- {
- }
- public void RemoveAllDocuments()
- {
- }
- [NotTransactional]
- public override string ToString()
- {
- }
- }
As you can see MyEntityBase class marked by [NotTransactional] attribute, but if we assume that all classes are not transactional by default we must not mark it by this attribute.