Skip to content

Commit

Permalink
Cleanup ReSharper recommendations.
Browse files Browse the repository at this point in the history
  • Loading branch information
tylermercier committed Sep 26, 2012
1 parent 18a3a6d commit fc9573a
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 354 deletions.
18 changes: 5 additions & 13 deletions Mvc.Mailer/EmptyHttpContext.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web;

namespace Mvc.Mailer
{
public class EmptyHttpContext : HttpContextBase
{
public override HttpRequestBase Request
{
get
{
namespace Mvc.Mailer {
public class EmptyHttpContext : HttpContextBase {
public override HttpRequestBase Request {
get {
return new HttpRequestWrapper(new HttpRequest("", "", ""));
}
}
Expand Down
13 changes: 3 additions & 10 deletions Mvc.Mailer/ExtensionMethods/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
using System.Web.Mvc;
using System;
using System.Web;
using System.Text;

namespace Mvc.Mailer
{
public static class HtmlHelperExtensions
{
namespace Mvc.Mailer {
public static class HtmlHelperExtensions {
/// <summary>
/// Produces the tag for inline image
/// </summary>
/// <param name="htmlHelper"></param>
/// <param name="contentId">e.g. logo</param>
/// <param name="alt">e.g. Company Logo</param>
/// <returns><img src="cid:logo" alt="Company Logo"/></returns>
public static IHtmlString InlineImage(this HtmlHelper htmlHelper, string contentId, string alt = "")
{
public static IHtmlString InlineImage(this HtmlHelper htmlHelper, string contentId, string alt = "") {
return htmlHelper.Raw(string.Format("<img src=\"cid:{0}\" alt=\"{1}\"/>", contentId, alt));
}

}

}
33 changes: 8 additions & 25 deletions Mvc.Mailer/ExtensionMethods/MailMessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Threading;
using System.Net.Mail;

namespace Mvc.Mailer
{
namespace Mvc.Mailer {
/// <summary>
/// Adds the much needed send method to MailMessage so that you can do the following
/// MailMessage email = new MyMailer().WelcomeMessage();
/// email.Send();
///
/// The underlying implementation utilizes the SMTPClient class to send the emails.
/// </summary>
public static class MailMessageExtensions
{
public static class MailMessageExtensions {
/// <summary>
/// Sends a MailMessage using smtpClient
/// </summary>
/// <param name="message">The mailMessage Object</param>
/// <param name="smtpClient">leave null to use default System.Net.Mail.SmtpClient</param>
public static void Send(this MailMessage message, ISmtpClient smtpClient = null)
{
public static void Send(this MailMessage message, ISmtpClient smtpClient = null) {
smtpClient = smtpClient ?? GetSmtpClient();
using (smtpClient)
{
using (smtpClient) {
smtpClient.Send(message);
}
}
Expand All @@ -38,22 +27,16 @@ public static void Send(this MailMessage message, ISmtpClient smtpClient = null)
/// <param name="message">The mailMessage Object</param>
/// <param name="userState">The userState</param>
/// <param name="smtpClient">leave null to use default System.Net.Mail.SmtpClient</param>
public static void SendAsync(this MailMessage message, object userState = null, ISmtpClient smtpClient = null)
{
public static void SendAsync(this MailMessage message, object userState = null, ISmtpClient smtpClient = null) {
smtpClient = smtpClient ?? GetSmtpClient();
smtpClient.SendAsync(message, userState);
}

public static ISmtpClient GetSmtpClient()
{
if (MailerBase.IsTestModeEnabled)
{
public static ISmtpClient GetSmtpClient() {
if (MailerBase.IsTestModeEnabled) {
return new TestSmtpClient();
}
return new SmtpClientWrapper();

}

}

}
37 changes: 14 additions & 23 deletions Mvc.Mailer/ExtensionMethods/UrlHelperExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System.Web.Mvc;
using System;
using System.Web;
using System.Text;
using System.Configuration;

namespace Mvc.Mailer
{
public static class UrlHelperExtensions
{
namespace Mvc.Mailer {
public static class UrlHelperExtensions {
public static readonly string BASE_URL_KEY = "MvcMailer.BaseUrl";

/// <summary>
Expand All @@ -16,35 +12,30 @@ public static class UrlHelperExtensions
/// <param name="urlHelper">The object that gets the extended behavior</param>
/// <param name="relativeOrAbsoluteUrl">A relative or absolute URL to convert to Absolute</param>
/// <returns>An absolute Url. e.g. http://domain:port/controller/action from /controller/action</returns>
public static string Abs(this UrlHelper urlHelper, string relativeOrAbsoluteUrl)
{
public static string Abs(this UrlHelper urlHelper, string relativeOrAbsoluteUrl) {
var uri = new Uri(relativeOrAbsoluteUrl, UriKind.RelativeOrAbsolute);
if (uri.IsAbsoluteUri)
{
if (uri.IsAbsoluteUri) {
return relativeOrAbsoluteUrl;
}

Uri combinedUri;
if (Uri.TryCreate(BaseUrl(urlHelper), relativeOrAbsoluteUrl, out combinedUri))
{
if (Uri.TryCreate(BaseUrl(urlHelper), relativeOrAbsoluteUrl, out combinedUri)) {
return combinedUri.AbsoluteUri;
}

throw new Exception(string.Format("Could not create absolute url for {0} using baseUri{0}", relativeOrAbsoluteUrl, BaseUrl(urlHelper)));
}


private static Uri BaseUrl(UrlHelper urlHelper)
{
string baseUrl = ConfigurationManager.AppSettings[BASE_URL_KEY];

private static Uri BaseUrl(UrlHelper urlHelper) {
var baseUrl = ConfigurationManager.AppSettings[BASE_URL_KEY];

//No configuration given, so use the one from the context
if(string.IsNullOrWhiteSpace(baseUrl)){
baseUrl = urlHelper.RequestContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority);
if (string.IsNullOrWhiteSpace(baseUrl)) {
baseUrl = urlHelper.RequestContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority);
}

return new Uri(baseUrl);
}

return new Uri(baseUrl);
}
}

}
}
12 changes: 3 additions & 9 deletions Mvc.Mailer/ILinkedResourceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Net.Mail;
using System.IO;

namespace Mvc.Mailer
{
namespace Mvc.Mailer {
/// <summary>
/// Declares the methods for creating LinkedResources
/// </summary>
public interface ILinkedResourceProvider
{
public interface ILinkedResourceProvider {
/// <summary>
/// Gets a list of resources given their Id and FilePath
/// </summary>
Expand Down
9 changes: 2 additions & 7 deletions Mvc.Mailer/ISmtpClient.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace Mvc.Mailer
{
public interface ISmtpClient : IDisposable
{
namespace Mvc.Mailer {
public interface ISmtpClient : IDisposable {
event SendCompletedEventHandler SendCompleted;
void Send(MailMessage mailMessage);
void SendAsync(MailMessage mailMessage);
Expand Down
44 changes: 15 additions & 29 deletions Mvc.Mailer/LinkedResourceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;

namespace Mvc.Mailer
{
namespace Mvc.Mailer {
/// <summary>
/// This class is a utility class for instantiating LinkedResource objects
/// </summary>
public class LinkedResourceProvider : ILinkedResourceProvider
{
public virtual List<LinkedResource> GetAll(Dictionary<string, string> resources)
{
var linkedResources = new List<LinkedResource>();
foreach (var resource in resources)
{
linkedResources.Add(Get(resource.Key, resource.Value));
}
return linkedResources;
public class LinkedResourceProvider : ILinkedResourceProvider {
public virtual List<LinkedResource> GetAll(Dictionary<string, string> resources) {
return resources
.Select(resource => Get(resource.Key, resource.Value))
.ToList();
}

public virtual LinkedResource Get(string contentId, string filePath)
{
LinkedResource resource = new LinkedResource(filePath, GetContentType(filePath));
resource.ContentId = contentId;
return resource;
public virtual LinkedResource Get(string contentId, string filePath) {
return new LinkedResource(filePath, GetContentType(filePath)) { ContentId = contentId };
}

public virtual ContentType GetContentType(string fileName)
{
string ext = System.IO.Path.GetExtension(fileName).ToLower();

Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
public virtual ContentType GetContentType(string fileName) {
// Tyler: Possible null reference
var ext = System.IO.Path.GetExtension(fileName).ToLower();

if (regKey != null && regKey.GetValue("Content Type") != null)
{
var regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null) {
return new ContentType(regKey.GetValue("Content Type").ToString());
}
return null;
}
}
}
}
Loading

0 comments on commit fc9573a

Please sign in to comment.