Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Static Code Analysis

, 15 Mar 2010 CPOL
A static code analyzer building method call networks + sample applications.
CodeAnalyzerAndToDoSample.zip
Generator
bin
.noclean
Arebis.CodeAnalysis.Static.dll
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
Arebis.Common.dll
CGen.exe
Content.cst
Content.cst.gen
COperation.cst
COperation.cst.gen
files
GeneratorMain.cst
GeneratorMain.cst.gen
Index.cst
Index.cst.gen
IndexTop.cst
IndexTop.cst.gen
SOperation.cst
SOperation.cst.gen
UIOperation.cst
UIOperation.cst.gen
Output
SampleApp
LocalTestRun.testrunconfig
ToDoApplication
Infrastructure
Properties
Service References
ToDoContract
Properties
ToDoSample.vsmdi
ToDoService
Entities.edmx
Infrastructure
Properties
ToDoSample.Service.csproj.user
ToDoSampleDb.mdf
ToDoSampleDb_log.ldf
ToDoUnitTests
Properties
Testing
ToDoSample.UnitTests.csproj.user
UnitTests
StaticCodeAnalyzer
Arebis.CodeAnalysis.Static
Arebis.CodeAnalysis.Static.csproj.user
CodeModel.cd
Processors
Rules
Properties
Arebis.Common
Arebis
Collections
Generic
Reflection
Properties
System
Collections
Generic
Net2
Threading
Net2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ToDoSample.Service
{
    internal static class ToDoComponentManager
    {
        /// <summary>
        /// Lists all users.
        /// </summary>
        internal static IEnumerable<User> ListAllUsers()
        {
            using (var context = new ToDoObjectContext())
            {
                // Return whole user set:
                return context.UserSet.ToList();
            }
        }

        /// <summary>
        /// Lists all users with names matching a given regular expression.
        /// </summary>
        /// <param name="nameMatchString">Regular expression to match user names to.</param>
        internal static IEnumerable<User> ListUsersMatching(string nameMatchString)
        {
            using (var context = new ToDoObjectContext())
            {
                // Return filtered user set:
                return FilterUsers(context.UserSet, nameMatchString).ToList();
            }

        }

        /// <summary>
        /// Returns the user with the given id.
        /// </summary>
        internal static User GetUser(int id)
        {
            using (var context = new ToDoObjectContext())
            {
                // Return selected user:
                return context.UserSet.Where(u => u.Id == id).FirstOrDefault();
            }
        }

        /// <summary>
        /// Returns users with the given name.
        /// </summary>
        internal static User FindUserByName(string name)
        {
            // Return the first user matching the full name:
            return ListUsersMatching(name).FirstOrDefault();
        }

        /// <summary>
        /// Lists all non-done ToDo items for the given user.
        /// </summary>
        internal static IEnumerable<ToDoItem> ListToDoItemsForUser(int userId)
        {
            using (var context = new ToDoObjectContext())
            {
                // Query for ToDoItems not yet done:
                var query = from item
                            in context.ToDoItemSet
                                .Include("Owner")
                            where item.Owner.Id == userId
                               && item.DoneDate == null
                            select item;

                // Return query result:
                return query.ToList();
            }
        }

        /// <summary>
        /// Lists all ToDo items.
        /// </summary>
        internal static IEnumerable<ToDoItem> ListAllToDoItems()
        {
            using (var context = new ToDoObjectContext())
            {
                // Return whole ToDoItem set:
                return context.ToDoItemSet.ToList();
            }
        }

        /// <summary>
        /// Lists all done ToDo items for the given user.
        /// </summary>
        internal static IEnumerable<ToDoItem> ListDoneItemsForUser(int userId)
        {
            using (var context = new ToDoObjectContext())
            {
                // Query for done ToDoItems:
                var query = from item
                            in context.ToDoItemSet
                                .Include("Owner")
                            where item.Owner.Id == userId
                               && item.DoneDate != null
                            select item;

                // Return query result:
                return query.ToList();
            }
        }

        /// <summary>
        /// Returns the ToDo item with the given id.
        /// </summary>
        internal static ToDoItem GetToDoItem(int id)
        {
            using (var context = new ToDoObjectContext())
            {
                // Return selected user:
                return context.ToDoItemSet
                    .Include("Owner")
                    .Where(t => t.Id == id)
                    .FirstOrDefault();
            }
        }

        /// <summary>
        /// Creates a new ToDo item.
        /// </summary>
        /// <param name="userId">The user's id to assign the ToDo to.</param>
        /// <param name="toDoText">The text or message of the ToDo.</param>
        /// <param name="dueDate">The due date of the ToDo.</param>
        /// <returns>The created ToDo item.</returns>
        internal static ToDoItem CreateToDoItem(int userId, string toDoText, DateTime? dueDate)
        {
            using (var context = new ToDoObjectContext())
            {
                context.Connection.Open();

                // Retrieve user/owner:
                User owner = context.UserSet.Where(u => u.Id == userId).FirstOrDefault();
                if (owner == null) throw new Contract.InvalidIdException();

                // Create ToDo item:
                ToDoItem newItem = new ToDoItem()
                {
                    CreatedDate = DateTime.Now,
                    Owner = owner,
                    Text = toDoText,
                    DueDate = dueDate
                };

                // Add to context:
                context.AddToToDoItemSet(newItem);

                // Save context changes:
                context.SaveChanges();

                // Return the created ToDo item:
                return newItem;
            }
        }

        /// <summary>
        /// Updates the text and due date of the ToDoItem with the given id.
        /// </summary>
        /// <param name="itemId">The id of the ToDo item to update.</param>
        /// <param name="toDoText">The new ToDoItem text.</param>
        /// <param name="dueDate">The new ToDoItem due date.</param>
        internal static void UpdateToDoItem(int itemId, string toDoText, DateTime? dueDate)
        {
            using (var context = new ToDoObjectContext())
            {
                context.Connection.Open();

                // Retrieve todo item:
                ToDoItem item = context.ToDoItemSet.Where(i => i.Id == itemId).FirstOrDefault();

                // Update item:
                item.Text = toDoText;
                item.DueDate = dueDate;

                // Save context changes:
                context.SaveChanges();
            }
        }

        /// <summary>
        /// Marks the ToDo item with the given id as done.
        /// </summary>
        internal static void MarkToDoItemDone(int itemId)
        {
            using (var context = new ToDoObjectContext())
            {
                context.Connection.Open();

                // Retrieve todo item:
                ToDoItem item = context.ToDoItemSet.Where(i => i.Id == itemId).FirstOrDefault();
                if (item == null) throw new Contract.InvalidIdException();

                // Mark item done:
                item.DoneDate = DateTime.Now;

                // Save context changes:
                context.SaveChanges();
            }
        }

        /// <summary>
        /// Deletes the ToDo item with the given id.
        /// </summary>
        internal static void DeleteToDoItem(int itemId)
        {
            using (var context = new ToDoObjectContext())
            {
                context.Connection.Open();

                // Retrieve todo item:
                ToDoItem item = context.ToDoItemSet.Where(i => i.Id == itemId).FirstOrDefault();
                if (item == null) throw new Contract.InvalidIdException();

                // Delete item:
                context.DeleteObject(item);

                // Save context changes:
                context.SaveChanges();
            }
        }

        #region Private implementation

        private static IEnumerable<User> FilterUsers(IEnumerable<User> users, string nameMatchString)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rudi Breedenraedt
Architect Wolters Kluwer Belgium
Belgium Belgium
Rudi is a Software Architect at Wolters Kluwer Belgium.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 15 Mar 2010
Article Copyright 2010 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid