views:

151

answers:

4

Do you think it could be a good idea to use F# for implementation Business Logic Layer? I am going to use Entity Framework as 'data mapper' and implement UI logic using C#.

Any thought are welcome. I would appreciate any help!

Thanks.

P.S. What is a purpose of that? I am newbie in F# and would like to experiment with that Language (technology). I have to implement a relatively small project and it could be good to get F# experience.

+3  A: 

There's no reason you shouldn't do so.

I would just keep the UI logic in C#, since VS doesn't have terribly good support for F# UI design yet.

Zor
Well, there could be a reason. For example, the OP notes "I am newbie in F#"... if this is for a production system where there are deadlines, it could be not such a good idea to learn the language while trying to deliver a system.
codekaizen
Not a "real production"... :) I feel, it is sage to test :) But thank you anyway
Budda
+2  A: 

This is not a bad idea at all. If you do Linq over Entities, you might need F# PowerPack - http://fsharppowerpack.codeplex.com/

Mitya
+5  A: 

Implementing the "actual data processing" in F# is perhaps the most common use of F# currently, so implementing the business logic in F# seems like a good choice.

I don't think that you'll be able to use Entity Framework (easily) directly from F#, so you'll need to use C# to generate the data model and expose relevant data to F#. If you wanted to use LINQ to SQL then you can just generate the mapping in C# and write queries in F# using PowerPack (as Mitya suggests).

Perhaps the easiest thing to do would be to have three projects:

  • Data access layer in C# that simply uses Entity Framework and exposes the important data (using the IEnumerable type, which can be easily consumed from F#).

  • Business layer in F# that uses the data, performs the "actual processing" and exposes a couple of types that can be used from C#. If you declare a class in F# then it will be compiled just like any normal .NET class, so you can easily use it from C#. You just need to be careful not to use F# specific features in the public interface. A couple of suggestions would be to use delegates (instead of functions), class types and IEnumerable (called seq in F#) instead of functional lists.

  • User interface layer in C# that calls the types declared in F#. If you follow the simple rules above, then the C# code can easily call F# types.

As a side note - even though F# doesn't support designers, it can be quite great for user interface programming (see for example this article or my talk about Silverlight in F#). One thing you could do is to create your user interface in a C# library project, mark everything as public and then reference it from an F# project that actually controls the user interaction. However, this is a bit more advanced, so I think that starting with the business layer is a good idea.

Tomas Petricek
All good suggestions; I think though that data access layer can expose IQueryables instead of IEnumerables (which would mean keeping C# layer at a minimum - just what edmgen generates)
Mitya
Entity Framework could work fine in F#, too, but I guess for simplicity, better stick to C#. Either way, good suggestions.
Zor
@Mitya: You're right. It depends on the scenario. For example, writing grouping or joining isn't all that easy in the current version of F#, so it may be a good idea to do that in C# (for simplicity). However, you don't loose anything by exposing `IQueryable`, so that's probably a good idea.
Tomas Petricek
+4  A: 

I can only speak for myself, but after getting over the initial F# learning curve, I find that:

1) My F# code is more succinct than C# or C++.

2) The F# is more maintainable.

3) I can visualize solutions to problems clearer in F#.

4) Patterns I come up with in F# are easier to re-use in new programs.

So I will definitely be considering F# for many future coding tasks including BLL's.

-Neil

TechNeilogy