Showing posts with label Social Network DevNotes. Show all posts
Showing posts with label Social Network DevNotes. Show all posts

Saturday, 9 July 2011

LINQ to SQL: using First vs FirstOrDefault

In the Fisharoo examples of ASP.Net 3.5 Social Networking, there is an AccountRepository class which will retrieves populated Account entites from the database using LINQ to SQL.

Here is a snippet of the code:

        public Account GetAccountByID(int AccountID)
        {
            Account account = null;

            using (FisharooDataContext dc = conn.GetContext())
            {
                account = (from a in dc.Accounts
                           where a.AccountID == AccountID
                           select a).First();
            }
            return account;
        }

See any problems with this code?

I didn't...but after running it the during development when my Account table had no records...ie i was running the Application and Registering the very first user ever...the above code threw this exception on the LINQ code:

System.InvalidOperationException: Sequence contains no elements

This is happening because the LINQ query is returning an empty set and then you're trying to call .First() on it.

I don't understand why this was written this way...especially for the GetByID method...it seems quite likely to me that there would be instances where this code could be called with an ID of an account that doesn't exist.

Either way, its easy fixed by using the FirstOrDefault() method instead...this will simply assign 'null' to the 'account' variable in the event that the LINQ query returns an empty set - lovely!

Fisharoo: Register New User Process

I found that the book doesn't exactly explain everything - fair enough.

There was code in the book that didn't match code in my solution - which i was sure i got from the book. But after a closer look the code in the book was actually consistent and i had managed to get code from somewhere else...i think it had something to do with the publisher putting the wrong code files up on the website for the book. Anyway...

So i had to learn a bit more about the CreateUserWizardStep.

This article helped me understand the difference between CreateUserWizardStep and WizardStep

This article helped me understand how to add a WizardStep at the end of a CreateUserWizardStep

Tuesday, 5 July 2011

Understanding the MVP Pattern

Taken from the examples given in the Fisharoo project (Book: ASP.Net 3.5 Social Networking)

View
  • The ASP.Net code
  • The code behind (or is it code beside?) files 
    • Note that the code behind does not do any decision making...it only handles display logic like events and then calls the methods of the Presenter instance to defer the decision making.
  • An interface - implemented by the code behind and passed to the Presenter, enabling the Presenter access to methods/properties/et al of the View
    • This Interface is what allows us to swap out the UI whilst maintaining the Presenter and Model
    • That is: as long as the View (new UI) implements the Interface, it can continue to use the Presenter (which in turns use the Model)
  • Responsible for handling the events from the page such as button clicks
  • Also takes care of simple display issues...such as?
  • Provides methods to the Presenter to enable it to toggle the state of various display items
Presenter
  • Often does not include a lot of logic either
  • Quick to pass on the major logic to the Model, often via Services
  • The Presenter will populate Business Entities with values passed to it from the View and then pass those Entities onto methods in the Model
  • The Presenter will use information passed to it from the View to perform validation and redirection
  • The Presenter will also use information passed back to it from the Model to decide what is the next best course of action eg Redirect, Display Message, etc.

    Monday, 13 June 2011

    Fisharoo - Database Design - Questionable NULL attributes?

    Relating to the example project, Fisharoo, associated with the fantastic book ASP.Net 3.5 Social Networking by Andrew Siemer, I am wondering why the architect has allowed for NULL entries in the following columns:

    Email
    Username
    Password
    CreateDate
    LastUpdateDate

    To me these fields are all imperative to the management of a User Account based website - especially a social network site.

    So unless there is some other reason (EG to make working with StructureMap or other automation tools smoother), then i'll have to lean towards disagreeing with the architect on that one...

    Until proven otherwise, i'll be setting these fields as NON NULLs...easier to change to NULL later on than Vice Versa...!

    Friday, 6 May 2011

    ASP.Net Social Networking - Fisharoo: Inconsistent Connection class

    In one post, the Connection class is listed like this:

    using System.Configuration;
    using System.Linq;
    using System.Data.Linq;
    namespace Fisharoo.FisharooCore.Core.DataAccess.Impl
    {
        public class Connection
        {
            public static FisharooDataContext GetContext()
            {
                FisharooDataContext fdc = new FisharooDataContext
                   (ConfigurationManager.ConnectionStrings
                   ["Fisharoo"].ToString());
                return fdc;
            }
        }
    }

    In another listing, it is like this:

    using System;
    using System.Configuration;
    using System.Linq;
    using System.Data.Linq;
    using System.Xml;
    using Fisharoo.FisharooCore.Core.Domain;
    using Fisharoo.FisharooCore.Properties;
    using StructureMap;
    namespace Fisharoo.FisharooCore.Core.DataAccess.Impl
    {
        public class Connection
        {
            public FisharooDataContext GetContext()
            {
                string connString = "";
                //logic to retrieve your connectionString
                FisharooDataContext fdc = new
                FisharooDataContext(connString);
                return fdc;
            }
        }
    }
    The other issue is that the code doesn't even compile...I get the error:

    Error    6    'Fisharoo.FisharooCore.Domain.FisharooDataContext' does not contain a constructor that takes 1 arguments    ....trunk\source\FisharooCore\DataAccess\Impl\Connection.cs    16    39    FisharooCore

    So i deleted the LINQ to SQL file 'Fisharoo.dbml' and then performed 'Add New Item...' from the Solution context menu to see if it would somehow magically create a new constructor which takes the ConnectionString as an argument...

    I then got this...notice how there is no Fisharoo.cs file (yet):


    I then added all the Tables via Server Explorer. 

    Removed all the Relationships on the LINQ Object Relational Designer surface (as recommended in the book) and clicked Save.

    Still no Fisharoo.cs file - perhaps this is a good thing?

    Going into the Class View, I can now see all the relevant Entity Classes that  Visual Studio has generated for me...YIPPEE!!!

    However...I have some Domain naming issues. All the source code references in the book go by the convention:

    "Fisharoo.SOME_DOMAIN.SOME_SUB_DOMAIN.Class"

    However, I've named all my projects without the "Fisharoo" prefix, simply because all the "SOME_DOMAINS" in the book are prefixed with "Fisharoo" anyway...eg FisharooCore, FisharooWeb...etc.

    So, i'll have to either to check everything out and rename my projects to suit the convention...which ideally i would like to do as it would then be consistent with the book and also follow Naming Conventions i found earlier last month...

    OR
    Just update the namespace references within all the referencing classes...an easy and not so ideal fix.

    ASP.Net Social Networking - Fisharoo: Interfaces

    Have been plugging away using Andrew Siemer's book for some time now.

    Its now time to start blogging about some of the challenges and learnings.

    The first thing is, Andrew doesn't go into any detail regarding the definition of the Interfaces defining the behaviour driving the StrucutreMap Factory Dependancy Injection pattern.

    So at this point, I have created a new namespace:

    Fisharoo.Interfaces

    And for each class that uses StructureMap, ie, has a [Pluggable] attribute, i define an interface as such:

    namespace FisharooCore.Interfaces
    {
        [PluginFamily("Default")]
        public interface IConfiguration
        {
        }
    }

    Thing is, he often refers to the use of a ConfigurationManager class.

    Where is this defined? System.Configuration???

    Not in the .Net version i have (3.5)...at thats what i thought!

    I searched all the documentation and everything pointed to the fact that the class should be available as long as the System.Configuration namespace was being referenced.

    AMATEUR MISTAKE: You also have to explicitly Add Reference to the System.Configuration DLL to your project.

    I guess i just assumed that it would have been referenced by default...but its not.