Read-only list and data access with an ObjectFactory

Read-only list and data access with an ObjectFactory

Old forum URL: forums.lhotka.net/forums/t/7579.aspx


Dane posted on Sunday, September 06, 2009

I just starting experimenting with the Csln .net framework in an attempt to prototype an idea for an application I have.  The application I have in mind would be n-tier and would have the busniess object assembly isolated from all data access.  As a first test I decided to implement a simple read-only list (inherits from ReadOnlyListBase).  I put the business object (TestList) in a Library assembly and created an ObjectFactory (TestListFactory) in a DataAccess assembly.  I decorated TestList with the ObjectFactory attribute and implemented the Fetch method on the TestListFactory object.  I created a small boot-strap application to spin up the list and when I do the factory object's Fetch method is called, data is accessed but then I get the exception "Insert is an invalid operation".  I figured out this is because the ReadOnly property is never set on the list for load.  The question is how do you set that property when your data access is occuring in an different class (and different assembly)?  It appears at first glace like the ReadOnlyListBase simply isn't a usable class if you want to use ObjectFactory and encapsulate your data access in an assembly outside the primary business object assembly.

JonnyBee replied on Sunday, September 06, 2009

Hi,

Your factory class must/should inherit from Csla.Server.ObjectFactory.

The ReadOnlyList is exactly what the name says - Readonly - so you must use the inherited function SetIsReadOnly(mylist, false) before you add items into the list and reset list to readonly before you leave the Fetch method.

You may want t look at the ProjectTrackercs in Samples for Csla 3.6.3 for Net 2 - this project uses ObjectFactory for Data Access. http://www.lhotka.net/cslacvs/viewvc.cgi/branches/V3-6-x-N2/

ObjectFactory also exposes other methods that is very helpful for creating a data layer!

It is also recommended to create your own base classes for use in your app to create logging ansd extend the framework when you need to. A starting point for baseclasses can be found at CslaContrib - in the MyCsla project.

And I strongly recommend the Expert C# 2008 Business Objects book from Rocky to learn the framework and how to use it.

/jonnybee

Dane replied on Tuesday, September 08, 2009

Thanks.  The SetIsReadOnly method is what I was looking for.  I do understand the meaning of "read-only" and, in fact, in my post I stated I knew why the exception was occurring but didn't know a way around it.  I had implemented my own version of a method to set read-only on the list class itself but I wasn't happy with that because it seemed like framework probably had a way to do it already.  I own and have read Expert C# 2008 Business Objects but the ReadOnlyList examples in the book and in the version of the cslatest solution I have currently are pretty limited and do not implement the Object Factory pattern for the read-only list.  In fact the discussion of how to implement the Object Factory (pg. 553) doesn't really cover what I'm trying to accomplish other than saying it’s possible.  It states "The factory object may be in the business assembly, in its own assembly or in the data access assembly." then it goes on to illustrate the case that the factory object is implemented in the business assembly.  I'm trying to maintain complete separation of the business objects so that clients would need to receive only the business object assembly (and of course the csla assembly) and have no knowledge of the factory object or data access assemblies.

I'll look through the version of the test projects you linked to.  It sounds like it contains the ProjectTracker project implemented with a different pattern?

Thanks again for the info.  I guarantee I'll be posting more questions down the line (I already have another in mind).

Copyright (c) Marimer LLC