Entity Framework on different project - saving? - c#

I have an established SQL Server database setup. I then generated an Entity Framework model in a Console Application to test some selecting, editing and adding to the database. All went well.
I am now moving more towards my final physical design of my WinForms and WebApp. So, I have decided to do the project in separate projects. I moved the Entity Framework to a Data project, I created a Services project, and I still have my console app as a test application (All in the same solution).
I have a ClassLib with data transfer objects to pass between my layers. So my GUI layer and Service layer don't know of the Entity Framework. I have helper methods in my EF project that convert the EF data into List etc...
Eg of a helper method:
using ClassLib;
namespace Data
{
public class PayeeDAL : EntityBase
{
public static List<PayeeDto> GetPayees()
{
var payees = (from payee in Db.payees
select payee).ToList();
List<PayeeDto> reply = new List<PayeeDto>();
foreach (var pa in payees)
{
PayeeDto p = new PayeeDto
{
PayeeId = pa.payee_id,
Name = pa.name,
Deleted = pa.deleted == null
};
reply.Add(p);
}
return reply;
}
}
}
And my data transfer object looks like this:
namespace ClassLib
{
public class PayeeDto
{
public int PayeeId { get; set; }
public string Name { get; set; }
public bool Deleted { get; set; }
}
}
So, my selection is working well with this design... but... have no idea how to handle saving.
In my console application, when the EF was available to me, I did this:
db.AddToaccount_transaction(ac);
account_transaction_line atl = new account_transaction_line
{
amount = amount,
cost_centre =
db.cost_centre.FirstOrDefault(
cc => cc.cost_centre_id == costcentreid),
sub_category =
db.sub_category.First(
sc => sc.sub_category_id == subcategoryId),
account_transaction = ac,
budget = db.budgets.FirstOrDefault(b => b.budget_id == budgetid)
};
db.AddToaccount_transaction_line(atl);
}
db.SaveChanges();
But now I don't have access to .AddTo.... and .SaveChanges... In my Console app, I'd create a parent object, and then add a few child objects... and then add the child objects to the parent object, and save.
But how would this be done in my new structure? I'm thinking I'd have a Save method in each of my helper classes... And then pass a List<> of the child objects, along with a single Parent class to the save method... and then transfor the Dtos to EF models, and then save it that way?
Is that an acceptable plan?

I only use DTO objects to transfer data from A to B. The updating, adding, removing etc., I always encapsulate in Commands (Command Pattern).
Retrieving data I do similarily with "Helper" classes.
Example of command pattern:
The base classes:
namespace Busker.Data.Commands
{
/// <summary>
/// The 'Command' abstract class
/// </summary>
public abstract class Command
{
private string message = "";
public string Message
{
get { return message; }
set { message = value; }
}
private bool success = false;
public bool Success
{
get { return success; }
set { success = value; }
}
private Validator validator = new Validator();
public Validator Validator
{
get { return validator; }
set { validator = value; }
}
private CommandStatusCode statusCode = CommandStatusCode.OK;
public CommandStatusCode StatusCode
{
get { return statusCode; }
set { statusCode = value; }
}
public LoggingLevel LoggingLevel = LoggingLevel.Debug;
//public BuskerContext BuskerContext;
public bool IsValid()
{
if (validator.Errors.Count > 0)
return false;
return true;
}
public abstract void Execute();
public void FailedSubCommand(Command cmd)
{
this.Success = cmd.Success;
this.Message = cmd.message;
}
}
}
namespace Busker.Data.Commands
{
public class Invoker
{
private Command command;
public Command Command
{
get { return command; }
set { command = value; }
}
public void SetCommand(Command command)
{
this.Command = command;
}
public virtual void ExecuteCommand()
{
if (command == null)
throw new Exception("You forgot to set the command!!");
try
{
log(this.command.GetType().Name + " starting execution ");
command.Execute();
if (!command.Success)
{
log(this.command.GetType().Name + " completed execution but failed. Message: " + command.Message + " " + command.StatusCode.ToString());
}
else
log(this.command.GetType().Name + " completed execution. Success!");
}
catch (Exception ex)
{
command.StatusCode = CommandStatusCode.Error;
Loggy.AddError("An unhandled error was caught in " + this.command.GetType().Name + ": " + ex.Message, ex);
command.Message = ex.ToString();
//throw;
}
}
private void log(string msg)
{
switch (command.LoggingLevel)
{
case Busker.Data.Constants.LoggingLevel.Debug:
Loggy.Debug(msg);
break;
case Busker.Data.Constants.LoggingLevel.Off:
break;
default:
Loggy.Add(msg);
break;
}
}
public virtual void ExecuteLinqCommand()
{
this.ExecuteCommand();
}
}
}
namespace Busker.Data.Commands
{
public static class Extensions
{
/// <summary>
/// Executes the command using the default invoker.
/// </summary>
/// <param name="aCommand"></param>
public static void Invoke(this Command aCommand)
{
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
System.Reflection.MethodBase m = stackTrace.GetFrame(1).GetMethod();
String strMethodName = m.DeclaringType.Name + "." + m.Name;
try
{
Invoker invoker = new Invoker();
invoker.SetCommand(aCommand);
invoker.ExecuteCommand();
}
catch (Exception ex)
{
Loggy.AddError("An error occured in Extensions.Invoke. + " + strMethodName ,ex);
throw ex;
}
}
}
Implementation Example:
namespace Busker.Data.Commands.Message
{
public class CreateMessageCommand :Command
{
public CreateMessageCommand (int from, int to, string title, string body)
{
// set private variable..
}
public override void Execute()
{
// Do your stuff here
be.SaveChanges();
this.Success = true;
}
}
}
Usage:
CreateMessageCommand cmd = new CreateMessageCommand (...);
//Don't use the execute method of the command
//the invoker, because implemented as an extension can be exchange in different
//environments
cmd.Invoke();

Related

Is there any solution to handle dataType for TestCaseSource ? [Nunit Framework]

Based on https://gigi.nullneuron.net/gigilabs/data-driven-tests-with-nunit/ website. I have try to create a simple testcase which prepare for read data in the future. But I have no idea how to handle Argument and use it properly
I have try to set as a object, but i think this might not be a correct solution
[TestCaseSource("GetDataString")]
public void TestMethod2(object configs)
{
}
Here is source code
namespace SAP
{
[TestFixture]
public class Scenario1
{
// This one Give System.ArgumentException
[TestCaseSource("GetDataString")]
public void TestMethod(List<Config> configs)
{
Console.WriteLine("Config " + configs);
}
// This one can handle an Exception
[TestCaseSource("GetDataString")]
public void TestMethod2(object configs)
{
}
public static List<Config> GetDataString()
{
var datas = new List<Config>();
datas.Add(new Config("Nick", "Coldson"));
return datas;
}
}
public class Config
{
public string NickName { get; set; }
public string Name { get; set; }
public Config(string nickname, string name)
{
NickName = nickname;
Name = name;
}
}
}
Here is error msg
System.ArgumentException : Object of type 'SAP.Config' cannot be
converted to type 'System.Collections.Generic.List`1[SAP.Config]'.
The testcasesource has slightly different definition pattern. Assuming you use nunit 3 it should be:
[TestCaseSource(typeof(MyTestData), nameof(GetDataString))]
public void TestMethod2(List<Config> configs)
{
...
}
public class MyTestData
{
public static IEnumerable GetDataString()
{
var datas = new List<Config>();
datas.Add(new Config("Nick", "Coldson"));
return new TestCaseData(datas);
}
}
For more info, check the documentation:
https://github.com/nunit/docs/wiki/TestCaseData
Your GetDataString returns a List<Config>.
Meaning, your test method with a [TestCaseSource("GetDataString")] will be executed as many times as many items the list has and your method must match the item type.
//// This one throws System.ArgumentException
//[TestCaseSource("GetDataString")]
//public void TestMethod(List<Config> configs)
//{
// Console.WriteLine("Config " + configs);
//}
// This one is ok
[TestCaseSource("GetDataString")]
public void TestMethod(Config config)
{
Console.WriteLine(config);
}
If you need to get List<Config> instances in your test, then your source must return some collection containing list items.

No default Instance is registered and cannot be automatically determined for type 'EPiServer.Framework.Cache.IRequestCache

I have the following code
[Quartz.DisallowConcurrentExecutionAttribute()]
public class SearchIndexJob : IJob
{
private readonly ILog _Log = null;
private SearchManager _SearchManager;
public SearchIndexJob()
{
_Log = LogManager.GetLogger(GetType());
}
#region IJob Members
public void Execute(IJobExecutionContext context)
{
var container = new StructureMap.Container();
IServiceConfigurationProvider services = new StructureMapConfiguration(container);
var locator = new EPiServer.ServiceLocation.StructureMapServiceLocator(container);
var context2 = new EPiServer.ServiceLocation.ServiceConfigurationContext(HostType.WebApplication, services);
new Mediachase.Commerce.Initialization.CommerceInitialization().ConfigureContainer(context2);
container.Configure(ce =>
{
ce.For<IMarketService>().Use<MarketServiceDatabase>();
ce.For<IMarket>().Use<MarketImpl>();
ce.For<ICurrentMarket>().Singleton().Use<Mediachase.Commerce.Markets.CurrentMarketImpl>();
ce.For<ISynchronizedObjectInstanceCache>().Singleton().Use<EPiServer.Events.RemoteCacheSynchronization>();
ce.For<IObjectInstanceCache>().Use<HttpRuntimeCache>();
//ce.For<ITypeScannerLookup>().Use<FakeTypeScannerLookup>();
ce.For<IWarehouseRepository>().Singleton().Use<Mediachase.Commerce.Inventory.Database.WarehouseRepositoryDatabase>();
ce.For<IChangeNotificationQueueFactory>().Singleton().Use<CommerceChangeQueueFactory>();
ce.For<IPriceService>().Singleton().Use<PriceServiceDatabase>();
ce.For<IPriceDetailService>().Use<PriceDetailDatabase>();
ce.For<IWarehouseInventoryService>().Singleton().Use<WarehouseInventoryProxy>();
ce.For<IInventoryService>().Singleton().Use<InventoryServiceProvider>();
ce.For<IApplicationContext>().Use<FakeAppContext>();
ce.For<CatalogConfiguration>().Use(CatalogConfiguration.Instance);
ce.For<IRequiredMetaFieldCollection>().Singleton().Use<DefaultRequiredMetaFields>();
ce.For<MetaDataContext>().Singleton().Use(() => CatalogContext.MetaDataContext);
//ce.For<EventContext>().HybridHttpOrThreadLocalScoped().Use(eventContext);
ce.For<FrameworkContext>().Use(() => FrameworkContext.Current);
//ce.For<SqlContext>().Use(() => new SqlContext(BusinessFoundationConfigurationSection.Instance.Connection.Database));
ce.For<IChangeNotificationManager>().Singleton().Use<ChangeNotificationManager>();
////ce.For<Mediachase.Commerce.Catalog.ICatalogSystem>().Singleton().Use(() => Mediachase.Commerce.Catalog.CatalogContext.Current);
ce.For<IEventRegistry>().Use<EPiServer.Events.Clients.EventRegistry>();
ce.For<IEventBroker>().Use<FakeEventBroker>();
ce.For<Mediachase.Search.IndexBuilder>().Use<FakeIndexer>();
});
EPiServer.ServiceLocation.ServiceLocator.SetLocator(locator);
string applicationName = context.JobDetail.Description;
if (String.IsNullOrEmpty(applicationName) || applicationName == "all") // index all applications
{
AppDto dto = AppContext.Current.GetApplicationDto();
foreach (AppDto.ApplicationRow row in dto.Application)
{
IndexApplication(row.Name);
}
}
else
{
IndexApplication(applicationName);
}
}
#endregion
void IndexApplication(string applicationName)
{
_Log.Info(String.Format("Creating Search Manager for \"{0}\" Application.", applicationName));
_SearchManager = new SearchManager(applicationName);
_Log.Info("Created Search Manager.");
try
{
_SearchManager.SearchIndexMessage += new SearchIndexHandler(_SearchManager_SearchIndexMessage);
_SearchManager.BuildIndex(true);
}
catch (Exception ex)
{
_Log.Error("Search Manager Failed.", ex);
}
}
void _SearchManager_SearchIndexMessage(object source, SearchIndexEventArgs args)
{
_Log.Info(String.Format("Percent Complete: {0}%, {1}", Convert.ToInt32(args.CompletedPercentage), args.Message));
}
}
public class FakeEventBroker : IEventBroker
{
public bool Enabled { get; set; }
public System.Threading.Tasks.Task RaiseEventAsync(Guid eventId, Object parameter)
{
return null;
}
public event EventHandler<EventReceivedEventArgs> EventReceived;
public event EventHandler<EventMissedEventArgs> EventMissed;
}
public class FakeAppContext : IApplicationContext
{
public bool HasContentModelTypes { get; set; }
public bool DisableVersionSync { get; set; }
}
public class FakeIndexer : Mediachase.Search.IndexBuilder
{
public FakeIndexer() : base("","","")
{
}
}
and I get this error
"No default Instance is registered and cannot be automatically determined for type 'EPiServer.Framework.Cache.IRequestCache"
in this line " _SearchManager.BuildIndex(true);"
Any ideas?
It is hard to tell but I assume you need to register the IRequestCache in your container
I.e.
container.Configure(ce =>
{
ce.For<IMarketService>().Use<MarketServiceDatabase>();
ce.For<IMarket>().Use<MarketImpl>();
ce.For<IRequestCache>().Use<NoRequestCache>(); // or whatever implementation you need
...
}
Schedule Job is trying to Intialize Commerce, Most probably you will require to fix more then IRequestCache including DBContext, See an integration sample here. GIT Integration Sample

HttpSessionStateBase losing property values of inherited type

We are using HttpSessionStateBase to store messages in a set up similar to this working example:
public class HttpSessionMessageDisplayFetch : IMessageDisplayFetch
{
protected HttpSessionStateBase _session;
private IList<ICoreMessage> messages
{
get
{
if (_session[EchoCoreConstants.MESSAGE_KEY] == null)
_session[EchoCoreConstants.MESSAGE_KEY] = new List<ICoreMessage>();
return _session[EchoCoreConstants.MESSAGE_KEY] as IList<ICoreMessage>;
}
}
public HttpSessionMessageDisplayFetch()
{
if (HttpContext.Current != null)
_session = new HttpSessionStateWrapper(HttpContext.Current.Session);
}
public void AddMessage(ICoreMessage message)
{
if (message != null)
messages.Add(message);
}
public IEnumerable<IResultPresentation> FlushMessagesAsPresentations(IResultFormatter formatter)
{
var mToReturn = messages.Select(m => m.GetPresentation(formatter)).ToList();
messages.Clear();
return mToReturn;
}
}
When we pass in a QualityExplicitlySetMessage (which inherits from ICoreMessage, see below) it is saved correctly to messages.
This is how the object looks after being inserted into the messages list, at the end of AddMessage(ICoreMessage message) above.
But when we come to access it after changing controllers the inherited member's properties are null, which causes a variety of null reference exceptions.
This is how the object now looks after we call FlushMessagesAsPresentations. I've commented out var mToReturn... as this tries to access one of these null ref properties.
I'd like to ask the following:
Why is the HttpSessionStateBase failing to capture these values taken
by the inherited type?
Is this an issue in saving to the HttpSession or in retrieving?
Is this anything to do with, as I suspect, inheritance?
Or is the fact I'm potentially calling a new controller that dependency injects the HttpSessionMessageDisplayFetch causing an issue?
I'm a first-time poster so please let me know if I'm making any kind of faux pas - Super keen to learn! Any input is very welcome.
Some potentially useful code snippets:
QualityExplicitlySetMessage
public class QualityExplicitlySetMessage : QualityChangeMessage
{
public QualityExplicitlySetMessage(IQPossession before, IQPossession after, IQEffect qEffect)
: base(before, after, qEffect)
{
IsSetToExactly = true;
}
}
QualityChangeMessage - Working example
public abstract class QualityChangeMessage : CoreMessage, IQualityChangeMessage
{
protected PossessionChange Change;
public PossessionChange GetPossessionChange()
{
return Change;
}
protected QualityChangeMessage(IQPossession before, IQPossession after, IQEffect qEffect)
{
Change = new PossessionChange(before, after, qEffect);
StoreQualityInfo(qEffect.AssociatedQuality);
}
public override IResultPresentation GetPresentation(IResultFormatter formatter)
{
return formatter.GetQualityResult(this);
}
#region IQualityChangeMessage implementation
public int LevelBefore
{
get { return Change.Before.Level; }
}
//... And so on with values dependent on the Change property.
}
CoreMessage - Working example
public abstract class CoreMessage : ICoreMessage
{
public string MessageType
{
get { return GetType().ToString(); }
}
public string ImageTooltip
{
get { return _imagetooltip; }
set { _imagetooltip = value; }
}
public string Image
{
get { return _image; }
set { _image = value; }
}
public int? RelevantQualityId { get; set; }
protected void StoreQualityInfo(Quality q)
{
PyramidNumberIncreaseLimit = q.PyramidNumberIncreaseLimit;
RelevantQualityId = q.Id;
RelevantQualityName = q.Name;
ImageTooltip = "<strong>" + q.Name + "</strong><br/>" + q.Description + "<br>" +
q.EnhancementsDescription;
Image = q.Image;
}
public virtual IResultPresentation GetPresentation(IResultFormatter formatter)
{
return formatter.GetResult(this);
}
}
UserController - Working example.
public partial class UserController : Controller
{
private readonly IMessageDisplayFetch _messageDisplayFetch;
public UserController(IMessageDisplayFetch messageDisplayFetch)
{
_messageDisplayFetch = messageDisplayFetch;
}
public virtual ActionResult MessagesForStoryletWindow()
{
var activeChar = _us.CurrentCharacter();
IEnumerable<IResultPresentation> messages;
messages = _messageDisplayFetch.FlushMessagesAsPresentations(_storyFormatter);
var vd = new MessagesViewData(messages)
{
Character = new CharacterViewData(activeChar),
};
return View(Views.Messages, vd);
}
}

Using { get set } Accessors for Multiple Values in MVC

EDIT: Question Reconstructed.
OK, I have revisited my get and set methods, but I am still very unclear on how it all works.
What I want to achieve is the Model is populated by the Controller, from the values that it takes form the form. This is then sent to the Db_Facade, which compares the uName and uPwd, and if they are equal returns the ACCESS, which will be set for the entire scope of the program.
I don't know if the get and set declarations are done correctly, or if they can be bunched together (If this is possible it would be great because I will be using this for much larger collections of data), and I'm pretty sure I'm implementing them wrong as well.
If you can help, my knowledge of Accessors is incredibly limited.
Here is my Compare Login method in my Controller:
public static void Compare_Login(User_Login_View Login_View)
{
User_Model getACCESS = new User_Model(); // Creates a new oject of User_Model
getACCESS.Name = Login_View.txtUsername.Text; //Populates the Model from the Login View
getACCESS.Pwd = Login_View.txtPassword.Text;
if (getACCESS.ACCESSLEVEL > 0)
{
Login_View.Close();
}
else
{
Login_View.lblError.Visible = true;
}
Login_View.Menu.SetMenuView();
}
Here is my Model:
public class User_Model
{
public string Name
{
get
{
return Db_Facade.uName;
}
set
{
Db_Facade.uName = value;
}
}
public string Pwd
{
get
{
return Db_Facade.uPwd;
}
set
{
Db_Facade.uPwd = value;
}
}
public int ACCESSLEVEL
{
get
{
return Db_Facade.ACCESS;
}
set
{
Db_Facade.ACCESS = value;
}
}
}
Here is the dummy database comparison:
class Db_Facade
{
public static string uName;
public static string uPwd;
public static string cPwd;
public static int ACCESS;
public static void getLoginACCESS()
{
uName = "paul";
uPwd = "pwd";
ACCESS = 1;
/* I get a "getACCESS does not exist" error here
if (uName == getACCESS.Name && uPwd == getACCESS.Pwd)
{
getACCESS.ACCESSLEVEL = ACCESS;
}
else
{
getACCESS.ACCESSLEVEL = 0;
}
*/
}
}
I don't know if it's needed, but here is my View
public partial class User_Login_View : Form
{
public Menu_View Menu { get; set; }
public User_Login_View()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
User_Controller.Compare_Login(this);
}
}
2 Questions / Hints
1.) Where do you call your getLoginACCESS() ?
2.) Why do you think Db_Facade is able to access getACCESSfrom your class User_Controller?
a solution would be to modyfie your getLoginACCESS() to getLoginACCESS(User_Model getACCESS) and than call it in your Compare_Login(User_Login_View Login_View) befor your if like Db_Facade.etLoginACCESS(getACCESS);

condense c# dnn module settings code

This is a code block I use in most of my DNN Settings modules but it seems too verbose for what it does. How would you condense this to make it more usable or at least less redundant?
/// -----------------------------------------------------------------------------
/// <summary>
/// LoadSettings loads the settings from the Database and displays them
/// </summary>
/// -----------------------------------------------------------------------------
public override void LoadSettings()
{
try
{
if (Page.IsPostBack == false)
{
ddlTreeTabId.DataSource = GetTabs();
ddlTreeTabId.DataBind();
if (!string.IsNullOrEmpty((string)TabModuleSettings["TreeTabID"]))
{ //Look for the tree tab id
this.ddlTreeTabId.SelectedValue = (string)TabModuleSettings["TreeTabID"];
//If we're here, we have a tab module id, now we can grab the modules on that page
LoadTabModules(ddlTreeModuleID, int.Parse((string)TabModuleSettings["TreeTabID"]));
//we only do this part if the proceeding steps checked out
//if we have a tree module id
if (!string.IsNullOrEmpty((string)TabModuleSettings["TreeModuleID"]))
{
try
{
//carefully try to select that item from the module id drop down list
this.ddlTreeModuleID.SelectedValue = (string)TabModuleSettings["TreeModuleID"];
}
catch (Exception ex)
{ //There may have been a module id but it aint on that page any more. Ignore the error.
// I hate invoking try just to ignore an error. seems wasteful.
}
}
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
My ideal solution would implement the properties in such a way that throughout the module a Tree Module ID can be returned without typing all this.
if (!string.IsNullOrEmpty((string)Settings["TreeTabID"]) &&
!string.IsNullOrEmpty((string)Settings["TreeModuleID"]))
{
Do_SomethingWithTheIDs(
int.Parse((string)Settings["TreeTabID"]),
int.Parse((string)Settings["TreeModuleID"]));
}
Imagine the complexity of loading several modules like that. Ugh.
UPDATE
Thanks to Olivier, I've wrote a new class to manage the properties
public class TreeSettingsBase : ModuleSettingsBase
{
public int? TreeTabID
{
get
{
string s = (string)Settings["TreeTabID"];
int id;
return Int32.TryParse(s, out id) ? id : (int?)null;
}
}
public int? TreeModuleID
{
get
{
string s = (string)Settings["TreeModuleID"];
int id;
return Int32.TryParse(s, out id) ? id : (int?)null;
}
}
public int? PersonTabID
{
get
{
string s = (string)Settings["PersonTabID"];
int id;
return Int32.TryParse(s, out id) ? id : (int?)null;
}
}
public int? PersonModuleID
{
get
{
string s = (string)Settings["PersonModuleID"];
int id;
return Int32.TryParse(s, out id) ? id : (int?)null;
}
}
}
So now my LoadSettings looks like this:
public override void LoadSettings()
{
try
{
if (Page.IsPostBack == false)
{
ddlTreeTabId.DataSource = GetTabs();
ddlTreeTabId.DataBind();
if (TreeTabID.HasValue)
{
this.ddlTreeTabId.SelectedValue = TreeTabID.ToString();
LoadTabModules(ddlTreeModuleID, TreeTabID.Value);
if (TreeModuleID.HasValue)
{
try
{
this.ddlTreeModuleID.SelectedValue = TreeModuleID.ToString();
}
catch (Exception ex)
{
}
}
}
ddlPersonTabId.DataSource = GetTabs();
ddlPersonTabId.DataBind();
if (PersonTabID.HasValue)
{
this.ddlPersonTabId.SelectedValue = PersonTabID.ToString();
LoadTabModules(ddlPersonModuleID, PersonTabID.Value);
if (PersonModuleID.HasValue)
{
try
{
this.ddlPersonModuleID.SelectedValue = PersonModuleID.ToString();
}
catch (Exception ex)
{
}
}
}
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
ModuleSettingsBase is available everywhere so TreeTabID, TreeModuleID, PersonTabID and PersonModuleID will be too. That's what I wanted and it uses less code so thanks Olivier!
It would be nice to ditch the try catch but there's no guarantee the value will be in the drop down list otherwise it would be even smaller. Still better though.
Create a wrapper class for your settings.
public class TabModuleSettingsWrapper {
private SettingsCollection _settings; // I do not know of which type your settings are.
public TabModuleSettingsWrapper(SettingsCollection settings) {
_settings = settings;
}
public int? TreeModuleID {
get {
string s = (string)_settings["TreeModuleID"];
int id;
return Int32.TryParse(s, out id) ? id : (int?)null;
}
}
// Repeat this for all the settings
}
Now you can access the settings with:
var settings = new TabModuleSettingsWrapper(TabModuleSettings);
if (settings.TreeTabID.HasValue && settings.TreeModuleID.HasValue) {
Do_SomethingWithTheIDs(settings.TreeTabID, settings.TreeModuleID);
}

Categories