refactoring

This commit is contained in:
Paul Schneider 2014-10-08 10:47:40 +02:00
commit b5d19c5da6
38 changed files with 149 additions and 117 deletions

View file

@ -0,0 +1,35 @@
using System;
using System.Configuration;
using System.Reflection;
using System.Collections.Specialized;
using Yavsc.Model.Blogs.Configuration;
namespace Yavsc.Model.Blogs
{
public static class BlogHelper
{
public static BlogProvider GetProvider ()
{
BlogProvidersConfigurationSection config = ConfigurationManager.GetSection ("system.web/blog") as BlogProvidersConfigurationSection;
if (config == null)
throw new ConfigurationErrorsException("The configuration bloc for the blog provider was not found");
BlogProviderConfigurationElement celt =
config.Providers.GetElement (config.DefaultProvider);
if (config == null)
throw new ConfigurationErrorsException("The default blog provider was not found");
ConstructorInfo ci = Type.GetType (celt.Type).GetConstructor (Type.EmptyTypes);
BlogProvider bp = ci.Invoke (Type.EmptyTypes) as BlogProvider;
NameValueCollection c = new NameValueCollection ();
c.Add ("name", celt.Name);
c.Add ("type", celt.Type);
c.Add ("connectionStringName", celt.ConnectionStringName);
c.Add ("description", celt.Description);
c.Add ("applicationName", celt.ApplicationName);
bp.Initialize (celt.Name, c);
return bp;
}
}
}

View file

@ -0,0 +1,71 @@
using System;
using Yavsc.Model.Blogs;
namespace Yavsc.Model.Blogs
{
public static class BlogManager
{
public static long RemoveComment(long cmtid)
{
return Provider.RemoveComment (cmtid);
}
public static void Comment (string from, long postid, string content, bool visible)
{
provider.Comment (from, postid, content);
}
static BlogProvider provider;
public static BlogProvider Provider {
get {
if (provider == null)
provider = BlogHelper.GetProvider();
return provider;
}
}
public static BlogEntry GetPost (string username, string title)
{
return Provider.GetPost (username, title );
}
public static BlogEntry GetPost(long postid)
{
return Provider.GetPost (postid);
}
public static void Post(string username, string title, string content, bool visible)
{
Provider.Post(username, title, content, visible );
}
public static void UpdatePost(long postid, string content, bool visible)
{
Provider.UpdatePost(postid, content, visible);
}
public static BlogEntryCollection FindPost (string pattern, FindBlogEntryFlags searchflags, int pageIndex, int pageSize, out int totalRecords)
{
return Provider.FindPost (pattern, searchflags, pageIndex, pageSize, out totalRecords);
}
public static void RemovePost (string username, string title)
{
Provider.RemovePost (username, title);
}
public static BlogEntryCollection LastPosts (int pageIndex, int pageSize, out int totalRecords)
{
return Provider.LastPosts (pageIndex, pageSize, out totalRecords);
}
public static Comment[] GetComments(long postid, bool getHidden=true)
{
return Provider.GetComments (postid,getHidden);
}
/// <summary>
/// Tag the specified post by postid.
/// </summary>
/// <param name="postid">Postid.</param>
/// <returns>The tag identifier</returns>
public static long Tag(long postid, string tag) {
return Provider.Tag (postid, tag);
}
}
}

View file

@ -0,0 +1,33 @@
using System;
using System.Configuration;
using System.Configuration.Provider;
using System.Collections.Generic;
namespace Yavsc.Model.Blogs
{
public abstract class BlogProvider: ProviderBase
{
public abstract BlogEntry GetPost (long postid);
public abstract BlogEntry GetPost (string username, string title);
public abstract long GetPostId (string username, string title);
public abstract long Post (string username, string title, string content, bool visible);
public abstract void UpdatePost (long postid, string content, bool visible);
public abstract BlogEntryCollection FindPost (string pattern, FindBlogEntryFlags searchflags,
int pageIndex, int pageSize, out int totalRecords);
public abstract void RemovePost (string username, string title);
public abstract void RemovePost (long postid);
public abstract long RemoveComment (long cmtid);
public abstract BlogEntryCollection LastPosts(int pageIndex, int pageSize, out int totalRecords);
public abstract string BlogTitle (string username);
public abstract long Comment (string from, long postid, string content);
public abstract Comment[] GetComments (long postid, bool getHidden) ;
public abstract bool AutoValidateComment { get; set; }
public abstract void ValidateComment (long cmtid);
public abstract void UpdateComment (long cmtid, string content, bool visible);
public abstract long Tag (long postid,string tag);
public abstract void RemoveTag (long tagid);
}
}

View file

@ -0,0 +1,40 @@
using System;
using System.Configuration;
using System.ComponentModel;
namespace Yavsc.Model.Blogs.Configuration
{
public class BlogProviderConfigurationElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey=true)]
public string Name {
get { return (string)this ["name"]; }
set { this ["name"] = value; }
}
[ConfigurationProperty("type", IsRequired = true, IsKey=false)]
public string Type {
get { return (string)this ["type"]; }
set { this ["type"] = value; }
}
[ConfigurationProperty("connectionStringName")]
public string ConnectionStringName {
get { return (string)this ["connectionStringName"]; }
set { this ["connectionStringName"] = value; }
}
[ConfigurationProperty("description")]
public string Description {
get { return (string)this ["description"]; }
set { this ["description"] = value; }
}
[ConfigurationProperty("applicationName")]
public string ApplicationName {
get { return (string)this ["applicationName"]; }
set { this ["applicationName"] = value; }
}
}
}

View file

@ -0,0 +1,26 @@
using System;
using System.Configuration;
using System.ComponentModel;
namespace Yavsc.Model.Blogs.Configuration
{
public class BlogProvidersConfigurationCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement ()
{
return new BlogProviderConfigurationElement();
}
protected override object GetElementKey (ConfigurationElement element)
{
return ((BlogProviderConfigurationElement) element).Name;
}
public BlogProviderConfigurationElement GetElement (string name)
{
return this.BaseGet(name) as BlogProviderConfigurationElement;
}
}
}

View file

@ -0,0 +1,36 @@
using System;
using System.Configuration;
using System.ComponentModel;
namespace Yavsc.Model.Blogs.Configuration
{
public class BlogProvidersConfigurationSection : ConfigurationSection
{
/// <summary>
/// Gets or sets the default provider.
/// </summary>
/// <value>The default provider.</value>
[ConfigurationProperty("defaultProvider")]
public string DefaultProvider {
get { return (string) this ["defaultProvider"]; }
set { this["defaultProvider"] = value; }
}
[ConfigurationProperty("providers")]
[ConfigurationCollection(typeof(BlogProvidersConfigurationCollection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
/// <summary>
/// Gets or sets the providers.
/// </summary>
/// <value>The providers.</value>
public BlogProvidersConfigurationCollection Providers{
get { return (BlogProvidersConfigurationCollection) this["providers"]; }
set { this["providers"] = value; }
}
}
}

View file

@ -0,0 +1,40 @@
using System;
using System.Configuration;
namespace Yavsc.Model.WorkFlow.Configuration
{
public class WFProvider:ConfigurationElement
{
[ConfigurationProperty("name", IsKey=true, IsRequired=true)]
public string Name {
get {
return (string) base ["name"];
}
set { base ["name"] = value; }
}
[ConfigurationProperty("type")]
public string Type {
get { return (string) this ["type"]; }
set {
this ["type"] = value;
}
}
[ConfigurationProperty("applicationName")]
public string ApplicationName {
get {
return (string)this ["applicationName"];
}
set {
this ["applicationName"] = value;
}
}
[ConfigurationProperty("connectionStringName")]
public string ConnectionStringName {
get { return (string)this ["connectionStringName"]; }
set { this ["connectionStringName"] = value; }
}
}
}

View file

@ -0,0 +1,22 @@
using System;
using System.Configuration;
namespace Yavsc.Model.WorkFlow.Configuration
{
public class WFProviderCollection : ConfigurationElementCollection
{
protected override object GetElementKey (ConfigurationElement element)
{
return ((WFProvider) element).Name;
}
protected override ConfigurationElement CreateNewElement ()
{
return new WFProvider();
}
public WFProvider GetElement (string name)
{
return this.BaseGet (name) as WFProvider;
}
}
}

View file

@ -0,0 +1,38 @@
using System;
using System.Configuration;
namespace Yavsc.Model.WorkFlow.Configuration
{
public class WorkflowConfiguration : ConfigurationSection
{
/// <summary>
/// Gets or sets the default provider.
/// </summary>
/// <value>The default provider.</value>
[ConfigurationProperty("defaultProvider")]
public string DefaultProvider {
get { return (string)base ["defaultProvider"]; }
set { base ["defaultProvider"] = value; }
}
/// <summary>
/// Gets or sets the providers.
/// </summary>
/// <value>The providers.</value>
[ConfigurationProperty("providers")]
[ConfigurationCollection(typeof(WFProvider),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public WFProviderCollection Providers {
get {
return this["providers"] as WFProviderCollection;
}
set {
this["providers"]=value;
}
}
}
}

View file

@ -0,0 +1,108 @@
using System;
using Yavsc.Model.WorkFlow;
using System.Configuration;
using Yavsc.Model.WorkFlow.Configuration;
using System.Collections.Specialized;
namespace Yavsc.Model.WorkFlow
{
/// <summary>
/// Work flow manager.
/// It takes orders store them and raise some events for modules
/// It publishes estimates and invoices
/// </summary>
public static class WorkFlowManager
{
public static event EventHandler NewOrder;
public static Estimate GetEstimate (long estid)
{
return ContentProvider.GetEstimate (estid);
}
public static Estimate [] GetEstimates (string client)
{
return ContentProvider.GetEstimates (client);
}
public static void UpdateWritting (Writting wr)
{
ContentProvider.UpdateWritting (wr);
}
public static void DropWritting (long wrid)
{
ContentProvider.DropWritting (wrid);
}
public static void DropEstimate (long estid)
{
ContentProvider.DropEstimate(estid);
}
static IContentProvider contentProvider;
public static IContentProvider ContentProvider {
get {
WorkflowConfiguration c = (WorkflowConfiguration) ConfigurationManager.GetSection ("system.web/workflow");
if (c == null)
throw new Exception ("No system.web/workflow configuration section found");
WFProvider confprov = c.Providers.GetElement (c.DefaultProvider);
if (confprov == null)
throw new Exception ("Default workflow provider not found (system.web/workflow@defaultProvider)");
string clsName = confprov.Type;
if (clsName == null)
throw new Exception ("Provider type not specified (system.web/workflow@type)");
if (contentProvider != null)
{
if (contentProvider.GetType ().Name != clsName)
contentProvider = null;
}
if (contentProvider == null)
{
Type cpt = Type.GetType (clsName);
if (cpt == null)
throw new Exception (string.Format("Type not found : {0} (wrong name, or missing assembly reference?)",clsName));
System.Reflection.ConstructorInfo ci =cpt.GetConstructor (System.Type.EmptyTypes);
contentProvider = (IContentProvider)ci.Invoke (System.Type.EmptyTypes);
}
contentProvider.ApplicationName = confprov.ApplicationName;
NameValueCollection config = new NameValueCollection ();
config.Add ("name", confprov.Name);
config.Add ("connectionStringName", confprov.ConnectionStringName);
config.Add ("applicationName", confprov.ApplicationName);
contentProvider.Initialize (confprov.Name, config);
return contentProvider;
}
}
/// <summary>
/// Creates the estimate.
/// </summary>
/// <returns>The estimate identifier.</returns>
/// <param name="title">Title.</param>
public static long CreateEstimate(string client, string title)
{
return ContentProvider.CreateEstimate (client, title);
}
public static long Write(long estid, string desc, decimal ucost, int count, long productid)
{
return ContentProvider.Write(estid, desc, ucost, count, productid);
}
public static void SetEstimateStatus(long estid, int status, string username)
{
ContentProvider.SetEstimateStatus (estid, status, username);
}
}
}

View file

@ -8,7 +8,7 @@
<ProjectGuid>{68F5B80A-616E-4C3C-91A0-828AA40000BD}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>yavscModel</RootNamespace>
<AssemblyName>yavscModel</AssemblyName>
<AssemblyName>YavscModel</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -67,6 +67,16 @@
<Compile Include="IModule.cs" />
<Compile Include="WorkFlow\BasketImpact.cs" />
<Compile Include="WorkFlow\Commande.cs" />
<Compile Include="Blogs\BlogManager.cs" />
<Compile Include="Blogs\BlogProvider.cs" />
<Compile Include="WorkFlow\WorkFlowManager.cs" />
<Compile Include="WorkFlow\Configuration\Provider.cs" />
<Compile Include="WorkFlow\Configuration\ProviderCollection.cs" />
<Compile Include="WorkFlow\Configuration\WorkflowConfiguration.cs" />
<Compile Include="Blogs\BlogHelper.cs" />
<Compile Include="Blogs\Configuration\BlogProviderConfigurationElement.cs" />
<Compile Include="Blogs\Configuration\BlogProvidersConfigurationCollection.cs" />
<Compile Include="Blogs\Configuration\BlogProvidersConfigurationSection.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>