CSLA and the Entity Framework

CSLA and the Entity Framework

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


gPlans posted on Saturday, February 21, 2009

I have searched all over the internet for a single example of the two technologies (CSLA and EF) can work together but in vain. In theory, EF should provide CSLA with the DAL. In practice, no one seems to have attempted it yet.

Can someone please point me to a good example? And can someone answer me a simple question: what is to be gained from using the two together since it's very obvious that the two can be used independently?

I have looked at Rocky's Expert C# 2008 Business Objects but it dwells more on L2S and just a mention of EF. With rumors going around (and I suspect they are not just rumors) that MS is no longer interested in further development of L2S I would have thought anyone choosing between L2S and EF should have chosen EF. Or what I am I missing?

 

sergeyb replied on Saturday, February 21, 2009

Pretty much everything from L2S applies to EF.  You would use EF in the same way with CSLA as you would L2S – as DAL.  I have been working part-time on CSLA/SL/EF project, and I actually like the combination.  We are not using stored procedures, just plain EF.  The DAL code is very different if you are using L2S with stored procedures though.   You still create a context though, retrieve the entity (for updates, inserts and deletes) and update it from BO, then call SaveChanges on context.  Retrieve data code is pretty much identical to L2S.  I think using EF results in less code than when you are using raw ADO, especially when it comes to parent/child/grandchild object trees.  There is a learning curve (albeit not very steep) in learning how to satisfy relationships during update process.  I think I will be talking at Atlanta Code Camp on CSLA for Silverlight, and I am planning to copy and re-work Rolodex project to use EF.  The Code Camp is in three weeks.  Whether my sessions gets approved or not, I will have the sample working and post all code in any case.

 

 

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: gPlans [mailto:cslanet@lhotka.net]
Sent: Saturday, February 21, 2009 8:07 AM
To: Sergey Barskiy
Subject: [CSLA .NET] CSLA and the Entity Framework

 

I have searched all over the internet for a single example of the two technologies (CSLA and EF) can work together but in vain. In theory, EF should provide CSLA with the DAL. In practice, no one seems to have attempted it yet.

Can someone please point me to a good example? And can someone answer me a simple question: what is to be gained from using the two together since it's very obvious that the two can be used independently?

I have looked at Rocky's Expert C# 2008 Business Objects but it dwells more on L2S and just a mention of EF. With rumors going around (and I suspect they are not just rumors) that MS is no longer interested in further development of L2S I would have thought anyone choosing between L2S and EF should have chosen EF. Or what I am I missing?

 



gPlans replied on Saturday, February 21, 2009

Thanks both,

Sergeyb, I had actually tried to play around with EF/CSLA but your advice makes me feel like I am on the right track. Once you are done with your project, please post it somewhere we can access it.

 

I am trying to take advantage of some of EF's features most notably Table per Type Inheritance. One of the problems I am running into is that most code generation tools can only generate the BO’s from the database schema. This means that although I have all the fields I want in entities, there is no way of generating BO’s from them. I have to create either Views or Stored Procedures with joins to generate my BO’s so that the fields correspond with those of the entities. The other option is writing the BO’s by hand.

 

Anyone has any ideas how one can go round this?

sergeyb replied on Saturday, February 21, 2009

Not that I am aware of.  Oddly enough, I do a fair amount of hand-coding on business objects, but I did write a small code generator myself.  It does not create entire objects, just code blocks, such as properties, data access code and some validation rules.   This tool allows me to pick fields from multiple tables, then I just copy/paste what I need into my business objects.  I found that this approach is not much slower to me than full code gen, but it results in cleaner code IMHO because I only write methods that I need for each object,  Just my 2 cents though, I know that majority of people cod-gen entire classes.  I will surely post the project in about 3 weeks, give or take.

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: gPlans [mailto:cslanet@lhotka.net]
Sent: Saturday, February 21, 2009 12:05 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: CSLA and the Entity Framework

 

Thanks both,

Sergeyb, I had actually tried to play around with EF/CSLA but your advice makes me feel like I am on the right track. Once you are done with your project, please post it somewhere we can access it.

 

I am trying to take advantage of some of EF's features most notably Table per Type Inheritance. One of the problems I am running into is that most code generation tools can only generate the BO???s from the database schema. This means that although I have all the fields I want in entities, there is no way of generating BO???s from them. I have to create either Views or Stored Procedures with joins to generate my BO???s so that the fields correspond with those of the entities. The other option is writing the BO???s by hand.

 

Anyone has any ideas how one can go round this?



sergeyb replied on Monday, March 16, 2009

I presented on CSLA for Silverlight at the Atlanta Code Camp on Saturday.  I have posted the entire project on my blog (scroll down to SkyDrive / AtlantaCodeCamp to download).  I wrote DAL using Entity Framework, but without re-fetching the data for updates.  This is the most efficient, but far from straightforward way of doing it.  If you are interested, here is the link.

http://sergeybarskiy.spaces.live.com/default.aspx

 

Thanks

Sergey

 

lukky replied on Tuesday, March 17, 2009

Hi Sergey,

I'm still thinking of implementing a series of CodeSmith templates that would read an EF model, and generate the BO from it, with EF as the DAL.

All that I'm missing is a working sample of how to integrate EF with CSLA.

I'll look at your sample project, and it might become the base for my project Smile [:)]

Regards.

crussi replied on Wednesday, April 29, 2009

Hi Sergey,

What are some of the benefits of migrating from L2S to EF in regards to which is used for the DAL in a CSLA.NET project?  Thank you so much for providing the EF sample BTW :-)  I can't wait to dig into it!

Chris

crussi replied on Thursday, April 30, 2009

Hi Sergey,

I was able to compile and run the EF sample (from your downloads) under CSLA 3.6.1.  But, when I try to edit a record I get an XAML error on lines dealing with the csla datagrid validation control.  I am running SL2.  Any suggestions?  I was running SL3, but I downgraded to SL2 because I thought that was it.  I'm excited to work with the EF under CSLA and any help would be greatly appreciated.

 

Best regards,

Chris

crussi replied on Friday, May 01, 2009

I'm just bumping this up ...

Anyone have any ideas on the EF sample?

Chris

sergeyb replied on Friday, May 01, 2009

I used 3.6.2 for this sample.  Make sure you are referencing correct version of CSLA and Silverlight.  I am not sure what else would cause the issue.  Knowing exact error might help…

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: crussi [mailto:cslanet@lhotka.net]
Sent: Friday, May 01, 2009 5:25 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: RE: CSLA and the Entity Framework

 

I'm just bumping this up ...

Anyone have any ideas on the EF sample?

Chris



crussi replied on Tuesday, May 05, 2009

Hi Sergey,

Thank you so much for your response.  I apologize for not seeing your reply until now.  I started with 3.6.2, but got a strange error "The call is ambiguous between the following methods or properties" similar to the one described in this post: http://forums.lhotka.net/forums/thread/32337.aspx  So, I assumed I needed to use the earlier 3.6.1 version and that is when I got the XAML error on lines dealing with the csla datagrid validation control.  So, I will upgrade again to using 3.6.2.

So, I should have csla.net 3.6.2 framework and SL2.  I'll try again.  Can you please take a look at the error discussed here http://forums.lhotka.net/forums/thread/32337.aspx?  Any idea what was the cause of the original error "The call is ambiguous between the following methods or properties".  My gut feeling was that the client was using cslalight and the server was using csla.  Since the object class is shared between the client and the server code base the compiler was confused about a particular property.

I'm excited to migrate up to EF from LINQ to SQL and your sample app is the only model I know of to follow.  So, I'm anxious to get it working.

Best regards,

Chris

sergeyb replied on Tuesday, May 05, 2009

I am guessing that default value needs to be explicitly casted.

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: crussi [mailto:cslanet@lhotka.net]
Sent: Tuesday, May 05, 2009 3:38 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: RE: RE: CSLA and the Entity Framework

 

Hi Sergey,

Thank you so much for your response.  I apologize for not seeing your reply until now.  I started with 3.6.2, but got a strange error "The call is ambiguous between the following methods or properties" similar to the one described in this post: http://forums.lhotka.net/forums/thread/32337.aspx  So, I assumed I needed to use the earlier 3.6.1 version and that is when I got the XAML error on lines dealing with the csla datagrid validation control.  So, I will upgrade again to using 3.6.2.

So, I should have csla.net 3.6.2 framework and SL2.  I'll try again.  Can you please take a look at the error discussed here http://forums.lhotka.net/forums/thread/32337.aspx?  Any idea what was the cause of the original error "The call is ambiguous between the following methods or properties".  My gut feeling was that the client was using cslalight and the server was using csla.  Since the object class is shared between the client and the server code base the compiler was confused about a particular property.

I'm excited to migrate up to EF from LINQ to SQL and your sample app is the only model I know of to follow.  So, I'm anxious to get it working.

Best regards,

Chris



rsbaker0 replied on Saturday, February 21, 2009

gPlans:

...

 And can someone answer me a simple question: what is to be gained from using the two together since it's very obvious that the two can be used independently?

...

I haven't specifically looked at EF yet, but I assume based on what I have read it provides similar functionality to ORMs.

CSLA is giving you mobile objects and the ability to deploy your application in tiers, plus data bindability, validation rules, field-level authorization, and n-level undo. I think these are a plus independent of whatever data access method is chosen. Moreover, CSLA basically leaves fetching and persisting of objects completely up to you and hence can be used with almost whatever DAL you choose.

I'll be very interested in a CSLA + EF sample when one becomes available. I'm not unhappy with the ORM I'm currently using, but I don't have LINQ capability to retrieve objects from the database and still have to generate SQL.

Copyright (c) Marimer LLC