ILogger injection #3443
-
Hello all, Is it possible to inject dependencies into business objects such as the Thank you for your help. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 9 replies
-
Yes, you can have anything from DI injected into a dataportal method. Add a parameter of the type you want injected to your DataPortal method, prefixed with the [Inject] attribute. [Fetch]
private void Fetch([Inject] ILogger<MyBusinessObject> logger)
{
logger.LogDebug("Hello world!");
} |
Beta Was this translation helpful? Give feedback.
-
For the first question there is no need to store dependencies.CSLA Objects have a property called
|
Beta Was this translation helpful? Give feedback.
-
You can store dependencies, but it might cause you problems because of the behaviour of the dataportal. CSLA will serialize the object in quite a few scenarios. As a result, the objects you store are probably going to have to implement IMobileObject in order to be compatible with the serialization. That might be a problem for you. I agree that using ApplicationContext.GetRequiredService() is a little bit of an anti-pattern. This is the service locator design pattern, and it is generally recommended that you avoid this pattern, if you can. However, needs must, and if the dependency you are using won't play nicely with CSLA's serialization, then this is an option. You can write wrappers for types that need to be serialized and which implement IMobileObject, but it will create more work for you. |
Beta Was this translation helpful? Give feedback.
-
Generally speaking, logically server-side behaviors (such as sending an email) should be implemented in data portal operation methods, and you can inject parameters into those methods. Logging I see as a potentially different thing, since it seems reasonable to think about logging user interactions with the domain objects when they are running on the logical client. There's no good answer for this at the moment, because constructor injection isn't possible with mobile objects. Nor is property injection, though perhaps that's something to consider. Hmm. Basically, CSLA controls the lifecycle of domain objects, because it may serialize them to/from the server, or to implement n-level undo, or for the Deserialization means invoking the constructor of the domain types, and at least so far I have been unwilling to treat all domain types as "services" from a DI perspective. There are some interesting consequences of doing such a thing. We would
To avoid those negative consequences, I've chosen to implement method-level injection of parameters in the data portal operations, and leave any other (more rare) scenarios to relying on the service locator pattern via |
Beta Was this translation helpful? Give feedback.
-
For logging we have considered 'DataPortal_OnDataPortalInvoke' in the past. I am assuming if you don't send emails for logging then we have implemented an EmailSender class which is essentially a BusinessBase and the reason is, we have some business/validation rules and Command object wasn't adequate. |
Beta Was this translation helpful? Give feedback.
Generally speaking, logically server-side behaviors (such as sending an email) should be implemented in data portal operation methods, and you can inject parameters into those methods.
Logging I see as a potentially different thing, since it seems reasonable to think about logging user interactions with the domain objects when they are running on the logical client. There's no good answer for this at the moment, because constructor injection isn't possible with mobile objects. Nor is property injection, though perhaps that's something to consider. Hmm.
Basically, CSLA controls the lifecycle of domain objects, because it may serialize them to/from the server, or to implement n-level undo, …