.Net Code Monkey RSS 2.0
 Saturday, April 12, 2008

I'm sure many of you are aware that you can override the connection string settings in your DataAccess layer of your web application with connection string settings in your Web.Config. We found this out recently and found it very useful in an intranet application at work recently. However this week one of the other developers required that he could override a setting the BusinessEntity layer of his application from the App.Config in the console layer.

A quick chat with Mr Google didn't turn up a solution for this, so with this in mind all three of us quickly tried to return a working solution. I'm going to try to give an example of what we did, incase it something others may find of use. First we create a solution with two class library projects within it. The first will simulate the DataAccess layer and the second will simulate the BusinessEntity layer in an application. In the BusinessEntity project we will create a reference to the DataAccess project.

Starting with the DataAccess project we will add an App.Config file by creating a setting called "MySetting" in the Settings tab of the project Properties. The setting scope will be set to Application.

This creates a new sectionGroup called applicationSettings and a new section in the App.Config file and places the new setting in their too.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" 
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="Playground.ConfigurationOverride.DataAccess.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" /> </sectionGroup> </configSections> <applicationSettings> <Playground.ConfigurationOverride.DataAccess.Properties.Settings> <setting name="MySetting" serializeAs="String"> <value>Setting in DataAccess</value> </setting> </Playground.ConfigurationOverride.DataAccess.Properties.Settings> </applicationSettings> </configuration>

Next we will add a simple class to the DataAccess project with a single static method that will return the value of the setting we have just created.

using System;

namespace Playground.ConfigurationOverride.DataAccess
{
    public static class SettingsHelper
    {
        public static string GetMySetting()
        {
            return Properties.Settings.Default.MySetting;
        }
    }
}

Next we will create anothe simple class, SettingsGetter, in the but in the BusinessEntity layer this time. It will just return the value returned from the SettingsHelper class in the DataAccess layer.

using System;

namespace Playground.ConfigurationOverride.BusinessEntity
{
    public static class SettingsGetter
    {
        public static string GetDataAccessSettingValue()
        {
            return DataAccess.SettingsHelper.GetMySetting();
        }
    }
}
Next we need to create a presentation layer. So first off we will create a small web application and add it to the solution, and grab a reference to the BusinessEntity layer. Set the web site as the startup project. We will also add a Label to the default webform and a Button. In the Page_Load handler method populate the label text from the business entity method.
protected void Page_Load(object sender, EventArgs e)
 {
     uxSettingsLabel.Text = 
         Playground.ConfigurationOverride.BusinessEntity.SettingsGetter.GetDataAccessSettingValue();
 }

...and run the application, and allow the Web.Config file to be created. The value of the setting in the DataAccess App.Config file should be displayed.

 

More to follow soon...

Saturday, April 12, 2008 7:03:24 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.Net | Asp.Net | C# | Config Files | XML
 Friday, April 11, 2008

Back to Part 2

Review Of Progress So Far

So, to date we have created an interface for our ShapeFactory and any other shape factory to implement, an enumeration that represents all the available shapes we may wish to build, and a factory class that can build any of our two current shapes. The next step is to create a graphical user interface that will call the factory and receive a couple of shape objects for it's trouble.

The Client Gets What They Deserve

Next we need a client to display the shapes, so for simplicity we are going to create a small Windows Forms project called WindowsApplication and add it to the solution. The project will have a single form called Main (700x550) which will have a picture box (uxPictureBox) and a button, uxDrawButton.

We now need to add a handler for the Draw Button and declare a Bitmap and a Graphics object at the top of the class. In the form constructor we will initialise the bitmap to a new instance which will be the size of the PictureBox. We will then create the Graphics object from the Bitmap.

In the button handler method we will clear the graphics object to a white background.

public partial class Main : Form
 {
     private Bitmap _bitmap;
     private Graphics _graphics;

     public Main()
     {
         InitializeComponent();

         _bitmap = new Bitmap(uxPictureBox.Width, uxPictureBox.Height);
         _graphics = Graphics.FromImage(_bitmap);
         uxPictureBox.Image = _bitmap;
     }

     private void uxDrawButton_Click(object sender, EventArgs e)
     {
         _graphics.Clear(Color.White);
     }
 }

We now need a collection to hold our shapes, so we will declare a list of objects that implement IShape at the top of the class, and to do this we will need to provide a reference to the ShapeInterfaces project. At the same time add a reference to the Factories project and import the namespaces for both projects at the top of the form code.

using System.Text;
using System.Windows.Forms;
using Playground.ShapeMaker.Factories;
using Playground.ShapeMaker.ShapeInterfaces;

namespace Playground.ShapeMaker.WindowsApplication
{
    public partial class Main : Form
    {
        private Bitmap _bitmap;
        private Graphics _graphics;
        private List<IShape> _shapes;

        public Main()
        {

Next we need to add two method stubs that are void of return values, AddShapes() and DrawShapes(). Surprisingly, AddShapes() will take responsibility for adding new shapes to our list and DrawShapes() will draw those shapes. We will add a line to create an instance of our list in the constructor and below that we will place a call to the AddShapes() method. We will place a call in the button handler to the DrawShapes() method, below the line that clears the graphics to a white background. Follow this with a refresh of the PictureBox.

public Main()
 {
     InitializeComponent();
     _bitmap = new Bitmap(uxPictureBox.Width, uxPictureBox.Height);
     _graphics = Graphics.FromImage(_bitmap);
     uxPictureBox.Image = _bitmap;

     _shapes = new List<IShape>();
     AddShapes();
 }

 /// <summary>
 /// Handles the draw button pressed event.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void uxDrawButton_Click(object sender, EventArgs e)
 {
     _graphics.Clear(Color.White);
     DrawShapes();
uxPictureBox.Refresh(); }

In the AddShapes() method we need to create a new instance of our ShapeFactory object. Then using the ShapeFactory object add two shapes, a circle and a rectangle to the list.

 /// <summary>
 /// Adds shapes to the list
 /// </summary>
 private void AddShapes()
 {
     ShapeFactory shapeFactory = new ShapeFactory();
     _shapes.Add(shapeFactory.CreateShape(Shape.Circle));
     _shapes.Add(shapeFactory.CreateShape(Shape.Rectangle));
 }

Because we know that the shapes Implement the IShape interface we have a contract that says we can call the Draw() methods on any of these shapes. These objects will then take the responsibility for drawing them selves. So in the DrawShapes() method all we need to do is iterate through all the shapes in the list and call each shapes Draw() method.

 /// <summary>
 /// Draws the shapes in the list
 /// </summary>
 private void DrawShapes()
 {
     foreach(IShape shape in _shapes)
     {
         shape.Draw();
     }
 }

And that would be it... apart from we haven't added any working code into the Draw() methods of each concrete object yet!

A Change In The Contract

Apart from the fact there is no code in the concrete objects to actually draw them selves, I have just noticed the need to be able to pass the Graphics object into each shape object, so we need to go back to the interfaces and change the signature for the Draw() method.

So if we take a look in the IShape interface we need to be able to pass in a Graphics object as a method parameter.

public interface IShape
 {
     /// <summary>
     /// Represents the center of the shape
     /// </summary>
     Point Center { get; set;}

     /// <summary>
     /// The method used to draw the shape
     /// </summary>
     /// <param name="graphics">A reference to the Graphics object to draw the shape on.</param>
     void Draw(Graphics graphics);
 }

And while we are in this project I think we will take the opportunity to extend the CreateShape() method signature in the IShapeFactory interface, too. We are going to add a center point parameter; this will allow us to position the shape where we like.

public interface IShapeFactory
 {
     /// <summary>
     /// Use this method to create a shape.
     /// </summary>
     /// <param name="shape">Indicates the type of shape to be made.</param>
     /// <param name="center">Indicates the center point of the shape.</param>
     /// <returns>An object that implements the IShape interface.</returns>
     IShape CreateShape(Shape shape, Point center);
 }

A quick build of the project will highlight where our change in interface has broken the contract between it and the objects that implement it so in the BaseShape object we need to add a Graphics parameter into the Draw() method. This will be the same in the Rectangle and the Circle class.

public abstract void Draw(Graphics graphics);

In the ShapeFactory class we need to extend the CreateShape(), CreateCircle() and CreateRectangle() methods signatures to include the center point parameter. Then in the CreateShape() method we need to pass the center point parameter on to the worker method calls.

/// <returns>A shape object that implements IShape.</returns>
public IShape CreateShape(Shape shape, Point center)
{

In the two worker methods, CreateCircle() and CreateRectangle() we will populate the object's Center property with the value from the center parameter passed in.

 /// <summary>
 /// This method creates a circle object.
 /// </summary>
 /// <returns>A shape object that implements ICircle.</returns>
 private ICircle CreateCircle(Point center)
 {
     Circle circle = new Circle();
     circle.Center = center;
     circle.Diameter = 50;
     return (ICircle)circle;
 }

 /// <summary>
 /// This method creates a rectangle object.
 /// </summary>
 /// <returns>A shape object that implements IRectangle.</returns>
 private IRectangle CreateRectangle(Point center)
 {
     BusinessEntity.Rectangle rectangle = 
         new BusinessEntity.Rectangle();
     rectangle.Center = center;
     rectangle.Size = new Size(400, 200);
     return (IRectangle)rectangle;
 }

Then back in the form code we need to alter the calls to the ShapeFactory CreateShape() methods to include a center position

 /// <summary>
 /// Adds shapes to the list
 /// </summary>
 private void AddShapes()
 {
     ShapeFactory shapeFactory = new ShapeFactory();
     _shapes.Add(shapeFactory.CreateShape(Shape.Circle, new Point(250,250)));
     _shapes.Add(shapeFactory.CreateShape(Shape.Rectangle, new Point(350, 200)));
 }

 /// <summary>
 /// Draws the shapes in the list
 /// </summary>
 private void DrawShapes()
 {
     foreach(IShape shape in _shapes)
     {
         shape.Draw(_graphics);
     }
 }

Back to the Drawing Board

So finally we can get back into the concrete shape classes and write the code that will draw the shape.

So let us look at the Circle's Draw() method first. We'll add the code to allow the Circle to draw it's self.

 /// <summary>
 /// Use this method to draw the shape
 /// </summary>
 public override void Draw(Graphics graphics)
 {
     Pen pen = new Pen(Color.DarkGray, 0);
     int left = (int)Center.X - (Diameter/2);
     int top = (int)Center.Y - (Diameter/2);

     System.Drawing.Rectangle outlineRectangle =
         new System.Drawing.Rectangle(left, top, Diameter, Diameter);
     graphics.DrawEllipse(pen, outlineRectangle);
 }

And the same for the Rectangle.

 /// <summary>
 /// Use this method to draw the shape
 /// </summary>
 public override void Draw(Graphics graphics)
 {
     Pen pen = new Pen(Color.Red, 0);
     int left = (int)Center.X - (Size.Width / 2);
     int top = (int)Center.Y - (Size.Height / 2);

     System.Drawing.Rectangle outlineRectangle =
         new System.Drawing.Rectangle(left, top, Size.Width, Size.Height);
     graphics.DrawRectangle(pen, outlineRectangle);
 }

Now we need to make sure we have set the start up project to be the windows application and we should be good to go!

Conclusions

Now I'm certain that the application architecture and the code may not be laid out to best practises, and I'm aware there are plenty of parts that could have been better thought out and implemented, but really I just wanted to get a simple factory based application down on paper, so to speak, to get it a little clearer in my mind.

If any of these articles have helped you or planted the seed of inquisitiveness in your mind to find out more about factories then that can only be a good thing. If there is anything I have done in this article that is quite clearly incorrect or bad practise, please feel free to leave a comment. Any constructive feed back is appreciated.

The next step will be to make the objects prinatable. But until then, have fun!

Part 1 | Part 2

2008-04-11_ShapeMaker.zip (103.25 KB) <- Solution files all zipped up.
Friday, April 11, 2008 8:44:42 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.Net | C# | Classes | Factory Pattern | Inheritance | Interfaces
 Thursday, April 10, 2008

Back to Part 1

Review Of Progress So Far

So far we have two concrete shape classes, Circle and Rectangle that derive from a base shape class. The base shape class implements the IShape interface and the concrete classes implement in turn the IShape interface as well as their respective shape interfaces. The next step is to create a factory class to create our shapes.

Working In The Factory

So, we need to add to the solution, another new class library project which we will call Factories. This will house our ShapeFactory class. The ShapeFactory class will be responsible for creating our shape objects. It will be able to create and return an object that implements the IShape interface. So far we have ensured that both our circle and rectangle inherently implement the IShape interface for two reasons. One, because they both derive from the BaseShape object and we know that implements IShape, and two, because they implement ICircle and IRectangle which in turn implements IShape, as well. (Maybe some one with a little more experience than I, could advise if this is an acceptable practise. -> I can't see why not!)

But before we create our ShapeFactory we have one more thing to do. Because we may want to use more than one factory in the future to create our shapes we are first going to create another interface, which will describe what a ShapeFactory need to create some shape objects. This will be the IShapeFactory interface, and we are going to add that for the sake of simplicity to our ShapeInterfaces project

using System;

namespace Playground.ShapeMaker.ShapeInterfaces
{
    public interface IShapeFactory
    {
        /// <summary>
        /// Use this method to create a shape.
        /// </summary>
        /// <returns>An object that implements the IShape interface.</returns>
        IShape CreateShape();
    }
}

As you can see we have determined that any factory that implements IShapeFactory will have to contain a method that returns an object implementing IShape. The problem is, how do we determine what sort of shape we want to get? Well even though we only have two shapes at the moment for future expansion we will need an enumeration to represent these. As enumeration is to be used in every class that implements IShapeFactory and will need to be a parameter in the CreateShape method, it seems logical to me to place the enumeration in the ShapeInterfaces project. If any one can advise whether this is good practise or not, please do! So, let us add the Shape enumeration.

using System;

namespace Playground.ShapeMaker.ShapeInterfaces
{
    /// <summary>
    /// Represents the types of shapes that can be made
    /// </summary>
    public enum Shape
    {
        /// <summary>
        /// Indicates the shape is to be a circle
        /// </summary>
        Circle,

        /// <summary>
        /// Indicates the shape is to be a rectangle
        /// </summary>
        Rectangle
    }
}
We can now modify the CreateShape method of the IShapeFactory interface to accept a Shape enumerated value.
public interface IShapeFactory
 {
     /// <summary>
     /// Use this method to create a shape.
     /// </summary>
     /// <param name="shape">Indicates the type of shape to be made.</param>
     /// <returns>An object that implements the IShape interface.</returns>
     IShape CreateShape(Shape shape);
 }

So it's back to the Factories project now to create the ShapeFactory class that will provide us with some shapes. To create some shapes we need to add a reference to both the BusinessEntity and the ShapeInterfaces projects into the Factories project. After adding our new ShapeFactory class into the Factories project we need to import the BusinessEntity and the ShapeInterfaces namespaces into the top of it. We now need to make the ShapeFactory class implement IShapeFactory.

using System;
using Playground.ShapeMaker.BusinessEntity;
using Playground.ShapeMaker.ShapeInterfaces;

namespace Playground.ShapeMaker.Factories
{
    public class ShapeFactory : IShapeFactory
    {

        #region IShapeFactory Members

        public IShape CreateShape(Shape shape)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}

We are now going to create a pair of private methods that will create our two shapes. Although these methods will create two concrete objects the methods will return not the objects but their interfaces. First though we will just create the method stubs. Then we will modify the CreateShape() method to call one or the other of these two methods depending upon the enumerated value passed in.

#region IShapeFactory Members

 /// <summary>
 /// Creates a shape and returns it.
 /// </summary>
 /// <param name="shape">Indicates the shape to create.</param>
 /// <returns>A shape object that implements IShape.</returns>
 public IShape CreateShape(Shape shape)
 {
     IShape createdShape = null;
     switch (shape)
     {
         case Shape.Circle:
             createdShape = CreateCircle();
             break;

         case Shape.Rectangle:
             createdShape = CreateRectangle();
             break;

         default:
             throw new Exception("Unknown shape encountered in CreateShape() method.");
     }
     return createdShape;
 }

 #endregion

 /// <summary>
 /// This method creates a circle object.
 /// </summary>
 /// <returns>A shape object that implements ICircle.</returns>
 private ICircle CreateCircle()
 {

 }

 /// <summary>
 /// This method creates a rectangle object.
 /// </summary>
 /// <returns>A shape object that implements IRectangle.</returns>
 private IRectangle CreateRectangle()
 {

 }

We have used switch / case to select which method will create our shape object. Notice that we use the default case to throw an exception if we encounter a shape that we haven't catered for. This is in case at any time the shape enumeration is extended to provide more shapes. Notice also that although the CreateShape() method returns an object that implements IShape, the two worker methods return ICircle and IRectangle. This works because both ICircle and IRectangle both implement the IShape interface, and that is what CreateShape() will return.

Anyway let us crack on and 'flesh out' the CreateCircle() worker method! Because our shapes using the System.Drawing namespace, our factory that is going to create these will need to also. So we need to add a reference to and import that namespace for the ShapeFactory. (Incidentally have you noticed the more you look at the word "circle" the more wrong it looks!)

 /// <summary>
 /// This method creates a circle object.
 /// </summary>
 /// <returns>A shape object that implements ICircle.</returns>
 private ICircle CreateCircle()
 {
     Circle circle = new Circle();
     circle.Center = new Point(250,250);
     circle.Diameter = 50;
     return (ICircle)circle;
 }

Now I would normally use constructor in my code, but this example isn't to show best practise of constructing objects, so I am going to construct my objects using the properties today. You will probably also notice that I am casting the newly constructed Circle object to the type of ICircle before returning it. You don't need to do this but I believe it is good practise. (Can any one confirm this please?)

At this point I have just realised an error with choosing a rectangle for a shape, due to a conflict with the System.Drawing.Rectangle object. Well we could change the shape to a square or a triangle.... but we have got this far, so I am going to stick with it. It will however mean there will be some ugly code with the BusinessEntity namespace forced in here and there!!!

 /// <summary>
 /// This method creates a rectangle object.
 /// </summary>
 /// <returns>A shape object that implements IRectangle.</returns>
 private IRectangle CreateRectangle()
 {
     BusinessEntity.Rectangle rectangle = 
         new BusinessEntity.Rectangle();
     rectangle.Center = new Point(300, 300);
     rectangle.Size = new Size(400, 200);
     return (IRectangle)rectangle;
 }

Now that should be it for our ShapeFactory for the moment. If all has gone well for you then your project should build without errors.

To sum up we have created an interface for this and any other shape factory to implement, an enumeration that represents all the available shapes we may wish to build, and a factory class that can build any of our two current shapes. the next step is to create a graphical user interface that will call the factory and receive a couple of shape objects for it's trouble.

Part 3

Thursday, April 10, 2008 6:28:13 PM (GMT Standard Time, UTC+00:00)  #    Comments [2] -
.Net | C# | Classes | Factory Pattern | Inheritance | Interfaces
Archive
<April 2008>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Blogroll
 Clemens Vasters
 Harry Pierson
Passion * Technology * Ruthless Competence
 Joshua Flanagan
A .NET Software Developer
 Michael Schwarz's Blog
Developing applications on the Microsoft platform since Windows 3.1!
 Omar Shahine
Yet another Microsoft blogger
 Scot GU
Scott Guthrie lives in Seattle and builds a few products for Microsoft
 Scott Hanselman
Programming Life and the Zen of Computers
 Tom Mertens
Tom's corner
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Duane Wingett
Sign In
Statistics
Total Posts: 17
This Year: 17
This Month: 2
This Week: 0
Comments: 2
Themes
Pick a theme:
All Content © 2008, Duane Wingett
DasBlog theme 'Business' created by Christoph De Baene (delarou)